No need to feel bad about not knowing something. Nobody knows it all and we
all started as newbies.
Anyway, it seems like you're doing this the hard way. You can get the index
quite quickly like this:
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim j As Integer
j = lstIncdType.Items.IndexOf _
(lstIncdType.Items.FindByText("Blue"))
Label1.Text = "Blue is item " & _
j.ToString & " (zero-based)"
CheckForText()
End Sub
If you need to check for a partial string, you can get it like this:
Sub CheckForText()
Dim j As Integer
Dim Thingy As ListItem
For Each Thingy In lstIncdType.Items
If InStr(Thingy.Text, "Blu") Then
j = lstIncdType.Items.IndexOf(Thingy)
Exit For
End If
Next
Label1.Text = "Search for 'Blu'... see zero-based item " & _
j.ToString
End Sub
<form id="Form1" method="post" runat="server">
<P>
<asp:ListBox id="lstIncdType" runat="server">
<asp:ListItem Value="Red">Red</asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
<asp:ListItem Value="Black">Black</asp:ListItem>
<asp:ListItem Value="Yellow">Yellow</asp:ListItem>
</asp:ListBox></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>
</form>
You need to put in some error checking because the preceding will give false
results for the zero item.
Let us know if this helps?
Ken
Microsoft MVP [ASP.NET]
"Eeediot" <ee*****@hotmail.com> wrote in message
news:6s********************@giganews.com...
I am trying to write code that will go through the existing list items in a
listbox and perform a string comparision with a supplied string. That
particular item will automatically selected if there is a match. I am
trying to use the IndexOf method to accomplish this task but, alas, I have
been unsuccessful.
If anyone has any ideas or suggestions it would be much appreciated.
Yes, I am a newbie to ASP.NET.
Yes, I am a newbie to VB.NET.
I am familiar with ASP.Classic and maybe that's my problem.
Here's my code:
=====
Protected lstIncdType as ListBox
...
Dim j as Integer
Dim Thingy as ListItem
...
j = 0
For Each Thingy In lstIncdType.Items
If Thingy.Text = CompareString Then
j = IndexOf(What!?!) 'Offending Code...
Exit For
End If
Next
lstIncdType.SelectedIndex = j
...
=====
TIA