Detect scroll in a ListView

  • Thread starter Thread starter roidy
  • Start date Start date
roidy said:
How do I detect when a user scrolls a ListView control?

Thanks
Rob

You need to create a usercontrol derived from the listview. In this control
you will override the WndProc and raise an event when the WM_VSCROLL is
sent.

Follow these steps:

1. Add a new usercontrol and call it something like MyListView
2. In the MyListView.Designer.vb file change the inherits clause to:
Inherits System.Windows.Forms.ListView
As soon as you have done that you will see errors in the
InitializeComponent sub. Just comment or delete those two lines

3. In the code file for MyListView use the following code:

Public Class MyListView
Public Event ProcMsg(ByVal m As Message)
Public Const WM_VSCROLL As Integer = 277

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case m.Msg
Case WM_VSCROLL
RaiseEvent ProcMsg(m)
End Select
MyBase.WndProc(m)
End Sub
End Class

4. Build your project , this will add the MyListView control to the
toolbox.

5. Now add a MyListView control to your form.

6. Add code to handle the ProcMsg event. Since you didn't say what you
were going to do when this occurred this is where I stop.

Hope this helps
LS
 
Thanks Lloyd,

Basically my application displays a listview with 2 columns, when the user
selects a row the second column has a combobox overlaid on top of it so the
user can make a selection from it. Now if the user scrolls the listview I
need to remove the combobox because it will no longer lineup with the
selected row.

I`m at step 6 in your explanation all I need to do is detect when the scroll
bar is moved. I just dont know how to handle the ProcMsg event.

Again thanks for the easy to follow explanation.

Rob
 
roidy said:
Thanks Lloyd,

Basically my application displays a listview with 2 columns, when the user
selects a row the second column has a combobox overlaid on top of it so
the user can make a selection from it. Now if the user scrolls the
listview I need to remove the combobox because it will no longer lineup
with the selected row.

I`m at step 6 in your explanation all I need to do is detect when the
scroll bar is moved. I just dont know how to handle the ProcMsg event.

Again thanks for the easy to follow explanation.

Rob

Are you saying you cannot capture the ProcMsg event. Once you have built
the project once the ProcMsg event will show in the Properties / Events
window so you can attach an event handler to it. Sample code that I used to
make sure this works is (the code in the sub was just there to confirm the
message was being captured):

Private Sub MyListView1_ProcMsg(ByVal m As System.Windows.Forms.Message)
Handles MyListView1.ProcMsg

This is where you would most likely set the combobox visible to false.

Try
Dim li As New ListViewItem("Scrolling " + m.Msg.ToString)
Me.ListView1.Items.Add(li)
li.EnsureVisible()
Catch ex As Exception
End Try
End Sub
 
Thanks Lloyd,

I didn`t realize that it had added a new ProcMsg event handler to the
MyListBox properties so your right it`s as easy as adding:-


Private Sub MyListView1_ProcMsg(ByVal m As System.Windows.Forms.Message)
Handles MyListView1.ProcMsg
ComboBox1.Hide()
End Sub

Thanks again for the help
Rob
 
Back
Top