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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form to have List1.List(1) selected by default on Form_Load

Status
Not open for further replies.

mark01

Technical User
Jan 17, 2001
600
US
I was wondering if there was a way to have List1.List(1) selected by default when the form is loaded.

Thx
 
Easy:

Code:
    List1.AddItem "Test1"
    List1.AddItem "Test2"
    List1.AddItem "Test3"
    List1.AddItem "Test4"
    List1.Text = "Test3"

Brian :)

 
Of course you may not know what's in the listbox, in which case this will work, provided that there is something in the list:

List1.ListIndex = 0

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Thanks for your replies.

I tried putting List1.ListIndex = 0 in Form_Load, but that item in the list wasn't selected when the form loaded.
 
As the controls are loaded during the Form_Load process it's best to put that code at the end of the Form_Load, and to be sure use Me.Show after you populate the list and before you set the ListIndex

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Hmm, i still can't get that to work. I have this at the very end of my form_load...

Me.Show
List1.ListIndex = 0

My list box is populated before this, and the TabIndex is set to 0...
 
You said in your first post it was List1(1) that you were concerned with. Have you tried List1(1).Listindex=0

I also normally include the form name Form1.List1(1).listindex=0

In addition, put in code to check if the list has something in it, otherwise it will crash.

If Form1.List1(1).Listcount>0 then
Form1.List1(1).listindex=0
else
Exit sub or whatever
End If



 
Oooops, just re-read your post, you state List1.List(1), not an array listbox as I misread. Your code List1.ListIndex = 0 should work, however try putting the form name in front see if makes any difference. The other thing, might be wrong and get more egg on my face, but the syntax List1.List(1), where (1) I think refers to a listindex value. If so and if thats what you want to start up with then I would have thought Form1.List1.Listindex=1 would have been what you wanted. Oh well, dug my hole now, move backwards slowly.
 
If you use the Form_Activate event it will fire every time the form regains focus from within the app, which may not be what is required.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Hmm tried all of it, still can't get it to work. Thanks for the replies.
 
You havent put any idex's in have you. so this wont work. try putting

list1.text = "Please select item from the list"

this is always a winner

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top