简体   繁体   中英

How to get the selected Item value from combo box

With the following code the combobox cbAdditionalFields is not holding the selected value in SelectedItem property. How to get the selected item property of the combobox? and on selection changed not focus

var userFields = recordType.UserFields.Where(u => u.Format == UserFieldFormats.String);

cbAdditionalFields.DataSource = userFields.ToList();
cbAdditionalFields.DisplayMember = "Name";

This might do the trick for you

private void cbAdditionalFields_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // ... Get the ComboBox.
    var comboBox = sender as ComboBox;
    //string value = comboBox.SelectedItem as string;
    UserField value = comboBox.SelectedItem as UserField;
    Console.WriteLine(value.Name);
}

Now value contains the currently selected item in the ComboBox.

you can solve this issue by setting the selected index or selected value property of the ComboBox. cb.selectedIndex = 0;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM