By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Stupid Dumba**ed question about ASP.NET...

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
maybe something like:

Protected lstIncdType as ListBox
...
Dim j as Integer
Dim Thingy as ListItem
...

For j=0 to lstIncdType.Items.Count-1
Thingy = CType( lstIncdType.Items(j), ListItem)
If Thingy.Text = CompareString Then
lstIncdType.SelectedIndex = j
Exit For
End If
Next

Eeediot wrote:
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

--
mikeb
Nov 18 '05 #2

P: n/a
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


Nov 18 '05 #3

P: n/a
Use a loop with a counter:

Dim i As Integer
For i = 0 To lstIncdType.Items.Count - 1
If lstIncdType.Items(i).Text = CompareString Then
lstIncdType.SelectedIndex = i
Exit For
End If
Next

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"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

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.