简体   繁体   中英

Multiply value from textbox with combobox and display in label in WPF

I'm pretty new to programming in WPF with xaml and C# and I've searched the forum for a similar problem, but I couldn't find any solution to my problem.

I've made a combobox of three items, each item has a text content. When one of these items are selected, I want to multiply the value in a textbox with a number and display the result in a label. Here is my code:

public MainWindow()
    {
        InitializeComponent();
        int a = Int32.Parse(weight.Text);
        double b;

        if (this.wcf.SelectedItem==weighing)
        {
            b = a * 1.03;
            wll.Content = b.ToString();
        }
        else if (this.wcf.SelectedItem == uptodate)
        {
            b = a * 1.1;
            wll.Content = b.ToString();
        }
        else if (this.wcf.SelectedItem == lessupdated)
        {
            b = a * 1.2;
            wll.Content = b.ToString();
        }

    }

"weight" is the name of the textbox, "wcf" is the name of the combobox, "weighing", "uptodate" and "lessupdated" is name of the combobox items and "wll" is the name of the label. These are defined in xaml in the main window.

You'll need an event handler for your combobox. Something like:

<Combobox x:Name="wcf" SelectionChanged="cb_SelectionChanged">[...]</Combobox>

In your code behind:

private void cb_SelectionChanged(object sender, RoutedEventArgs e)
{
...
}

The method cb_SelectionChanged will be called each time you click on an item.

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