简体   繁体   English

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

[英]Displaying multiple information in messagebox from listbox

I am working on a program that is having me input information about a boat and add that information to a listbox. 我正在开发一个程序,让我输入有关船的信息并将该信息添加到列表框中。 the only thing that will show up in the listbox is the name of the boat. 列表框中显示的唯一内容是船名。 I need to know how to display all the information from the other textboxes such as length, sail size, and engines in a messagebox. 我需要知道如何在消息框中显示其他文本框中的所有信息,例如长度,航行尺寸和引擎。 Any help is appreciated. 任何帮助表示赞赏。

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

If you are saying that you want to display multiple columns of data in the ListBox, then you should consider switching to a ListView . 如果您要在ListBox中显示多列数据,则应考虑切换到ListView

You would then add the other column values by using code similar to this: 然后,您将使用类似于以下代码的方式添加其他列值:

Add a ListView control to your form. ListView控件添加到您的窗体。

A am assuming you have 4 text boxes named txtBoatName , txtLength , txtSailSize , txtEngines 假设您有四个名为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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM