Reputation: 43
I am having a really weird issue with the comboBox in C# Visual Studio.
I have this code
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
if(cmbType.Text == "tiger")
{
chk1.IsChecked = true;
}
}
If I select tiger nothing happens but when I select the one below it which is "bears".. It checks the box
Upvotes: 0
Views: 118
Reputation: 7115
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
if(cmbType.SelectedValue.ToString() == "tiger")
{
chk1.Checked = true;
}
}
Upvotes: 0
Reputation: 1611
You can do it this way
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBoxItem = e.AddedItems[0] as ComboBoxItem;
if (comboBoxItem == null) return;
var content = comboBoxItem.Content as string;
if (content != null && content.Equals("tiger"))
{
cbAnimal.IsChecked = true;
}
}
Regards
Upvotes: 0
Reputation: 2793
You should check the comboBox items based on the index, as the text changes after the selected index
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
if(cmbType.SelectedText.ToString() == "tiger")
{
chk1.IsChecked = true;
}
}
Upvotes: 0
Reputation: 283
Try this:
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
if((string) cmbType.SelectedItem == "tiger")
{
chk1.IsChecked = true;
}
}
Upvotes: 1