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

Selected Listbox Row Not Highlighted 2

Status
Not open for further replies.

f64

Technical User
May 23, 2003
24
US
I am selecting a row in a listbox (single column, MultiSelectSingle) by code, and when the listbox is displayed in a userform the selected row is neither highlighted in blue, nor is it enclosed in the focus rectangle. I have tried the Selected, Value, and ListIndex properties, and none have the effect of highlighting the selected row.

Listbox.ListIndex = n
Listbox.Value = “abcdef”
Listbox.Selected(n) = True

Is it possible highlight the selected listbox row in blue by code, the same as it would be if it were clicked on by the mouse?
 
f64,

Where are you running the code to set the Listbox selection? Are you getting an error or does nothing appear to happen? If you test the result of setting the Listbox using a MsgBox or Debug.Print statement, what value is returned?

I cannot duplicate what you are seeing. Using ListBox.ListIndex, I have set it in a code module procedure (prior to displaying the Userform), in the Userform Initialize event handler and from a CommandButton on the form. In all cases, the indicator highlight is displayed.

Regards,
Mike
 
If I have a listbox on a userform, and a command button to select an item:
Code:
Private Sub CommandButton2_Click()
ListBox1.ListIndex = 3
End Sub
selects/highlights that item. Could you describe in more detail what you are doing? It seems to work for me.

Gerry
 
Mike,
I have created a multipage userform to input data to a SIMUL8 simulation, and the user has asked to have the capability to save and restore previous data. When the userform is closed, the contents of all variables, testboxes and listboxes are written to a hidden sheet. When the userform is launched, page 2 prompts the user to either restore previous data or input new (so at this point the userform is displayed). If restore is selected, a subroutine then populates all variables, textboxes, and listboxes with the saved data, so that as the user pages through the form it will look exactly as when first completed. The one thing I have not been able to accomplish is highlighting the original choices in the listboxes.
I have verified, for one specific listbox on page 5, by a breakpoint at the end of this subroutine, that the ListIndex number and Listbox.Value were both restored correctly. And yet when page 5 is reached, the listbox is populated correctly, but no row is highlighted.
Is the problem that this is a multipage userform, and when the re-populate subroutine is run, the other pages containing the listboxes aren't "active"?
Thanks for the help,
Joe
 
f64,

I am not familiar with SIMUL8. Are you creating your Userform & controls in this application? I am testing this issue using Excel.

After creating a Userform with a MultiPage control and adding a ListBox to one of the pages, I still cannot duplicate the problem. Regardless whether I put the code to select a specific item in a separate routine, a CommandButton event handler or the MultiPage Change event handler, I get the correct item highlighted in the ListBox.

Perhaps you can show the code that selects the ListBox entry.

Regards,
Mike
 
I have also tested, in Word. I made a UserForm, with a multi-page, picked up data, on close put the data into both bookmarks (hidden and visible) and formfields.

Then I set a docvariable that indicates whether there was data previous input from the userform. The userform checks for that, if it is true, it collects the data from the bookmarks and formfields, and repopulates the userform. This includes highlighting an item in the listbox. It works.

Now are you saying you want to highlight multiple items?

As Mike suggests, maybe you should post some code.

Gerry
 
Mike and Gerry,
Thanks for your feedback. After you indicated you had no problem highlighting the selection in a listbox, I took a closer look, and discovered that while I was indeed setting the listindex numbers on page 2, when leaving page 4 I was invoking a subroutine that cleared and then re-populated the listboxes (thus setting all the listindex #'s back to -1). I corrected the problem, and now the selected listbox row appears highlighted.
Thanks again!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top