通过循环将列表添加到 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
I have a file that stores flight information and then I have a search form that allows a user to select a starting city and state, destination city and state, the departure date and number of seats they want to book.
I then have the results of the matched flights getting printed into a TableLayoutPanel. My issue is that when the program loops through to find the flights, it adds them, but if it finds multiple flights, the previous indexes are all replaced with the current one. Here is my code that searches through the flights (the lists are all label lists):
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();
}
And here is my code for printing it to the table:
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);
}
}
}
And this is what an example flight would look like that is stored in the file:
Southwest 80 Austin Texas Miami Florida 180 12/04/2011
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没关系,我能弄清楚。我只需要每次循环时重置标签。
Nevermind I was able to figure it out. I just needed to reset the labels each time when going through the loop.
您似乎没有设置表格布局面板来允许添加多个航班。
我假设您正在使用视觉工作室。使用 ui 编辑器创建表格布局面板并研究它在设计文件中创建的代码。然后添加另一行数据并再次研究设计文件并记下添加的内容。
确保您的代码符合 VS 所做的一切以创建新行/列。
然后练习在 VS 中填充行/列,并确保您的代码也正确执行
It does not seem like you have your table layout panel setup to allow multiple flights to be added.
I's assuming you are using visual studio. Create a table layout panel using the ui editor and study the code it creates in the design file. Then add another row of data and study the design file again and take note of what was added.
Make sure that your code des everything VS does to make a new row/col.
Then practice populating the rows/columns in VS and make sure your code does the corectly also