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).
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:
ComboBox
class I'd recommend using the ValueMember property to store the ID and then using this rather than just using the displayed text.ComboBox
you can use the SelectedItem property to access the ID.ComboBox
you can use the SelectedItem proeprty to access the ID.