Setting A ListBox's Selected Index 
Author Message
 Setting A ListBox's Selected Index

I have a ListBox that's populated from a dataset as such:

listBoxState.DataBindings.Clear()
listBoxState.DataSource = GetTable("tlStateProvinces").Tables(0)
listBoxState.DisplayMember = "St_Prov_Name"
listBoxState.ValueMember = "StateProvID"

And I'm trying to set its Selected Index when grabbing a record from the
database.  The record has a field that links to this StateProvinces table,
so I'm looking to find the Item in the ListBox based on its ValueMember.  At
the moment nothing seems to be working.  Is this a problem other people have
had?  Does anyone have any quick sample code?  I've always been doing my
work in ASP .NET with DropDownLists and this is my first VB App and there
are some differences I'm trying to get used to.  Thanks!

David P. Donahue



Sat, 17 Sep 2005 01:28:41 GMT  
 Setting A ListBox's Selected Index


Quote:
> I have a ListBox that's populated from a dataset as such:

> listBoxState.DataBindings.Clear()
> listBoxState.DataSource = GetTable("tlStateProvinces").Tables(0)
> listBoxState.DisplayMember = "St_Prov_Name"
> listBoxState.ValueMember = "StateProvID"

> And I'm trying to set its Selected Index when grabbing a record from the
> database.  The record has a field that links to this StateProvinces table,
> so I'm looking to find the Item in the ListBox based on its ValueMember.  At
> the moment nothing seems to be working.  Is this a problem other people have
> had?  Does anyone have any quick sample code?  I've always been doing my
> work in ASP .NET with DropDownLists and this is my first VB App and there
> are some differences I'm trying to get used to.  Thanks!

.NET makes this one very easy:

Dim li As ListItem
li = listBoxState.Items.FindByValue(stateID)
if not li is nothing then
        li.Selected = True
end if

There is also a FindByText method if you need to search based on the
"St_Prov_Name" field.

--
Patrick Steele
Microsoft .NET MVP
http://radio.weblogs.com/0110109



Sat, 17 Sep 2005 01:55:04 GMT  
 Setting A ListBox's Selected Index
I'm not finding the ListItem class (or, rather, Visual Studio isn't showing
it to me).  Where is that?



Quote:


> > I have a ListBox that's populated from a dataset as such:

> > listBoxState.DataBindings.Clear()
> > listBoxState.DataSource = GetTable("tlStateProvinces").Tables(0)
> > listBoxState.DisplayMember = "St_Prov_Name"
> > listBoxState.ValueMember = "StateProvID"

> > And I'm trying to set its Selected Index when grabbing a record from the
> > database.  The record has a field that links to this StateProvinces
table,
> > so I'm looking to find the Item in the ListBox based on its ValueMember.
At
> > the moment nothing seems to be working.  Is this a problem other people
have
> > had?  Does anyone have any quick sample code?  I've always been doing my
> > work in ASP .NET with DropDownLists and this is my first VB App and
there
> > are some differences I'm trying to get used to.  Thanks!

> .NET makes this one very easy:

> Dim li As ListItem
> li = listBoxState.Items.FindByValue(stateID)
> if not li is nothing then
> li.Selected = True
> end if

> There is also a FindByText method if you need to search based on the
> "St_Prov_Name" field.

> --
> Patrick Steele
> Microsoft .NET MVP
> http://radio.weblogs.com/0110109



Sat, 17 Sep 2005 02:08:41 GMT  
 Setting A ListBox's Selected Index
Also, I'm not seeing the FindByValue function in the Items collection for my
ListBoxes...



Quote:


> > I have a ListBox that's populated from a dataset as such:

> > listBoxState.DataBindings.Clear()
> > listBoxState.DataSource = GetTable("tlStateProvinces").Tables(0)
> > listBoxState.DisplayMember = "St_Prov_Name"
> > listBoxState.ValueMember = "StateProvID"

> > And I'm trying to set its Selected Index when grabbing a record from the
> > database.  The record has a field that links to this StateProvinces
table,
> > so I'm looking to find the Item in the ListBox based on its ValueMember.
At
> > the moment nothing seems to be working.  Is this a problem other people
have
> > had?  Does anyone have any quick sample code?  I've always been doing my
> > work in ASP .NET with DropDownLists and this is my first VB App and
there
> > are some differences I'm trying to get used to.  Thanks!

> .NET makes this one very easy:

> Dim li As ListItem
> li = listBoxState.Items.FindByValue(stateID)
> if not li is nothing then
> li.Selected = True
> end if

> There is also a FindByText method if you need to search based on the
> "St_Prov_Name" field.

> --
> Patrick Steele
> Microsoft .NET MVP
> http://radio.weblogs.com/0110109



Sat, 17 Sep 2005 04:03:27 GMT  
 Setting A ListBox's Selected Index


Quote:
> I'm not finding the ListItem class (or, rather, Visual Studio isn't showing
> it to me).  Where is that?

Sorry.  You're using the ListBox, not the DropDownList.  In that case, I
think you'll need to search using the FindStringExact method (search for
a text match on the items in the listbox).

--
Patrick Steele
Microsoft .NET MVP
http://radio.weblogs.com/0110109



Sat, 17 Sep 2005 04:55:54 GMT  
 Setting A ListBox's Selected Index
David,
are you wanting to force selection on an item in the listbox?

if so, I just got this to work

listbox1.setselected(listbox1.findstringexact("OH"),true)

this will find hilight the first occurrence of OH in the list.

Hope this helps

Charlie



Quote:
> Also, I'm not seeing the FindByValue function in the Items collection for
my
> ListBoxes...





> > > I have a ListBox that's populated from a dataset as such:

> > > listBoxState.DataBindings.Clear()
> > > listBoxState.DataSource = GetTable("tlStateProvinces").Tables(0)
> > > listBoxState.DisplayMember = "St_Prov_Name"
> > > listBoxState.ValueMember = "StateProvID"

> > > And I'm trying to set its Selected Index when grabbing a record from
the
> > > database.  The record has a field that links to this StateProvinces
> table,
> > > so I'm looking to find the Item in the ListBox based on its
ValueMember.
> At
> > > the moment nothing seems to be working.  Is this a problem other
people
> have
> > > had?  Does anyone have any quick sample code?  I've always been doing
my
> > > work in ASP .NET with DropDownLists and this is my first VB App and
> there
> > > are some differences I'm trying to get used to.  Thanks!

> > .NET makes this one very easy:

> > Dim li As ListItem
> > li = listBoxState.Items.FindByValue(stateID)
> > if not li is nothing then
> > li.Selected = True
> > end if

> > There is also a FindByText method if you need to search based on the
> > "St_Prov_Name" field.

> > --
> > Patrick Steele
> > Microsoft .NET MVP
> > http://radio.weblogs.com/0110109



Sat, 17 Sep 2005 10:32:52 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Treeview's node selected item's index

2. Unable to get a selected index on an (asp form) listbox

3. Singleselect Listbox - how to get selected index ?

4. how to set up control's tabindex,and control array's index

5. Set selected Tabstrip index?

6. Setting ListBox item to selected..

7. Setting the item as selected in listboxes and radio buttons

8. Setting Multi-Select prop on ListBox

9. Drop-down Listbox - programatically setting the selected item

10. setting selected item in listbox

11. Multiselect listbox: How to SET selected item

12. How do I Neatly and Cleanly set Selected Property in Large ListBox (MultiSelect=Simple)

 

 
Powered by phpBB® Forum Software