[2005] Double click in empty ListView?
Hello everyone,
I have a ListView control in Details view with columns and I want a file dialog to show when the user double clicks an empty part of the control. Problem is, the events DoubleClick and MouseDoubleClick are only triggered when an item is double clicked.
So, is there an event that gets triggered when an empty part of the ListView control is (double) clicked?
Thanks for any help,
Alexander.
Re: [2005] Double click in empty ListView?
I don’t know of any event but it can be done by using apis. I have a class called GlobalMouseHook in my signature that you can use the double click event and determine if the click location is with in the listView control and then process your code.
Re: [2005] Double click in empty ListView?
Once in a while I come across a post that I just want to figure out for the sake of doing it. Even if I never need to use the code myself, it just helps to fortify my knowledge.
So I took a crack at this, and after a little digging, it turns out it was pretty simple to implement. You have to use an inherited listview class to accomplish this.
VB Code:
'EXTENDED LISTVIEW TO PROVIDE DOUBLE CLICK NOTIFICATION WHEN
'DOUBLE CLICKING A BLANK AREA OF A LISTVIEW
Public Class ListViewEx
Inherits ListView
'EVENT THAT WILL FIRE WHEN THE BLANK AREA OF THE LISTVIEW IS DOUBLE CLICKED
Public Event DoubleClickBlankArea(ByVal sender As System.Object, ByVal e As System.EventArgs)
'OVERRIDE THE WINPROC ROUTINE, SO WE CAN LOOK FOR SPECIFIC WINDOWS MESSAGES
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
'WE ARE NOT SUPPRESSING ANY MESSAGES, SO ITS FINE TO SEND
'ALL MESSAGES TO THE BASE CLASS FOR PROCESSING
MyBase.WndProc(m)
'SINCE WE ONLY WANT THE NEW EVENT TO FIRE WHEN ITS
'IN THE BLANK AREA, CHECK 2 THINGS
'1) THAT NO ITEMS ARE SELECTED IN THE LISTVIEW
'2) THAT THE WINDOWS MESSAGE IS 515 (AKA &H203 or WM_LBUTTONDBCLK)
If Me.SelectedItems.Count = 0 And m.Msg = 515 Then
'RAISE THE EVENT UP TO THE CONTROL ON YOUR FORM
RaiseEvent DoubleClickBlankArea(Me, New EventArgs)
End If
End Sub
End Class
VB Code:
'USAGE
'NORMAL DOUBLECLICK THAT FIRES WHEN DOUBLE CLICKING AN ITEM
Private Sub ListViewEx1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListViewEx1.DoubleClick
MessageBox.Show("Item Double Clicked")
End Sub
'NEW EVENT THAT FIRES WHEN DOUBLE CLICKING THE BLANK AREA OF LISTVIEW
Private Sub ListViewEx1_DoubleClickBlankArea(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListViewEx1.DoubleClickBlankArea
MessageBox.Show("Blank Area of " & DirectCast(sender, Control).Name & " Double Clicked")
End Sub
It worked for the scenarios I tested (which were details view)
Let me know how it works out for you.
Re: [2005] Double click in empty ListView?
Very sleek.
Nice job, Kleinma.... :thumb:
Re: [2005] Double click in empty ListView?
Sounds perfect! :)
But how do I implement this new event into a ListView control?
Re: [2005] Double click in empty ListView?
all you do is add a new class to your project and call it ListViewEx
copy the code from my post above into that and compile your project.
(this will put the ListViewEx control into your toolbox so you can add it to forms)
Then go back to your form, and replace your regular listview, with a ListViewEx control and you are all set.
Re: [2005] Double click in empty ListView?
Strange...
I have created the class, compiled the project and got the control in my toolbox, but when I try to add it to the form, Visual Studio terminates instantly...
Re: [2005] Double click in empty ListView?
I will create a demo project and upload it for you to see if that works for you.
1 Attachment(s)
Re: [2005] Double click in empty ListView?
here you go.
make sure when you open the sln file, the first thing you do is rebuild the project.
Re: [2005] Double click in empty ListView?
This is proving to be harder than it should be...
Now I get an error when compiling the project. The line:
VB Code:
If Me.SelectedItems.Count = 0 And m.Msg = 515 Then
Gives a StackOverflowException, with no error data available. :ehh:
Re: [2005] Double click in empty ListView?
that is really odd.
I know you are running .NET 2005, but what is the exact version? Did you install the VS.NET 2005 Service Pack 1?
I ran this code on my machine with no errors at all.
maybe a 3rd person could try it and see if it works for them. Then we can isolate the issue to something on your PC.
Re: [2005] Double click in empty ListView?
I'm running Visual Studio 2005 Professional. (Exact version number: 8.0.50727.42)
I haven't installed Service Pack 1, though. I will install it and try again.
Re: [2005] Double click in empty ListView?
I am running 8.0.50727.762 (SP.050727-7600)
I am also running WinXP SP2