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());
}
}
你能告訴我們你目前擁有的代碼。這聽起來像你需要充分填充ListBoxItem,但需要確保代碼 – 2012-04-17 17:01:39
我只是把代碼放在 – 2012-04-17 23:52:28
哇,代碼是完全不同的問題是什麼(家園和公寓與船和風帆尺寸) 。無論如何,爲了更直接地回答這個問題,「Apartment.GetAddress()」的代碼是什麼?如果你真的想將整個地址轉儲到列表框的一列(與ListView中的多列相似,就像在我的答案中一樣),那麼問題出在你的字符串格式爲Apartment.GetAddress()。很可能你包括CR,LF或兩者。 – GalacticJello 2012-04-18 15:34:14