2

I have some Items in a ComboBox. Each Item has an Id and a name.

     ______________
    │_____________▼│
    │111  Simon    │              
    │222  Patrick  │              
    │3333 John     │
    │155555 Ted    │
    └──────────────┘

I need to pass the Id of the selected item to a stored procedure. I will have to Parse part of the item to get only the Id. My problem is, how can I do this when I don't know the length of the Id. (It can be from 1 to 100 characters).

4
  • comboBox1.SelectedText.Split(' ') Commented Jul 16, 2012 at 15:01
  • 1
    What would this return ? mySplit[0] = 111 and mySplit[1] = Simon ? Commented Jul 16, 2012 at 15:02
  • Are the combobox items simple strings, or are you storing an object related to the list? note that all of the split related answers are returning strings for the ID part.... Commented Jul 16, 2012 at 15:11
  • I'm storing a string. I'm using a datareader. myCombo.Items.Add(myReader["someColumn"].ToString()); Commented Jul 16, 2012 at 15:12

4 Answers 4

3

You can just split on the space character and take the first result:

var id = comboBox.SelectedText.Split(' ')[0]; // Using array index
var id = comboBox.SelectedText.Split(' ').First(); // Using LINQ

As an aside:

  • If using the mobile framework ComboBox class I'd recommend using the ValueMember property to store the ID and then using this rather than just using the displayed text.
  • If using the System.Windows.Forms ComboBox you can use the SelectedItem property to access the ID.
  • If using the System.Windows.Controls ComboBox you can use the SelectedItem proeprty to access the ID.
Sign up to request clarification or add additional context in comments.

3 Comments

I tried both approach but I get an empty string. Any idea why ?
The string is empty and if I change the Index to [1] I get an Index out of range exception. I should get the name like simon.
Ooh i found out how to correct this. Your SelectedText.Split does not work. But I changed it for comboBox.SelectedItem.ToString().Split(' ')[0] and now it works thanks. Id still like to understand why the selectedText approach doesnt work though
3

You have to fill the items into the combobox so that:

comboboxName.DataSource = ds;
comboboxName.ValueMember = "Id";
comboboxName.DisplayMember = "Name";

Otherwise use comboboxName.SelectedText.Split(' '); to get these values.

2 Comments

Same here the second solution returns an empty string. Any idea why ?
@PhaDaPhunk, Instead of filling your combobox with 111 Simon │222 Patrick │ 3333 John, etc you should fill it so that you have a list of id's and names then bind it to the datasource property of the combobox as I explained in this answer.
1

Try this:

var foo = str.Split(' ');  //from 111  Simon will get '111'
var id = foo.Length > 1 ? foo[0] : null; 
if(id != null) {
  //do something with '111';
} else {
   //error
}

EDIT

I recommend you do it by using a custom Combobox class:

public class Foo
{
    public string Text { get; set; }
    public int Value { get; set; }

    public Foo(int id, string name)
    {
        Value = id;
        Text = name;
    }

    public override string ToString()
    {
        return Text;
    }
}

And then add the object(foo) into combobox:

comboBox1.Items.Add(new Foo(111, "simon"));

And then you can access the values by casting SelectedItem property to your class:

//assuming that comboBox1.SelectedText is "simon", the following is true:
var val = (Foo)comboBox1.SelectedItem;
val.Value // 111
val.Text // simon

2 Comments

Maybe with enough error checking to prevent failures? And perhaps getting the first split rather than the second?
TetsujinnoOni:Thanks for your comment. I fiexed it. ;)
0

Try String.Split on the selected response. That will split the text of the selected item at the space. You should get a two-element array, with the first member being the id you represent above.

More broadly, though, you might consider modifying your solution to place the value in the .ValueMember property, and the name in the .DisplayMember property (eliminating the need to parse anything). See if your listbox implementation doesn't support that.

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.