Get one value from concatenated value displayed in a listbox.

I have a listbox that displays values that are concatenated. I have a button that will execute a function, but I want input from the selected value in the listbox. Is there a way to select a value from the listbox even with the display a concatenated sting? For example, the listbox shows 1102 - Mark Smith, where Employee Id is concatenated with the emplyee name. A user can select an employee and clikc the button, but the underlying function only needs the employee Id number as input. How can I extract the employee ID? Thank you.
dodgerfanAsked:
Who is Participating?
 
anarki_jimbelCommented:
The above comment does the job but not exactly. The return value is "1102 ", i.e. has a space at the end, conversion to a number may fail.

I'd suggest splitting by " ":

string selItem = listBox1.SelectedItem.ToString();

string id = selitem.Split(' ')[0];

Console.WriteLine(id)'

Open in new window


Alternatively you may find the first occurance of " " character and get a substring from 0 to this index (not inclusive).
0
 
NorieVBA ExpertCommented:
You could use Split to get the ID.
string selItem = listBox1.SelectedItem.ToString();

string id = selitem.Split('-')[0];

Console.WriteLine(id)'

Open in new window

0
 
dodgerfanAuthor Commented:
Perfect, thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.