[英]Displaying multiple information in messagebox from listbox
我正在開發一個程序,讓我輸入有關船的信息並將該信息添加到列表框中。 列表框中顯示的唯一內容是船名。 我需要知道如何在消息框中顯示其他文本框中的所有信息,例如長度,航行尺寸和引擎。 任何幫助表示贊賞。
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());
}
}
如果您要在ListBox中顯示多列數據,則應考慮切換到ListView 。
然后,您將使用類似於以下代碼的方式添加其他列值:
將ListView控件添加到您的窗體。
假設您有四個名為txtBoatName , txtLength , txtSailSize , 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.