Click here to Skip to main content
15,914,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

I have a ComboBox with items in.

The items entry will be 1:Apple, 2:Grape , up to 181:Mango

What i want to do is to get the number value in front of the :

Can anyone please help

Thanks
Posted

You can but this is wrong approach.

The right approach is: don't store strings in the list of the ComboBox, store instances of some class or structure:

class ItemHelper {
    internal ItemHelper(string name, int id) {
        this.fName = name; this.fId = id;
    }
    public override void ToString() {
        return string.Format("{0}:{1}", this.fId, this.fName);
    }
    internal Name { get { return fName; } }
    internal Id { get { return fId; } }
    string fName;
    int fId;
}


Why ToString is overridden? To define how the item is presented in the string form when added to the list (of ComboBox or any other list, also a TreeView).

This way, you can access to the Id directly, without any string tricks, in a reliable way. Well, you will need to cast the item you need to ItemHelper.

—SA
 
Share this answer
 
Comments
Christian Graus 23-Jun-11 4:33am    
Interesting. I gave a correct answer, with code. Someone else gave half the answer I gave, after me. you gave far better advice than both of us. The dude with the worst advice, got marked as the answer.
Sergey Alexandrovich Kryukov 23-Jun-11 4:54am    
It happens, and not unusual :-). Thanks for your good words.
--SA
Prerak Patel 23-Jun-11 4:42am    
Yes SA, good answer. But OP might be interested in showing both - id:name.
Still 5 for good advice.
Sergey Alexandrovich Kryukov 23-Jun-11 4:52am    
No, no. I do show both, please take a look. And I also allow to retrieve both.
Thank you.
--SA
USe the split method of hte string class to split on the :. Then use int.TryParse to parse the first string in the array.

string [] s = comboBox1.Text.Split( new char[] { ':' } );
int n;

if (int.TryParse(s[0], out n))
{
// success.

}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Jun-11 4:32am    
My 4 this time. There is much more reliable and universal technique; please see my solution.
--SA
Christian Graus 23-Jun-11 4:34am    
You are, of course, correct, I answered the question at hand, instead of answering the underlying problem. Of course, the guy who gave a worse version of my answer, got marked as the right answer, so, go figure.
Sergey Alexandrovich Kryukov 23-Jun-11 4:37am    
No wonder, nothing to figure out. And no problem, isn't it?
--SA
If 1:Apple is selected, you want 1. Right?
Try ComboBox1.SelectedText.Split(new char[]{':'})[0]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Jun-11 4:36am    
My 4 this time. There is much more reliable and universal technique; please see my solution.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900