[RESOLVED] listbox - selectionmode = none issues
Hello,
I would like to have a listbox where I can pre-select an item in code, but prevent the user from selecting a different item (i.e. a "read-only" listbox). I don't want to disable the listbox, because then the user can't scroll it. If I set selectionmode = none, then I can't select an item in code. Any ideas on how I can achieve the results I am looking for here?
Thanks,
Bruce
Re: listbox - selectionmode = none issues
try this. pre-select your item , then set selectionmode in code
Re: listbox - selectionmode = none issues
Thanks, .paul., but unfortunately that does not work. What happens is, when I set selectionmode = none in code, the item that I previously selected in code is de-selected.
Re: listbox - selectionmode = none issues
ok. this works. its an extended listbox that can only have its selectedindex changed in code:
vb Code:
Public Class listboxEx
Inherits ListBox
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_LBUTTONUP As Integer = &H202
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_LBUTTONDOWN OrElse m.Msg = WM_LBUTTONUP OrElse m.Msg = WM_KEYDOWN OrElse m.Msg = WM_KEYUP Then Return
MyBase.WndProc(m)
End Sub
End Class
Re: listbox - selectionmode = none issues
You could just add a selection code on the index changed event
Code:
ListBox1.SelectedIndex = 1
So every time they change it, it instantly changes back.
Re: listbox - selectionmode = none issues
.paul.,
Very nice, I must say. I tried it out and it works great. I see you added it to your bag of tricks in your signature, as well you should. I think this little gem fills a need. Thanks very much!
Vectris - thanks for your input. I did the kind of thing you are talking about in VB6, but was hoping for a more elegant solution in .NET. As we can see, .NET still does not offer the desired functionality "out of the box", but .paul.'s extension gets the job done nicely.