【问题标题】:Adding a List to a TableLayoutPanel through a Loop通过循环将列表添加到 TableLayoutPanel
【发布时间】:2011-03-25 17:59:13
【问题描述】:

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

然后我将匹配航班的结果打印到 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 Austin Texas Miami Florida 180 12/04/2011

【问题讨论】:

    标签: c# list loops tablelayoutpanel


    【解决方案1】:

    没关系,我能弄明白。我只需要在每次循环时重置标签。

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-25
        • 2016-02-26
        • 2016-08-25
        • 2019-03-02
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        相关资源
        最近更新 更多