Thomas Eikje Gjerde
Thomas Eikje Gjerde

Reputation: 109

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.

Upvotes: 1

Views: 788

Answers (1)

diiN__________
diiN__________

Reputation: 7656

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.

Upvotes: 2

Related Questions