2012-04-17 120 views
0

我正在研究一個程序,該程序讓我輸入有關船的信息並將該信息添加到列表框中。唯一會出現在列表框中的是船的名字。我需要知道如何顯示其他文本框中的所有信息,例如長度,風帆大小和引擎在消息框中。任何幫助表示讚賞。在列表框中顯示消息框中的多個信息

public partial class Form1 : Form 
{ 
    ArrayList Home; 
    public Form1() 
    { 
     InitializeComponent(); 
     Home = new ArrayList(); 

    } 

    private void btnAddApartment_Click(object sender, EventArgs e) 
    { 
     //instantiate appartment and add it to arraylist 
     try 
     { 
      Apartment anApartment = new Apartment(txtID.Text, txtAddress.Text, int.Parse(txtYearBuilt.Text), int.Parse(txtBedrooms.Text), 
       double.Parse(txtSquareFootage.Text), double.Parse(txtPrice.Text), txtFurnished.Text); 
      Home.Add(anApartment); 
      ClearText(this); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Make sure you entered everything correctly!", "Error", MessageBoxButtons.OK); 
     }    

    } 

    private void btnAddHouse_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      House aHouse=new House(txtID.Text, txtAddress.Text, int.Parse(txtYearBuilt.Text), int.Parse(txtBedrooms.Text), 
       double.Parse(txtSquareFootage.Text), double.Parse(txtPrice.Text),int.Parse(txtGarageCapacity.Text)); 
      Home.Add(aHouse); 
      AddHouseToListBox(); 
      ClearText(this); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Make sure you entered everything correctly!", "Error", MessageBoxButtons.OK); 
     } 
    } 

    private void ClearText(Control controls) 
    { 
     foreach (Control control in controls.Controls) 
     { 
      if (control is TextBox) 
      { 
       ((TextBox)control).Clear(); 
      } 
     } 
    } 

    private void AddHouseToListBox() 
    { 
     lstHouse.Items.Clear(); 
     foreach (House person in Home) 
     { 
      lstHouse.Items.Add(person.GetAddress()); 
     } 
    } 

    private void AddApartmentToListBox() 
    { 
     lstApartment.Items.Clear(); 
     foreach (Apartment persons in Home) 
     { 
      lstApartment.Items.Add(persons.GetAddress()); 
     } 
    } 
+2

你能告訴我們你目前擁有的代碼。這聽起來像你需要充分填充ListBoxItem,但需要確保代碼 – 2012-04-17 17:01:39

+0

我只是把代碼放在 – 2012-04-17 23:52:28

+0

哇,代碼是完全不同的問題是什麼(家園和公寓與船和風帆尺寸) 。無論如何,爲了更直接地回答這個問題,「Apartment.GetAddress()」的代碼是什麼?如果你真的想將整個地址轉儲到列表框的一列(與ListView中的多列相似,就像在我的答案中一樣),那麼問題出在你的字符串格式爲Apartment.GetAddress()。很可能你包括CR,LF或兩者。 – GalacticJello 2012-04-18 15:34:14

回答

1

如果您要表示要在列表框中顯示多列數據,則應考慮切換到ListView

一個的ListView控件添加到您的窗體:

你會然後使用類似下面的代碼添加其他列的值。

一個我假設你有一個名爲txtBoatNametxtLengthtxtSailSize 4個文本框,txtEngines

// You can either set the columns and view in code like below, or use 
// the Form designer in Visual Studio to set them. If you set them in code, 
// place the following in Form.Load 

listView1.View = View.Details; 

listView1.Columns.Add("Boat Name", -2, HorizontalAlignment.Left); 
listView1.Columns.Add("Length", -2, HorizontalAlignment.Left); 
listView1.Columns.Add("Sail Size", -2, HorizontalAlignment.Left); 
listView1.Columns.Add("Engines", -2, HorizontalAlignment.Center); 

// When you want to add a boat to the ListView, use code like the following: 

ListViewItem item1 = new ListViewItem(txtBoatName.Text,0); 
item1.SubItems.Add(txtLength.Text); 
item1.SubItems.Add(txtSailSize.Text); 
item1.SubItems.Add(txtEngines.Text); 

listView1.Items.Add(item1);