By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

How do I get the value of a combobox?

P: n/a
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.
Jul 11 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jul 11, 4:20*pm, jmDesktop <needin4mat...@gmail.comwrote:
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.
Jul 11 '08 #2

P: n/a

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

"jmDesktop" <ne***********@gmail.comwrote in message
news:6b**********************************@a70g2000 hsh.googlegroups.com...
>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.

Jul 11 '08 #3

P: n/a
On Jul 11, 4:28*pm, rhaazy <rha...@gmail.comwrote:
On Jul 11, 4:20*pm, jmDesktop <needin4mat...@gmail.comwrote:


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.- Hide quoted text -
I have it in an button click event.

Jul 11 '08 #4

P: n/a
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().

--
Bjørn Brox
Jul 11 '08 #5

P: n/a
"jmDesktop" <ne***********@gmail.comwrote in message
news:6b**********************************@a70g2000 hsh.googlegroups.com...
>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.

Jul 12 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.