c# combobox return selected value code example

Example 1: c# combobox selectedvalue

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox cmb = (ComboBox)sender;
    int selectedIndex = cmb.SelectedIndex;
    int selectedValue = (int)cmb.SelectedValue;

    ComboboxItem selectedCar = (ComboboxItem)cmb.SelectedItem;
    MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));        
}

Example 2: c# if combobox selected index

//detect if no selected item on ComboBox is chosen?
if( ComboBox.SelectedItem == null ) {
   // do something
}