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

1 回答 1

1

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

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

ListView控件添加到您的窗体。

我假设您有 4 个名为txtBoatNametxtLengthtxtSailSizetxtEngines的文本框

// 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);
于 2012-04-17T17:11:03.410 回答