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.
No matter how fool-proof your program is, there will always be a better fool.
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.
Rating is a way of saying thank you. Don't forget to rate always!
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)
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.
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...
No matter how fool-proof your program is, there will always be a better fool.
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.
Last edited by arsmakman; Jan 25th, 2007 at 04:36 PM.
No matter how fool-proof your program is, there will always be a better fool.