2011-03-25 138 views
0

我有一個存儲航班信息的文件,然後我有一個搜索表單,允許用戶選擇一個起始城市和州,目的地城市和州,出發日期和數量他們想要預訂的座位。通過循環向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

回答

0

沒關係我能弄明白。每次通過循環時,我只需要重置標籤。

0

它似乎不像你有你的表佈局面板設置,以允許添加多個航班。

我假設你正在使用visual studio。使用ui編輯器創建表格佈局面板並研究它在設計文件中創建的代碼。然後添加另一行數據並再次研究設計文件並記錄添加的內容。

請確保您的代碼能夠阻止VS做出新的行/列。

然後練習填充VS中的行/列,並確保您的代碼也是正確的