Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys,

im working on a project and i just added the data of the datatable to the items of a dropdown control. Here is what i want, when a user selects an item from the dropdown menu i want just a single data to be selected so that i will be able to use it in another function. Example: item 1. 1 Marcus Kane, item 2. 2 Needles Kane, item 3. 3 The Preacher. The problem is i just want the "1" or "2" or "3" in the items mentioned above. i know that it's in the selected index of the dropdown but i just want the "1" or "2" or "3" not the whole of the value or text of the dropdown.selectedindex. Any help will be appreciated. Thanks in advance guys.

VB
Protected Sub bcharacters_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bcharacters.Click
        For c = 0 To dtcharacters.Rows.Count - 1
            drpcharacters.Items.Add(dtcharacters.Rows(c).Item(0) +" "+
dtcharacters.Rows(c).Item(1) +" "+ dtcharacters.Rows(c).Item(2))
        Next
End Sub
Posted
Comments
MugiwaraUK 29-May-12 8:50am    
Do you add the 1 and value yourself to the dropdown list? or does the data come like 1 Marcus Kane?
Michael Sernal 30-May-12 15:06pm    
hi MugiwaraUK,

No, i don't add the 1 and the other values, i retrieve them from a datatable and then after that in the event selectedindexchanged i want to separate the value of the selectedindex wherein i only want to get the values of the column 0 of the datatable from which i retrieved the items of the dropdown control. Any ideas? Thanks.

1 solution

Hey i havn't been working alot in VB but im very into C# and i know that you in C# can get a char from a string simply by doing this:

char c = stringname[index];

for larger numbers you can use a while loop and check if that char is a digit like this:

C#
int i = 0;
string numberindex = null;
while (char.IsDigit(combobox.SelectedText[i]))
{
    numberindex += combobox.SelectedText[i];
    i++;
}
int result = int.Parse(numberindex);


the above should work with a few modifications :)

hope this help
 
Share this answer
 
v3
Comments
Michael Sernal 29-May-12 16:02pm    
hello jackssen,

the above code worked but i encountered some problems. i included a messagebox so that once i already selected an item in the dropdown menu i will be able to see the number. the problem is that there are times that if i haven't finished scrolling down yet a msgbox would already appear with the number of the previous item. have any idea how to fix this? thanks.
Jackie00100 30-May-12 9:00am    
not really sure what you're trying to tell me if you have some sample codes of where the problem(s) may occur i will try to look into it. but what i understand is that the previously number is displayed even that you choose another one? in this case it might be the event handler that have some fail? otherwise try to see if the "new" keyword will do you any good?
Michael Sernal 30-May-12 14:59pm    
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For c = 0 To dtinfo.Rows.Count - 1
drpinfo.Items.Add(dtinfo.Rows(c).Item(1) + " " + dtinfo.Rows(c).Item(2) + " " + dtinfo.Rows(c).Item(3))
Next
End Sub

Protected Sub drpinfo_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles drpinfo.SelectedIndexChanged
Dim i As Integer = 0
Dim numberindex As String = Nothing

While Char.IsDigit(drpinfo.SelectedValue(i))
numberindex += drpinfo.SelectedValue(i)
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While

Dim result As Integer = Integer.Parse(numberindex)

msgbox(result)
End Sub

Here is the code, it works but there are times that the msgbox would display the previous selecteditem, thanks.
Jackie00100 30-May-12 19:06pm    
i most honestly say i cant see any thing wrong so i don't know if it is the control that doesn't work as it should but i cant see what is else would be just now, sorry but i hope you'll figured it out and you found my code above useful.

Michael Sernal 31-May-12 9:40am    
hi jackssen,

i removed the msgbox inside the selectedindexchanged and it works fine when i passed it to another variable which im using in another function. the msgbox was just to test if it's correct. Thanks a lot for the help, have a 5, hehe..☺

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