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

How to disable a listbox?

Status
Not open for further replies.

RocAFella2007

Technical User
Mar 21, 2007
45
GB
Hi, could somebody offer me a little help please? What I need is basically a listbox which is read only and NO item can be selected in that listbox as I have Up and Down Buttons. Some would think that the above is easy why dont I just make it read only and for the moment I have made it readonly by using the following coding:

Me.Listbox1.Enabled = False
Me.Listbox1.SelectedIndex = 0

Everything is working great apart from one problem, the text in the readonly listbox appears in gray and dull which I dont want. Is there a way making the text appear a different colour? Or even better another way around disabling selecting an item from the listbox without making it read only?

All suggestions welcome. Thanks
 
Try setting the Me.Listbox1.Enabled back to True

then,

Put Me.Listbox1.SelectedIndex = 0 to the following events
Listbox1.KeyDown
Listbox1.KeyUp
Listbox1.MouseDown
Listbox1.MouseUp

Also, set Listbox1.TabStop = False

This should show the listbox as working but when the user tries click/type, it shouldn't do anything...
 
As a work around place your listbox in a panel an set it
Locked =True
then
Code:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Me.ListBox1.SelectedIndex = -1
        Me.Panel1.Focus()
    End Sub
clear selection and remove the focus from the listbox.
hope this helps

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Hi CJelec and Abdulla, Cjelec your method works great apart from when the user clicks on the listbox the first item gets selected. Abdulla im facing some problem with your method.

Thanks both for your suggestions, however CJelec could I try something else when the user clicks on the listbox?
 
Cjelec I Have fixed the problem, im happy with selecting the first item thanks alot buddy youve helped me loads.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top