1

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

}
Jim
  • 113
  • 2
  • 10

3 Answers3

2

Try to use SelectedItem:

   string accountName = AccountsCmboBx.SelectedItem.ToString();

Please see the difference in post ComboBox SelectedItem vs SelectedValue

Community
  • 1
  • 1
Roman Marusyk
  • 19,402
  • 24
  • 55
  • 90
0
string accountName = AccountsCmboBx.Text; 

Will never have a NullReferenceException if there is no item selected.

Jon
  • 3,080
  • 1
  • 13
  • 26
0

if the text is the value you want, try:

string accountName =  AccountsCmboBx.SelectedText;
Viralwarrior012
  • 185
  • 1
  • 13