【问题标题】:Displaying multiple information in messagebox from listbox在列表框中的消息框中显示多个信息
【发布时间】:2012-04-17 16:59:30
【问题描述】:

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

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

【问题讨论】:

  • 你能告诉我们你目前拥有的代码吗?听起来你需要将ListBoxItem完全填满,但需要代码来确定
  • 哇,代码与问题完全不同(房屋和公寓与船和帆尺寸)。无论如何,为了更直接地回答这个问题,“Apartment.GetAddress()”的代码是什么?如果您真的想将整个地址转储到 ListBox 的一列中(相对于 ListView 中的多列,就像我的回答一样),那么问题在于您在 Apartment.GetAddress() 中的字符串格式。您很可能包含 CR、LF 或两者。

标签: c# winforms


【解决方案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-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    相关资源
    最近更新 更多