通过循环将列表添加到TableLayoutPanel

时间:2011-03-25 17:59:13

标签: c# list loops tablelayoutpanel

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

然后我将匹配的航班的结果打印到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);
                }
            }
        }

这就是存储在文件中的示例航班: 西南80奥斯汀德克萨斯州迈阿密佛罗里达州180 12/04/2011

2 个答案:

答案 0 :(得分:0)

没关系我能搞清楚。我只需要在每次循环时重置标签。

答案 1 :(得分:0)

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

我假设你正在使用visual studio。使用ui编辑器创建表格布局面板,并研究它在设计文件中创建的代码。然后添加另一行数据并再次研究设计文件,并记下添加的内容。

确保您的代码完成VS所做的所有操作以创建新的行/列。

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