Connecting Tech Pros Worldwide Forums | Help | Site Map

Extracting value from combobox

KSor
Guest
 
Posts: n/a
#1: Aug 6 '08
I have a combobox I want to extract the "selected value" BUT

When I ask in the Immidiate window I get this - an object-valuie I think :

? comboBox_Country.SelectedItem

{EstiMate.Components.Exchange.DataLayer.DVExchange Group}
Href: "https://193.169.16.6/Public/PSA%20KUNDER/(21)%20England/"
href: "https://193.169.16.6/Public/PSA%20KUNDER/(21)%20England/"
Name: "(21) England"
name: "(21) England"


And what I want to extract is the "(21) England" in the Name/name of the
object - but how ?

Best regards
KSor, Denmark


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Aug 6 '08

re: Extracting value from combobox


On Aug 6, 2:21*pm, "KSor" <keld.soerensenDELETET...@psa.dkwrote:
Quote:
I have a combobox I want to extract the "selected value" BUT
>
When I ask in the Immidiate window I get this - an object-valuie I think :
>
? comboBox_Country.SelectedItem
>
{EstiMate.Components.Exchange.DataLayer.DVExchange Group}
Href: "https://193.169.16.6/Public/PSA%20KUNDER/(21)%20England/"
href: "https://193.169.16.6/Public/PSA%20KUNDER/(21)%20England/"
Name: "(21) England"
name: "(21) England"
>
And what I want to extract is the "(21) England" in the Name/name of the
object - but how ?
Cast comboBox_Country.SelectedItem to DVExchangeGroup, and use the
Name property.

Jon
Pete Kane
Guest
 
Posts: n/a
#3: Aug 6 '08

re: Extracting value from combobox


KSor wrote:
Quote:
I have a combobox I want to extract the "selected value" BUT
>
When I ask in the Immidiate window I get this - an object-valuie I think :
>
? comboBox_Country.SelectedItem
>
{EstiMate.Components.Exchange.DataLayer.DVExchange Group}
Href: "https://193.169.16.6/Public/PSA%20KUNDER/(21)%20England/"
href: "https://193.169.16.6/Public/PSA%20KUNDER/(21)%20England/"
Name: "(21) England"
name: "(21) England"
>
>
And what I want to extract is the "(21) England" in the Name/name of the
object - but how ?
>
Best regards
KSor, Denmark
>
>
try this ( assuming you've set the DisplayMember correctly )

? comboBox_Country.Text

regards
=?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=
Guest
 
Posts: n/a
#4: Aug 6 '08

re: Extracting value from combobox



"KSor" wrote:
Quote:
I have a combobox I want to extract the "selected value" BUT
>
When I ask in the Immidiate window I get this - an object-valuie I think :
>
? comboBox_Country.SelectedItem
>
{EstiMate.Components.Exchange.DataLayer.DVExchange Group}
Href: "https://193.169.16.6/Public/PSA%20KUNDER/(21)%20England/"
href: "https://193.169.16.6/Public/PSA%20KUNDER/(21)%20England/"
Name: "(21) England"
name: "(21) England"
>
>
And what I want to extract is the "(21) England" in the Name/name of the
object - but how ?
>
Best regards
KSor, Denmark
>
>
>
Hi KSor,

SelectedItem holds an object reference so immediate will do its best to
display useful information from the object. To extract just the Name
information you can cast the object to the proper type and directly read the
Name property:

MyObject myObj = comboBox_Country.SelectedItem as MyObject;
string s = myObj.Name;

Specify displaymember/valuemember on the ComboBox I would imagine you would
display the name and use the url as value

comboBox_Country.DisplayMember = "Name";
comboBox_Country.ValueMember = "Href";

string s = comboBox_Country.Text;
string u = comboBox_Country.SelectedValue.ToString();

Displaymember and Valuemember must be valid properties.

You can override ToString on your object and return the Name property.

public class MyObj
{
public string Name{ get{ return name; } }

public override ToString(){ return Name; }
}

....

string s = comboBox_Country.SelectedItem.ToString();

--
Happy Coding!
Morten Wennevik [C# MVP]



Closed Thread