0

我有一个存储航班信息的文件,然后我有一个搜索表单,允许用户选择出发城市和州、目的地城市和州、出发日期和他们想要预订的座位数。

然后我将匹配航班的结果打印到 TableLayoutPanel 中。我的问题是,当程序循环查找航班时,它会添加它们,但如果它找到多个航班,则以前的索引都会被当前的索引替换。这是我搜索航班的代码(列表都是标签列表):

    private void searchFlights()
    {
        StreamReader sr = File.OpenText("F:\\C#\\Airline\\Flight.txt");
        string read = null;
        Button button = new Button();
        button.Text = "Book";
        totalSeats = int.Parse(peopleSearchComboBox.Text);
        while ((read = sr.ReadLine()) != null)
        {
            String[] flights = read.Split(' ');
            testSeats = int.Parse(flights[6]);
            if (cityStartSearchTextBox.Text == flights[2] & stateStartComboBox.Text == flights[3] & cityDestinationSearchTextBox.Text == flights[4] &
                stateDestComboBox.Text == flights[5] & dateSearchTextBox.Text == flights[7] & totalSeats <= testSeats)
            {
                airlineSearchLabel.Text = flights[0];
                priceSearchLabel.Text = flights[1];
                seatSearchLabel.Text = flights[6];
                startCityLabel.Text = flights[2];
                startStateLabel.Text = flights[3];
                endCityLabel.Text = flights[4];
                endStateLabel.Text = flights[5];

                price.Add(priceSearchLabel);
                airline.Add(airlineSearchLabel);
                seatsMatch.Add(seatSearchLabel);
                buttons.Add(button);
                cityStartMatch.Add(startCityLabel);
                stateStartMatch.Add(startStateLabel);
                cityDestMatch.Add(endCityLabel);
                stateDestMatch.Add(endStateLabel);
                flightsMatched++;
                Console.WriteLine(airline[0].Text); //I have this to check the index and on each pass through its different
            }
        }
        sr.Close();
    }

这是我将其打印到表格的代码:

private void fillTable()
    {
        blankTableLabel.Text = "";
        priceTableLabel.Text = "Price";
        seatsTableLabel.Text = "Open Seats";
        airlineTableLabel.Text = "Airline";
        noMatchedFlightsLabel.Text = "No Matches Found";
        flightsSearchedTable.RowCount = flightsMatched + 1;
        flightsSearchedTable.Controls.Add(blankTableLabel,0,0);
        flightsSearchedTable.Controls.Add(priceTableLabel,1,0);
        flightsSearchedTable.Controls.Add(airlineTableLabel,2,0);
        flightsSearchedTable.Controls.Add(seatsTableLabel,3,0);

        if (AppendTexts.totalFlights != 0 & flightsMatched != 0)
        {
            for (int x = 0; x < flightsMatched; x++)
            {
                if (WelcomeScreen.memberLoggedInCheck == true)
                {
                    flightsSearchedTable.Controls.Add(buttons[x]);
                    flightsSearchedTable.Controls.Add(price[x]);
                    flightsSearchedTable.Controls.Add(airline[x]);
                    flightsSearchedTable.Controls.Add(seatsMatch[x]);
                }
                else
                {
                    flightsSearchedTable.Controls.Add(price[x],1,x+1);
                    flightsSearchedTable.Controls.Add(airline[x],2,x+1);
                    flightsSearchedTable.Controls.Add(seatsMatch[x],3,x+1);
                }
            }
        }

这就是存储在文件中的示例航班的样子: Southwest 80 Austin Texas Miami Florida 180 12/04/2011

4

2 回答 2

0

您似乎没有设置表格布局面板来允许添加多个航班。

我假设您使用的是视觉工作室。使用 ui 编辑器创建一个表格布局面板,并研究它在设计文件中创建的代码。然后添加另一行数据并再次研究设计文件并记下添加的内容。

确保您的代码对 VS 所做的一切都是为了创建新的行/列。

然后练习填充 VS 中的行/列,并确保您的代码也正确执行

于 2011-03-25T19:02:51.273 回答
0

没关系,我能够弄清楚。每次通过循环时,我只需要重置标签。

于 2011-03-25T19:01:18.157 回答