Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting Items in a list box

Status
Not open for further replies.

afryer

Programmer
Mar 9, 2004
207
GB
Hello All, I hope some can help me with this frustrating problem:

I have developed an application, which uses a tab control with four separate tabs. Each of these tabs contains a different list box. When a tab is clicked then the change evern is obviously fired and in there I have the following code:

Code:
if (me.list_test.ListCount > 0) then
    me.list_test.Selected(1) = true
end if

This sort of works in that when I click on the tab the list box is displayed and the first item is selected. The problem is that when I try to do anything with the selection it says there is no item selected and I have to physically click on the item before it is marked as selected.

In addition I have tried using the ListIndex property to set values, i.e

Code:
me.list_test.ListIndex = 0 ' to select the first item

This again works partly and when it does work it solves the problem above. The problem I have with this is that sometimes when I click between tabs I get an error messge saying that the ListIndex propery has been used incorrectly!!!!

Does anyone have any idea how I can fix either of these problems as I have identified this in the testing of the code and it is holding up release.

Thanks in advance



Andrew
 
How are you getting the selected item from the listbox? Are you using list_test.Text?
Both your methods seem to work for me (Office 2002)

HTH
 
Hi,

The list box is populated from a table and the bound column is set to the primary key of whatever table it is displaying. All I am trying to do is select the first item in the list, which is what .Selected(1) = True should do and as I say it does work to a certain extent, but it still needs to be clicked before it recognises it.

The workaround I have at the moment is to use a global variable, which is will be set to 1 when the tab opens and then set again subsequently when the list box is clicked, but I am sure there must be an easier way to do it!

Cheers


Andrew

Cheers

Andrew
 
As far as I can tell, both methods of selecting item 1 work fine. What code are you then using to return the selected item, and what is the exact error message?
 
Hi,

I don't actually get an error message, what I basically have is a button on my form which gets the currently selected value in the list box using the .ListIndex property of the list. The first time the list is displayed it has a value of -1, which is not item selected. Once I click on the list this obviously changes to whatever has been selected!
 
What do you get if you use the .Text property instead of .ListIndex?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top