-1

So I have two combo boxes, one being dependant on another. I also have a label which I want the price to be displayed in. I would like to assistance on adding a number value to the ComboBox selection, which would then showUp on my Label.

 if (CarModelCB.Text == "Gallardo")
 { 
     lblCarPrice.Text = "180000";
 }

I'm getting quite a few red lines but this is roughly how I want it to be like.

Daffi
  • 506
  • 1
  • 7
  • 20
Abdo
  • 9
  • 2
  • 1
    Could you share a bit of actual code? Also, what UI engine are you using? WinForms? WPF? – Rik The Developer May 12 '16 at 11:05
  • It's a WFA and that was the code I was attempting to write. I'm quite new to this but I was trying to sort out a car dealership where one CB would show brands, the other show models of the brand. Then the code written on the top was to indicate what I wanted to do. – Abdo May 12 '16 at 12:44

2 Answers2

1

I would create a Dictionary, which stores the prices and uses the CarModel names as the keys.

Dictionary<string, int> prices = new Dictionary<string, int>();

prices.Add("Gallardo", 180000);

Then you can simply check for the price in the SelectedIndexChanged event of the ComboBox

private void CarModelCB_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        lblCarPrice.Text = prices[CarModelCB.Text].ToString();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
SLNC
  • 11
  • 2
0

I would use simple Databinding... First you will need a Model like this ...

class Car
{
    public string Model { get; set; }
    public decimal Price { get; set; }
}

Beware: You should probably implement INotifyPropertyChanged. There are a lot of examples on the internet.

Then bind the comboboxes to the model and set the relevant Datamember.

Daffi
  • 506
  • 1
  • 7
  • 20