![]() |
List Box
Could someone please explain how to consistently set the first
item in the list box to selected (i.e. highlit) without needing to click on it? I've tried List1.Selected(0) = True and list1.listindex = 0 Both *seemed* to work, but occasionaly fail. Thanks - Kirk PS it's not a MultiSelect listbox. |
List Box
Kirk,
Either one of the code you identified should work. Where do you have it in your code? Perhaps there are other lines that follow it which inadvertently changes the selection. Assuming you're trying to do this when the form loads, try putting it this way and place it as the last line of code in the UserForm_Initialize: If List1.ListCount 0 Then List1.ListIndex = 0 Other things to consider: 1. If you don't unload the form and just hide it, the next time you try to show it, the Initialize event will not fire and whatever was selected before you hid the form will be the one selected when it shows up again. 2. Step through your code to see if any lines are inadvertently changing the listbox selection. "kirkm" wrote: Could someone please explain how to consistently set the first item in the list box to selected (i.e. highlit) without needing to click on it? I've tried List1.Selected(0) = True and list1.listindex = 0 Both *seemed* to work, but occasionaly fail. Thanks - Kirk PS it's not a MultiSelect listbox. |
List Box
I am wondering if placing the reset command in the code for the click event
wouldn't be better than in the initialize event. It is the click event that selects the last ListIndex item, so if after the selection it is reset to 0, the first item should be in the window for the next use, whether or not the initialize event runs. Or am I out in left field? "Vergel Adriano" wrote: Kirk, Either one of the code you identified should work. Where do you have it in your code? Perhaps there are other lines that follow it which inadvertently changes the selection. Assuming you're trying to do this when the form loads, try putting it this way and place it as the last line of code in the UserForm_Initialize: If List1.ListCount 0 Then List1.ListIndex = 0 Other things to consider: 1. If you don't unload the form and just hide it, the next time you try to show it, the Initialize event will not fire and whatever was selected before you hid the form will be the one selected when it shows up again. 2. Step through your code to see if any lines are inadvertently changing the listbox selection. "kirkm" wrote: Could someone please explain how to consistently set the first item in the list box to selected (i.e. highlit) without needing to click on it? I've tried List1.Selected(0) = True and list1.listindex = 0 Both *seemed* to work, but occasionaly fail. Thanks - Kirk PS it's not a MultiSelect listbox. |
List Box
As an after thought, it would be necessary to have executed any use of the
value of the current ListIndex selection prior to the reset if the reset is put into the ListBox click event. "Vergel Adriano" wrote: Kirk, Either one of the code you identified should work. Where do you have it in your code? Perhaps there are other lines that follow it which inadvertently changes the selection. Assuming you're trying to do this when the form loads, try putting it this way and place it as the last line of code in the UserForm_Initialize: If List1.ListCount 0 Then List1.ListIndex = 0 Other things to consider: 1. If you don't unload the form and just hide it, the next time you try to show it, the Initialize event will not fire and whatever was selected before you hid the form will be the one selected when it shows up again. 2. Step through your code to see if any lines are inadvertently changing the listbox selection. "kirkm" wrote: Could someone please explain how to consistently set the first item in the list box to selected (i.e. highlit) without needing to click on it? I've tried List1.Selected(0) = True and list1.listindex = 0 Both *seemed* to work, but occasionaly fail. Thanks - Kirk PS it's not a MultiSelect listbox. |
All times are GMT +1. The time now is 10:46 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com