添加列表框项目和以不同标签显示的值(在不同形式之间)

I want to add items and values from form 2 to form 3 Listbox and show it accordingly in the form 3 labels.Final product should look like this form 2 adding the values and form 3 showing the value in menu and listbox accordingly I can add one item like the product name but I also want to add other aspects like price. So when I click on listbox in form 3 ,it should only show the names but in labels it should show the price aspect.

表格2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();

    }

    private void button2_Click(object sender, EventArgs e)
    {

        string getItem = NameTextBox1.Text;
        int getItem2 = Int32.Parse(PriceTxtBox2.Text);
        var f = new Form3(getItem, getItem2);
        f.Show();
    }

表格3:

 public partial class Form3 : Form
{
    public Form3()
    {

    }
    public struct Menu1
    {
        public string foodname;
        public int foodprice;
    }

    public Form3(string title, int txt)
    {
        InitializeComponent();
        List<Menu1> menulist = new List<Menu1>();
        menulist.Add(new Menu1
        {
            foodname = title,
            foodprice = txt
        });


        foreach (Menu1 details in menulist)
        {
            listBox1.Items.Add(String.Format(details.foodname));
            listBox1.Items.Add(details.foodprice);

        }
    }
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        label4.Text = listBox1.SelectedItem.ToString();
        label5.Text = listBox1.SelectedItem.ToString();

    }
}
评论
wquod
wquod

好像您没有连接到数据库,强烈建议您这样做。

但是按照您想要的方式,我想您可以

  1. 将您的列表框更改为可以具有多个列的列表视图或数据网格视图,然后将其他详细信息(例如价格和类别)放在这些列中,然后将其隐藏,仅保留具有用户可见产品名称的第一列,然后您只需在代码的隐藏列中检索文本即可显示在标签中。
  2. 将列表项以列表,多维数组等形式存在于公共静态类中(所有人都可以访问,并且可以跟踪从表2中添加的所有项-尽管仅在运行时不同于数据库)然后以您的形式3进行访问;获取所选项目的索引,然后将其用作索引以访问静态类中的列表。
点赞
评论