Reputation: 113
I am trying to get the selected value of the Combo box and display that in a label. It is in a c# winForm. This is what i have now:
private void AccountsCmboBx_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string accountName = AccountsCmboBx.SelectedValue.ToString();
FromAddrLabel.Text = accountName;
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
Upvotes: 1
Views: 2225
Reputation: 195
if the text is the value you want, try:
string accountName = AccountsCmboBx.SelectedText;
Upvotes: 0
Reputation: 3255
string accountName = AccountsCmboBx.Text;
Will never have a NullReferenceException if there is no item selected.
Upvotes: 0
Reputation: 24569
Try to use SelectedItem:
string accountName = AccountsCmboBx.SelectedItem.ToString();
Please see the difference in post ComboBox SelectedItem vs SelectedValue
Upvotes: 2