|
-
May 2nd, 2013, 01:08 PM
#1
Thread Starter
Member
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
-
May 2nd, 2013, 01:15 PM
#2
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
-
May 2nd, 2013, 01:38 PM
#3
Thread Starter
Member
Re: Setting ListBox1.selectedindex to match a variable.
Thanks,
doesnt seem to work.
gives system.data.rowview as the value.
-
May 2nd, 2013, 01:42 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|