フライト情報を保存するファイルがあり、ユーザーが出発都市と州、目的地都市と州、予約したい出発日と座席数を選択できる検索フォームがあります。
次に、一致したフライトの結果を 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