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.
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);
}
}
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.
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.