C# Combo box - retrieving selected value .net2.0

How to get selected value from windows combo box c# .net2.0

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 117353 Views - Last Post: 15 October 2011 - 06:49 AM Rate Topic: -----

#4 Chris Evans  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 6
  • Joined: 22-May 08

Re: C# Combo box - retrieving selected value .net2.0

Post icon  Posted 18 June 2008 - 09:45 AM

Hi

This is driving me round the twist.
All I want to do is from a populated Combo box retrieve the currently selected value so that I can pass it to some other method.

I have a combo box called cmboLetters with values set :
AB
BC
CD
DE

This was set in the collection of the control on the gui in VS2005

I have set in code the first viewable item on the combo box so we are not looking at an empty entry by doing the following:

this.cmboLetters.SelectedIndex = 0;

This loads up fine. However, I cannot get the selectedIndex value. When I say value I do not mean the index.
At index[0] here it should show me "AB". Well that was what I was expecting..... :[

I have tried all of the following and the results are:

cmboLetters.SelectedText = ""
cmboLetters.SelectedText.ToString() = ""
cmboLetters.SelectedItem.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue = "System.Data.DataRowView"


Can anyone help?
Was This Post Helpful? 1

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: C# Combo box - retrieving selected value .net2.0

Posted 18 June 2008 - 09:50 AM

For the display value use cmboLetters.SelectedItem.Text for the selected value use cmboLetters.SelectedIndex. Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#6 Chris Evans  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 6
  • Joined: 22-May 08

Re: C# Combo box - retrieving selected value .net2.0

Posted 19 June 2008 - 01:48 AM

View PostPsychoCoder, on 18 Jun, 2008 - 09:50 AM, said:

For the display value use cmboLetters.SelectedItem.Text for the selected value use cmboLetters.SelectedIndex. Hope that helps :)


Thanks for your reply, but there is only combobox.SelectedItem.ToString() available which returns a "System.Data.DataRowView" not the value of the item.

There must be a way t o get the value "AB" from the combo box.
Was This Post Helpful? 1
  • +
  • -

#7 Chris Evans  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 6
  • Joined: 22-May 08

Re: C# Combo box - retrieving selected value .net2.0

Posted 19 June 2008 - 02:12 AM

View PostChris Evans, on 19 Jun, 2008 - 01:48 AM, said:

View PostPsychoCoder, on 18 Jun, 2008 - 09:50 AM, said:

For the display value use cmboLetters.SelectedItem.Text for the selected value use cmboLetters.SelectedIndex. Hope that helps :)


Thanks for your reply, but there is only combobox.SelectedItem.ToString() available which returns a "System.Data.DataRowView" not the value of the item.

There must be a way t o get the value "AB" from the combo box.



I've found a way of doing it. Forget the combo box and use the List box instead. Here you can get the value in the list by doing the following:

lstBox.Text = "AB"

I'm still puzzled by the combo box???!!!!***
Was This Post Helpful? 1
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: C# Combo box - retrieving selected value .net2.0

Posted 19 June 2008 - 07:22 PM

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string txt;

            txt = comboBox1.SelectedItem.ToString();

            MessageBox.Show(txt);

            comboBox1.SelectedIndex = 1;
            
            txt = comboBox1.SelectedItem.ToString();

            MessageBox.Show(txt);
        }





Works for me! :)
Was This Post Helpful? 2

#9 Chris Evans  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 6
  • Joined: 22-May 08

Re: C# Combo box - retrieving selected value .net2.0

Posted 26 June 2008 - 06:45 AM

View Postn8wxs, on 19 Jun, 2008 - 07:22 PM, said:

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string txt;

            txt = comboBox1.SelectedItem.ToString();

            MessageBox.Show(txt);

            comboBox1.SelectedIndex = 1;
            
            txt = comboBox1.SelectedItem.ToString();

            MessageBox.Show(txt);
        }





Works for me! :)



The difference is I am binding the combox control with a datatable. At That point it's a nightmare. Admittedly I agree if you add items into the combo box without the datatable binding, it works.

Sadly for me it's come round to bite my behind again.
I can't use a list box as I have 100 items and as the combox consatinas the list it would be more functional for the user.

I'm still feeling hopefully.....Fingers and legs are still crossed for a bit of help.

Thanks
:)

If anyone can help with this one it would be very much apreciated.
Was This Post Helpful? 1
  • +
  • -

#10 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: C# Combo box - retrieving selected value .net2.0

Posted 27 June 2008 - 12:33 AM

edit--nevermind. I had the same answer as n8wxs. Sorry.

This post has been edited by OliveOyl3471: 27 June 2008 - 01:42 AM

Was This Post Helpful? 1
  • +
  • -

#11 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: C# Combo box - retrieving selected value .net2.0

Posted 29 June 2008 - 10:57 AM

View PostChris Evans, on 26 Jun, 2008 - 08:45 AM, said:

If anyone can help with this one it would be very much apreciated.


Did you figure it out yet?
PsychoCoder is probably the only one here with the right answer. If I understand it correctly, this is what he means:

Change
cmboLetters.SelectedValue.ToString(); = "System.Data.DataRowView"

To
cmboLetters.SelectedValue = cmboLetters.SelectedIndex;

And add:
cmboLetters.DisplayMember = cmboLetters.SelectedItem.Text;

If that doesn't work, maybe you can figure it out from one of these sites:

http://msdn.microsof...y/w67sdsex.aspx
http://forums.micros.......97&SiteID=1
Was This Post Helpful? 1
  • +
  • -

#27 seeker2008  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 1
  • Joined: 12-August 08

Re: C# Combo box - retrieving selected value .net2.0

Posted 12 August 2008 - 05:01 PM

View PostChris Evans, on 18 Jun, 2008 - 09:45 AM, said:

Hi

This is driving me round the twist.
All I want to do is from a populated Combo box retrieve the currently selected value so that I can pass it to some other method.

I have a combo box called cmboLetters with values set :
AB
BC
CD
DE

This was set in the collection of the control on the gui in VS2005

I have set in code the first viewable item on the combo box so we are not looking at an empty entry by doing the following:

this.cmboLetters.SelectedIndex = 0;

This loads up fine. However, I cannot get the selectedIndex value. When I say value I do not mean the index.
At index[0] here it should show me "AB". Well that was what I was expecting..... :[

I have tried all of the following and the results are:

cmboLetters.SelectedText = ""
cmboLetters.SelectedText.ToString() = ""
cmboLetters.SelectedItem.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue = "System.Data.DataRowView"


Can anyone help?




I know this was posted a long time ago, but I figured someone like me may be searching for an answer. I'm not really an expert (hope I'm understanding the question), but I was having a similar problem. Since I have some experience with web applications, I was expecting the combobox to behave like the dropdownlist, which it doesn't. Anyhow, I got the selected value by using

string myStr = myComboBox.Text;

It makes sense, I suppose, since users can actually type in their own values so a Combobox acts as a Textbox as well.
Was This Post Helpful? 2

#28 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: C# Combo box - retrieving selected value .net2.0

Posted 13 August 2008 - 05:40 AM

View PostChris Evans, on 19 Jun, 2008 - 05:12 AM, said:

View PostChris Evans, on 19 Jun, 2008 - 01:48 AM, said:

View PostPsychoCoder, on 18 Jun, 2008 - 09:50 AM, said:

For the display value use cmboLetters.SelectedItem.Text for the selected value use cmboLetters.SelectedIndex. Hope that helps :)


Thanks for your reply, but there is only combobox.SelectedItem.ToString() available which returns a "System.Data.DataRowView" not the value of the item.

There must be a way t o get the value "AB" from the combo box.



I've found a way of doing it. Forget the combo box and use the List box instead. Here you can get the value in the list by doing the following:

lstBox.Text = "AB"

I'm still puzzled by the combo box???!!!!***


the same code works for a combobox.

string txt = comboBox1.Text;


Was This Post Helpful? 2

#53 deayh4096  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 1
  • Joined: 25-November 08

Re: C# Combo box - retrieving selected value .net2.0

Posted 25 November 2008 - 02:59 PM

Hi

i experienced the same issue too. I managed to resolve it by ensuring i had set the following, on the combobox;

- Datasource
- DisplayMember
- ValueMember

It seems that without the ValueMember the SelectedValue method only returns the object type.

I hope this helps someone out.

Deathy

p.s. you'll have to excuse me if any of the terminology is a little off. I'm relatively new to coding.
Was This Post Helpful? 2

#54 althaz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 25-November 08

Re: C# Combo box - retrieving selected value .net2.0

Posted 25 November 2008 - 07:04 PM

cmbNameHere.Text = "Woman";
string VarName = cmbNameHere.Text;

MessageBox.Show(VarName);

;)

.Text is easy and useful for combo boxed. Very simple.
Was This Post Helpful? 1
  • +
  • -

#58 edg  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 1
  • Joined: 28-December 08

Re: C# Combo box - retrieving selected value .net2.0

Posted 28 December 2008 - 05:22 PM

Not so simple. It depends on the dropdown style. Values are not user editable in DropDownList style. If the style is Simple or DropDwn, use Text to get and set the value displayed in the textbox. If the style is DropDownList, use SelectedItem.ToString() to get the displayed value and Items.Add() to add to the list.

This post has been edited by edg: 28 December 2008 - 05:50 PM

Was This Post Helpful? 2

#59 harry15ster  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 07-April 09

Re: C# Combo box - retrieving selected value .net2.0

Posted 07 April 2009 - 06:08 AM

View PostChris Evans, on 18 Jun, 2008 - 08:45 AM, said:

Hi

This is driving me round the twist.
All I want to do is from a populated Combo box retrieve the currently selected value so that I can pass it to some other method.

I have a combo box called cmboLetters with values set :
AB
BC
CD
DE

This was set in the collection of the control on the gui in VS2005

I have set in code the first viewable item on the combo box so we are not looking at an empty entry by doing the following:

this.cmboLetters.SelectedIndex = 0;

This loads up fine. However, I cannot get the selectedIndex value. When I say value I do not mean the index.
At index[0] here it should show me "AB". Well that was what I was expecting..... :[

I have tried all of the following and the results are:

cmboLetters.SelectedText = ""
cmboLetters.SelectedText.ToString() = ""
cmboLetters.SelectedItem.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue = "System.Data.DataRowView"


Can anyone help?



string value=((System.Data.DataRowView)cmbCat.ComboBox.SelectedValue).Row.ItemArray[0].ToString();



try this , this will return the selected value in the variable "value".
here 'cmbCat' is the name of combobox.

Try this :

private void cmbCat_DropDownClosed(object sender, EventArgs e)
{
String value=((System.Data.DataRowView)cmbCat.ComboBox.SelectedValue).Row.ItemArray[0].ToString();
}


This will return the selected value of combobox in the variable 'value'.

Here 'cmbCat' is the ID of combobox.
Was This Post Helpful? 2

#60 krance  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 22-May 09

Re: C# Combo box - retrieving selected value .net2.0

Posted 22 May 2009 - 08:46 PM

View Postharry15ster, on 7 Apr, 2009 - 05:08 AM, said:

View PostChris Evans, on 18 Jun, 2008 - 08:45 AM, said:

Hi

This is driving me round the twist.
All I want to do is from a populated Combo box retrieve the currently selected value so that I can pass it to some other method.

I have a combo box called cmboLetters with values set :
AB
BC
CD
DE

This was set in the collection of the control on the gui in VS2005

I have set in code the first viewable item on the combo box so we are not looking at an empty entry by doing the following:

this.cmboLetters.SelectedIndex = 0;

This helped me a lot ..thnx
This loads up fine. However, I cannot get the selectedIndex value. When I say value I do not mean the index.
At index[0] here it should show me "AB". Well that was what I was expecting..... :[

I have tried all of the following and the results are:

cmboLetters.SelectedText = ""
cmboLetters.SelectedText.ToString() = ""
cmboLetters.SelectedItem.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue.ToString(); = "System.Data.DataRowView"
cmboLetters.SelectedValue = "System.Data.DataRowView"


Can anyone help?



string value=((System.Data.DataRowView)cmbCat.ComboBox.SelectedValue).Row.ItemArray[0].ToString();



try this , this will return the selected value in the variable "value".
here 'cmbCat' is the name of combobox.

Try this :

private void cmbCat_DropDownClosed(object sender, EventArgs e)
{
String value=((System.Data.DataRowView)cmbCat.ComboBox.SelectedValue).Row.ItemArray[0].ToString();
}


This will return the selected value of combobox in the variable 'value'.

Here 'cmbCat' is the ID of combobox.

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2