从列表框中显示消息框中的多个信息

时间:2012-04-17 16:59:30

标签: c# winforms

我正在制作一个程序,让我输入有关船只的信息并将该信息添加到列表框中。列表框中唯一显示的是船的名称。我需要知道如何显示来自其他文本框的所有信息,例如消息框中的长度,风帆大小和引擎。任何帮助表示赞赏。

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

1 个答案:

答案 0 :(得分:1)

如果您要在ListBox中显示多列数据,则应考虑切换到ListView

然后,您可以使用与此类似的代码添加其他列值:

在表单中添加 ListView 控件。

假设您有4个名为 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);