Not a Stupid question about ASP.NET...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Ken Cox [Microsoft MVP] (BANSPAMken_cox_at_sympatico.ca)
Date: 09/02/04


Date: Thu, 2 Sep 2004 19:27:21 -0400

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" <eeediot@hotmail.com> wrote in message
news:6sadnZwJ47lID6rcRVn-rw@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
>
>



Relevant Pages

  • RE: Newbie Question
    ... Subject: Newbie Question ... I have a file that contains many lines, each of which consists of a string ... distribution or copying of this communication is strictly prohibited. ...
    (comp.lang.python)
  • Re: Fast and Safe C Strings: User friendly C macros to Declare and use C Strings.
    ... simply a char array - and passed it to a function that expects a string. ... It's "dangerous" in that a newbie doesn't see the difference between: ... programmer from doing anything beyond newbie-stuff. ...
    (comp.lang.c)
  • Really simple string functions
    ... Hi, I'm a complete newbie to C, but I wanted to get started by writing ... I also wanted to make it terminate when the EOF character is ... This is a string of text % BLAHBLAHBLAH ...
    (comp.lang.c)
  • Re: Passing String to Function
    ... >I'm sure this is newbie but I've done some searching on this and ... >haven't found the exact same situation. ... >string to a function where it get's built up ...
    (comp.lang.c)
  • RE: Attempting to reduce userforms
    ... > VB Newbie: ... > Private Sub CommandButton1_Click ... >>> Dim Ctrl As MSForms.Control ... >>> Dim ob As String ...
    (microsoft.public.excel.programming)