changing the selected item in a listbox

matt0608

New member
Joined
Mar 16, 2005
Messages
3
Programming Experience
Beginner
I am writing a basic database program and I find myself stuck on something fairly simple. I can't figure out how to change which item in my listbox is selected through code. During runtime, the user can simply click the last item in the listbox, but I need to select that last item during design time. Here is an explanation of what i am trying to do. The listbox currently is filled by an access database file. I want to add a new row to this table. As of now, because the first item in the listbox is selected when I run the program, the new row is getting inserted in between the first and third rows instead of adding it after the last row. I'm sure there is a better way to go about adding rows than I have come up with so any other suggestion are appreciated.

Thanks to all,
Matt
 

Ruffnekk

Member
Joined
Mar 17, 2005
Messages
17
Location
Rotterdam, Netherlands
Programming Experience
10+
Can you post a piece of the code where you actually add the item to the list? That would be helpful. You might not even have to select the last item.
 

matt0608

New member
Joined
Mar 16, 2005
Messages
3
Programming Experience
Beginner
This being my first database project, I elected to create the data adapter, oledbConnection, and dataset through the DATA tab in the toolbox.
As a result, this is all the code I have to fill the listbox.

'— Clear the current data in the dataset
Me.DsAircraft.Clear()

'— Fill the Aircraft list dataset
Me.daAircraft.Fill(Me.DsAircraft)

Thanks for your interest in helping me out.
 

Ruffnekk

Member
Joined
Mar 17, 2005
Messages
17
Location
Rotterdam, Netherlands
Programming Experience
10+
You can select the last item item in listbox like this:

[vb]ListBox1.SelectedItem = ListBox1.Items(ListBox1.Items.Count - 1)[/vb]

Just make sure there's at least one item in the listbox (ListBox1.Items.Count > 0).
Also note that the first item in a listbox is referred to as item(0), so you need to subtract 1.
 

matt0608

New member
Joined
Mar 16, 2005
Messages
3
Programming Experience
Beginner
Your the man!

That worked exactly as I had hoped. Thanks so much for your help.

Take care
 
Top Bottom