How to override listbox click?

L

Larry Serflaten

I'm drawing my own inherited listbox, and I don't want the user
to select any items, but I do want to allow them to scroll the listbox.

When they click on an item, there is a flicker of the previously
selected item and the new selected item. Even though I don't
show anything selected, the default behaviour wants to redraw
those items.

What can I do to stop the user click from selecting an item in
the list?

LFS
 
R

Rob Teixeira [MVP]

Larry,
Are you using the OwnderDraw overrides? (OnItemDraw, for example)
If so, are you painting the background rectangle before drawing the text?
If you give me a few details, I can give you a better answer.

-Rob Teixeira [MVP]
 
G

Guest

This will prevent all LButton Clicks

Private Const WM_LBUTTONDOWN As Integer = &H20

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message
If m.Msg = WM_LBUTTONDOWN Then Exit Su
MyBase.WndProc(m
End Sub
 
L

Larry Serflaten

Rob Teixeira said:
Larry,
Are you using the OwnderDraw overrides? (OnItemDraw, for example)
If so, are you painting the background rectangle before drawing the text?
If you give me a few details, I can give you a better answer.

Yes, I've set the DrawMode to DrawMode.OwnerDrawVariable

I am overriding OnMeasureItem and OnDrawItem. In the OnDrawItem
I draw my own background as well as the stuff I want listed. When I add
an item, the control seems to erase everything in the list, and then loop
through the items to get them drawn. It would be nice to avoid that
invalidate process and just go ahead with the painting.


Also, when the user clicks on one of the items in the list, then that one,
and the one that was previously selected get erased, and called to be
redrawn. That is causing flicker for every click that changes the selected
item. There is no difference between a selected item, and the others, its
just that the listbox is keeping track of the ListIndex and erasing the ones
that would be changing. I would like to make that list non-clickable, but
I do want to allow them to scroll the list.

I was thinking I need to stop the click from getting through. I tried
setting SelectionMode to None, but the listbox still keeps track of the
ListIndex and acts as before. I tried overriding OnPaintBackground,
but that didn't get it....

In simple terms it is invalidating sections that I don't want invalidated.
Really all I want to allow them to use is the scroll bar.

LFS
 
L

Larry Serflaten

Whatshisname said:
This will prevent all LButton Clicks.

Private Const WM_LBUTTONDOWN As Integer = &H201

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_LBUTTONDOWN Then Exit Sub
MyBase.WndProc(m)
End Sub


Thanks, that did work for stopping user clicks. I am still wondering
how to stop the control from invalidating (painting over) sections of
the control.

LFS
 
G

Guest

I'm not sure if this will make it better but it's worth a shot

Private Const WM_LBUTTONDOWN As Integer = &H20
Private Const WM_ERASEBKGND As Integer = &H1

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message
If m.Msg = WM_LBUTTONDOWN Or m.Msg = WM_ERASEBKGND Then Exit Su
MyBase.WndProc(m
End Su
 
H

Herfried K. Wagner [MVP]

* "Larry Serflaten said:
I'm drawing my own inherited listbox, and I don't want the user
to select any items, but I do want to allow them to scroll the listbox.

When they click on an item, there is a flicker of the previously
selected item and the new selected item. Even though I don't
show anything selected, the default behaviour wants to redraw
those items.

What can I do to stop the user click from selecting an item in
the list?

<URL:http://dotnet.mvps.org/dotnet/samples/controls/downloads/LockListBox.zip>
 
L

Larry Serflaten

Whatshisname said:
I'm not sure if this will make it better but it's worth a shot.

Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_ERASEBKGND As Integer = &H14

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_LBUTTONDOWN Or m.Msg = WM_ERASEBKGND Then Exit Sub
MyBase.WndProc(m)
End Sub

That masks the symptoms, and I'll use it until something better comes along, but
I'd prefer to stop it at its source. IE, I'd rather the message would never get sent....

Thanks!
LFS
 
G

Guest

Personally, I would rather write my own listbox rather than trying to muscle Microsofts listbox. That way, I have total control of what it's doing and why it's doing it. It wouldn't take that much code to write. Draw a border, add an array list, then add a scrollbar and use the API ScrollWindow to scroll the items up and down. Piece of cake. But then thats all I do, write controls.
 
A

AlexS

I am not sure that recreating old functionality is productive way - except
when learning to program. So, I have question - why KeyDown and KepPressed
events are not suitable?

HTH
Alex

Whatshisname said:
Personally, I would rather write my own listbox rather than trying to
muscle Microsofts listbox. That way, I have total control of what it's doing
and why it's doing it. It wouldn't take that much code to write. Draw a
border, add an array list, then add a scrollbar and use the API ScrollWindow
to scroll the items up and down. Piece of cake. But then thats all I do,
write controls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top