2012-04-18 176 views
0

我正在爲需要用戶輸入有關其家庭的數據的程序編寫代碼。他們有兩個選擇。他們可以輸入關於他們家或公寓的信息。他們輸入關於房屋編號,地址,臥室,建成年份,價格和麪積的數據,並在兩個單獨的文本框中輸入是否提供其佈置(這是用於公寓選項)或他們進入車庫容量的信息(這是爲房子選項)。這六個參數適用於基本類別,裝修或車庫容量是公寓或住宅的兩個子類別。當用戶點擊「添加公寓」或「添加房子」按鈕時,地址應該進入公寓列表框或家庭列表框。這是我遇到障礙的地方。將單個數據片段添加到多個列表框中?

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()); 
    } 
} 
+0

asp.net? Silverlight的? WPF?的WinForms? – 2012-04-18 00:17:50

+0

它在C#窗體表單應用程序中 – 2012-04-18 00:19:12

+0

請不要用「C#」等標題來標題。這就是標籤的用途。 – 2012-04-18 01:35:37

回答

1

你需要調用AddApartmentToListBox在btnAddApartment_Click

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); 
    AddApartmentToListBox(); 
    ClearText(this); 
} 
catch (Exception) 
{ 
    MessageBox.Show("Make sure you entered everything correctly!", "Error", MessageBoxButtons.OK); 
}    

}

而且不是每次都清理和添加到列表框,你可以用

lstApartment.Items.Add(anApartment.GetAddress()); 

和替換AddApartmentToListBoxAddHouseToListBox

lstHouse.Items.Add(aHouse.GetAddress()); 
相關問題