Jim
Jim

Reputation: 113

How do I show the selected value of a combobox in a label c#?

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

Answers (3)

Viralwarrior012
Viralwarrior012

Reputation: 195

if the text is the value you want, try:

string accountName =  AccountsCmboBx.SelectedText;

Upvotes: 0

Jon
Jon

Reputation: 3255

string accountName = AccountsCmboBx.Text; 

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

Upvotes: 0

Roman Marusyk
Roman Marusyk

Reputation: 24569

Try to use SelectedItem:

   string accountName = AccountsCmboBx.SelectedItem.ToString();

Please see the difference in post ComboBox SelectedItem vs SelectedValue

Upvotes: 2

Related Questions