How do I get the value of a combobox?

J

jmDesktop

I know this should be straight forward, but I have not gotten it yet.

I have a combobox and a textbox. I want to display what is selected
in the combobox in the textbox.

I have:

txtMyTextBox.Text = cboMyBox.SelectedText; //is this right?

But it never shows up. I tried SelectedValue, and SelectedValue.
Thank you. I have been doing only web programming until now, so I was
a little spoiled. I have searched.

Another question please. I went to edit items in Visual Studio
Express and I can enter the text in, but nothing for a corresponding
value like in web express. How can I add items through the designer
with value and text? Thank you again.
 
R

rhaazy

I know this should be straight forward, but I have not gotten it yet.

I have a combobox and a textbox.  I want to display what is selected
in the combobox in the textbox.

I have:

txtMyTextBox.Text = cboMyBox.SelectedText; //is this right?

But it never shows up.  I tried SelectedValue, and SelectedValue.
Thank you.  I have been doing only web programming until now, so I was
a little spoiled.  I have searched.

Another question please.  I went to edit items in Visual Studio
Express and I can enter the text in, but nothing for a corresponding
value like in web express.  How can I add items through the designer
with value and text?  Thank you again.

what method are you using to call the line:
txtMyTextBox.Text = cboMyBox.SelectedText;

You are going to need to implement some sort of "on selection change"
event for your combobox (should be available in event list)
every time you change, you set the text of your textbox.
 
S

sloan

If you're doing winforms, then you have to remember that there is an
~object~ in the combobox, not a value/text pair like you would have in the
web.

Aka, if you bind the combobox to a collection of Employee (objects) , there
is an ~Employee~ object/instance in the combobox.

Try pulling out the SelectedValue, and casting it as (an Employee in this
mock example) your entity type.

...

From memory:

Employee e = cbo1.SelectedValue as Employee;
if(null!=e)
{
Console.Writeline(e.LastName);
}
 
J

jmDesktop

what method are you using to call the line:
 txtMyTextBox.Text = cboMyBox.SelectedText;

You are going to need to implement some sort of "on selection change"
event for your combobox (should be available in event list)
every time you change, you set the text of your textbox.- Hide quoted text -

I have it in an button click event.
 
B

Bjørn Brox

jmDesktop skrev:
I know this should be straight forward, but I have not gotten it yet.

I have a combobox and a textbox. I want to display what is selected
in the combobox in the textbox.

I have:

txtMyTextBox.Text = cboMyBox.SelectedText; //is this right?

But it never shows up. I tried SelectedValue, and SelectedValue.
Thank you. I have been doing only web programming until now, so I was
a little spoiled. I have searched.

Another question please. I went to edit items in Visual Studio
Express and I can enter the text in, but nothing for a corresponding
value like in web express. How can I add items through the designer
with value and text? Thank you again.

Make a SelectionIndexChanged eventhandler for the combobox where you
update your textbox.

Remember that an item in a combobox can be any kind of object, not
necessary a String, but whatever object that have a ToString().
 
J

John Vottero

jmDesktop said:
I know this should be straight forward, but I have not gotten it yet.

I have a combobox and a textbox. I want to display what is selected
in the combobox in the textbox.

I have:

txtMyTextBox.Text = cboMyBox.SelectedText; //is this right?

Nope. SelectedText is the text that is currently selected, not the
currently selected item as text. You want SelectedItem which is an object,
you have to cast or convert it to a string.
But it never shows up. I tried SelectedValue, and SelectedValue.
Thank you. I have been doing only web programming until now, so I was
a little spoiled. I have searched.

SelectedValue is the value of one of the SelectedItem's properties. You set
which property via the ValueMember.
Another question please. I went to edit items in Visual Studio
Express and I can enter the text in, but nothing for a corresponding
value like in web express. How can I add items through the designer
with value and text? Thank you again.

It's not that simple but, much more powerful. If you want more than simple
strings, you bind the combo box to a data source. Then you can set the
DisplayMember and the ValueMember which identify which members of the data
source should be used for displaying and values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top