简体   繁体   中英

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 .

You would then add the other column values by using code similar to this:

Add a ListView control to your form.

A am assuming you have 4 text boxes named 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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