简体   繁体   English

单击 ListBox 项目并获取它们的值并添加到小计

[英]Clicking ListBox items and taking their values and adding to a Subtotal

I'm working on a project right now and I've run across a curious thing that I don't have enough knowledge in to solve.我现在正在做一个项目,我遇到了一个奇怪的事情,我没有足够的知识来解决。

I have a private const decimal listed below which is the value of each item in the list box.我在下面列出了一个私有 const decimal,它是列表框中每个项目的值。 For each item in the listbox clicked, I would like to add it to my subtotal automatically.对于单击的列表框中的每个项目,我想将其自动添加到我的小计中。

How can I go about clicking a listbox item and having the corresponding price ($9.50) go into the Subtotal textbox.我怎样才能 go 关于单击列表框项目并具有相应的价格(9.50 美元) go 进入小计文本框。

Please let me know if I could provide more detail to solve this.如果我能提供更多细节来解决这个问题,请告诉我。

            private const decimal EXTRAS = 9.50m;

            subtotalPrice = deliveryTypePrice + balloonAmount + messagePrice + listBoxItemsPriceTotal;
            salesTaxPrice = subtotalPrice * TAX_RATE;
            totalPrice = subtotalPrice + salesTaxPrice;

            subtotalTextBox.Text = subtotalPrice.ToString("c");
            salesTaxTextBox.Text = salesTaxPrice.ToString("c");
            orderTotalTextBox.Text = totalPrice.ToString("c");

Hey this is quite simple.嘿,这很简单。 Don't try to use ToString() because it is more for formatting as string than be written as string.不要尝试使用 ToString() ,因为它更多地用于格式化为字符串而不是写为字符串。

You can just use it as followed:您可以按如下方式使用它:

private const decimal EXTRAS = 9.50m;

subtotalPrice = deliveryTypePrice + balloonAmount + messagePrice + listBoxItemsPriceTotal;
salesTaxPrice = subtotalPrice * TAX_RATE;
totalPrice = subtotalPrice + salesTaxPrice;

subtotalTextBox.Text = "" + subtotalPrice.ToString("c");
salesTaxTextBox.Text = "" + salesTaxPrice.ToString("c");
orderTotalTextBox.Text = "" + totalPrice.ToString("c");

I didn't test it so it could be that you need to delete the ToString("c") because there will be written crap.我没有对其进行测试,因此您可能需要删除 ToString("c") 因为会写废话。 If that is the Case then use ("" + totalPrice).ToString("c");如果是这种情况,则使用("" + totalPrice).ToString("c"); if you still need to format.如果你还需要格式化。 If not just delete ToString()如果不只是删除 ToString()

If you want to the value of the item you clicked, you can achieve it by subscribing to MouseClick event.如果你想要你点击的item的值,你可以通过订阅MouseClick事件来实现。 And then get the value by property SelectedItem .然后通过属性SelectedItem获取值。

Here is a demo.这是一个演示。

public Form1()
{
    InitializeComponent();
    // Subscribe to "MouseClick"
    listBox1.MouseClick += listBox1_MouseClick;
}

private void Form1_Load(object sender, EventArgs e)
{
    List<decimal> list = new List<decimal> { 1.2m, 3.4m, 6.3m };
    listBox1.DataSource = list;
}

private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
    // Show the selected value in textbox
    decimal value = (decimal)(listBox1.SelectedItem);
    subtotalTextBox.Text = value.ToString("C");
}

The test result,测试结果,

在此处输入图像描述

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

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