Results 1 to 4 of 4

Thread: Setting ListBox1.selectedindex to match a variable.

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Setting ListBox1.selectedindex to match a variable.

    I have poked around the web and forums and found no help.
    I have tried a couple of different ways in code but no joy on a solution to my prob.

    Here is it, I present the user with a ListBox1 of all "reporting periods" for the year to select from but I would like the the "current" period to be the selected index so the user can just click "SELECT" or select a differnet and click select.

    The Current period is stored in a Public Variable (CurrReportPeroidKey)

    the listbox has all the periods listed
    DisplayText = Period Description
    SelectedValue = ReportPeriodKey

    I would like to do somethinglike,

    Set listbox1.selectedindex where listbox1.value = currReportPeriodKey.
    Is there there a way to sift through the all the values in the listbox and find the index that matches the value to my stored variable value?

    the listbox1 is populated via a database table of period.

    I tried something like this..

    For Each Item In ListBox1.SelectedValue
    If Item.ListBox1.SelectedValue = CurrReportPeroidKey Then
    LBindex = Item.ListBox1.SelectedIndex

    End If

    Next
    Last edited by gmack; May 2nd, 2013 at 01:18 PM. Reason: add

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Setting ListBox1.selectedindex to match a variable.

    Loop through your items and check if the item matches your variable. Here is a quick example I whipped up:
    Code:
            'My listbox items are:
            '1
            '2
            '3
            '4
            'Etc.
            Dim fooVariable As String = "4"
            For i As Integer = 0 To ListBox1.Items.Count - 1
                If ListBox1.Items(i).ToString = fooVariable Then
                    ListBox1.SelectedIndex = i
                End If
            Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Re: Setting ListBox1.selectedindex to match a variable.

    Thanks,
    doesnt seem to work.
    gives system.data.rowview as the value.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Setting ListBox1.selectedindex to match a variable.

    You have it bound. That makes it slightly more difficult, since your items are DataRowViews, so what you will have to do is something more like this:

    If DirectCast(ListBox1.Items(i),DataRowView)("SomeField").ToString = fooVariable Then

    The SomeField is whatever field you are displaying, or whatever field you want to compare to fooVariable.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width