简体   繁体   English

如何从我的 listBox1 和 listBox2 中选择的项目更新我的 label?

[英]How do I update my label from selection of items in my listBox1 and listBox2?

So I have 2 list boxes within my form.所以我的表单中有 2 个列表框。 Listbox1 contains different types of items that have a price and Listbox2 contains how much of that item you want to purchase. Listbox1 包含具有价格的不同类型的项目,而 Listbox2 包含您要购买的该项目的数量。 How do I update my price label so when I select both options from each list box it updates the label and gives me a price.如何更新我的价格 label 所以当我 select 每个列表框中的两个选项时,它会更新 label 并给我一个价格。 Here's an example to help you better understand.这是一个示例,可以帮助您更好地理解。

I select the $1.50 Chocolate Chip Cookie item in my ListBox1 and in ListBox2 I select the 1 Dozen Cookie item. I select 我的 ListBox1 和 ListBox2 中的 1.50 美元巧克力曲奇项目 I select 1 打 Cookie 项目。 So I would want my priceLabel to update to $18.00.所以我希望我的 priceLabel 更新为 18.00 美元。 How would I do this?我该怎么做?

As of now I have tried creating some code in the listBox1_SelectedIndexChanged method but I am returned these 3 following values... $0.00 ... $2.00 ... $4.00到目前为止,我已经尝试在listBox1_SelectedIndexChanged方法中创建一些代码,但我返回了以下 3 个值... $0.00 ... $2.00 ... $4.00

Here's my code:这是我的代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            double index = listBox1.SelectedIndex;
            double index2 = listBox2.SelectedIndex;
            double total = index * index2;
            label9.Text = total.ToString("C");
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            const int ESTIMATED_ARRIVAL = 3;
            label10.Text = monthCalendar1.SelectionStart.AddDays(ESTIMATED_ARRIVAL).ToShortDateString();
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

In listBox1_SelectedIndexChanged(object sender, EventArgs e) you use listBox1.SelectedIndex;listBox1_SelectedIndexChanged(object sender, EventArgs e)您使用listBox1.SelectedIndex; and listBox2.SelectedIndex;listBox2.SelectedIndex; , if you refer to ListBox.SelectedIndex Property ,如果您参考ListBox.SelectedIndex Property

ListBox.SelectedIndex Property ListBox.SelectedIndex 属性

Gets or sets the zero-based index of the currently selected item in a ListBox.获取或设置 ListBox 中当前选定项的从零开始的索引。

Property Value适当的价值

Int32整数32

A zero-based index of the currently selected item.当前选定项的从零开始的索引。 A value of negative one (-1) is returned if no item is selected.如果未选择任何项目,则返回负一 (-1) 值。

it just return index of selected item, so for your purpose you must get value of selected item.它只返回所选项目的索引,因此为了您的目的,您必须获得所选项目的价值。

I hope this code be a good guide for you:我希望这段代码对你有一个很好的指导:

形式

Add handler of SelectedIndexChanged event of both list boxes to this method:将两个列表框的SelectedIndexChanged事件的处理程序添加到此方法:

private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{ 
   if (this.listBox1.SelectedIndex > -1 && this.listBox2.SelectedIndex > -1)//You can set default SelectedIndex for list boxes and remove this
   {
        string s1 = this.listBox1.Items[this.listBox1.SelectedIndex].ToString();
        string s2 = this.listBox2.Items[this.listBox2.SelectedIndex].ToString();

        //Now we extracting the number from string

        //NOTE this is a simple implementation. You must change it as your needs.

        //for example 
        //s1 = $1.50 Chocolate Chip Cookie
        //s2 = 1 Dozen Cookie
        int index = s1.IndexOf(' ');//get the index of first space after 1.50 (Number) in s1
        s1 = s1.Substring(1, index);
        index = s2.IndexOf(' ');//get the index of first space after 1 (Number) in s2
        s2 = s2.Substring(0, index);
        if (double.TryParse(s1, out double p1) && double.TryParse(s2, out double p2))
        {
            const int DOZEN = 12;
            double result = p1 * (p2 * DOZEN);
            //or
            //remove const int DOZEN = 12; and simply
            //double result = p1 * (p2 * 12);
            this.label9.Text = result.ToString("C");
        }
        else
        {
            MessageBox.Show("Can not parse double values.");
        }
     }
}

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

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