How to check if a ListView control has been clicked?
-
I have a Windows Form which uses a Timer to automatically close itself if no user interaction occurs within a given time. The form contains a Label, a Button and a ListView control. I basically want to run my UserHasClickedForm() sub if the user clicks anywhere in the form before the Timer ticks. I've used the Click event of each of the controls as shown below. The code works for each control except the ListView. If I click on the ListView control at run time, the click event does not get raised. Anyone know why this is, and what I could do to fix the problem?
Private Sub MyPopupDialog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
UserHasClickedForm()
End SubPrivate Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
UserHasClickedForm()
End SubPrivate Sub MyLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyLabel.Click
UserHasClickedForm()
End SubPrivate Sub MyListView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyListView.Click
UserHasClickedForm()
End Sub -
I have a Windows Form which uses a Timer to automatically close itself if no user interaction occurs within a given time. The form contains a Label, a Button and a ListView control. I basically want to run my UserHasClickedForm() sub if the user clicks anywhere in the form before the Timer ticks. I've used the Click event of each of the controls as shown below. The code works for each control except the ListView. If I click on the ListView control at run time, the click event does not get raised. Anyone know why this is, and what I could do to fix the problem?
Private Sub MyPopupDialog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
UserHasClickedForm()
End SubPrivate Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
UserHasClickedForm()
End SubPrivate Sub MyLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyLabel.Click
UserHasClickedForm()
End SubPrivate Sub MyListView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyListView.Click
UserHasClickedForm()
End SubDid you click on an item in the ListView or a blank area?? Try wiring up the ItemSelectionChanged event as well as the Click event.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Did you click on an item in the ListView or a blank area?? Try wiring up the ItemSelectionChanged event as well as the Click event.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Thanks for the response Dave, Using my code I've tried clicking on items in the ListView, blank area in the ListView control, Column Headers... Nothing seems to trigger a ListView_Click. Ultimately I want to flag that the user has clicked somewhere on the form regardless of whether it's on a blank part of the form, a control on the form like a Button or a ListView Item, a blank part of the ListView control or a Header in the ListView control. It seems to work as I would expect for all elements of the form except the ListView control, which has me stumped. Then again perhaps there's an easier way than coding the click event of each individual element of the form? Cheers Paul :-O I'm always humbled that complete strangers are so willing to share knowledge and help each other out on this site :-O
-
Thanks for the response Dave, Using my code I've tried clicking on items in the ListView, blank area in the ListView control, Column Headers... Nothing seems to trigger a ListView_Click. Ultimately I want to flag that the user has clicked somewhere on the form regardless of whether it's on a blank part of the form, a control on the form like a Button or a ListView Item, a blank part of the ListView control or a Header in the ListView control. It seems to work as I would expect for all elements of the form except the ListView control, which has me stumped. Then again perhaps there's an easier way than coding the click event of each individual element of the form? Cheers Paul :-O I'm always humbled that complete strangers are so willing to share knowledge and help each other out on this site :-O
Paul Hasler wrote:
It seems to work as I would expect for all elements of the form except the ListView control, which has me stumped. Then again perhaps there's an easier way than coding the click event of each individual element of the form?
There is no easier way to do this. If you click on a control, you're no longer clicking in the form. Like I said, wire up a different event for ListView.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
I have a Windows Form which uses a Timer to automatically close itself if no user interaction occurs within a given time. The form contains a Label, a Button and a ListView control. I basically want to run my UserHasClickedForm() sub if the user clicks anywhere in the form before the Timer ticks. I've used the Click event of each of the controls as shown below. The code works for each control except the ListView. If I click on the ListView control at run time, the click event does not get raised. Anyone know why this is, and what I could do to fix the problem?
Private Sub MyPopupDialog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
UserHasClickedForm()
End SubPrivate Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
UserHasClickedForm()
End SubPrivate Sub MyLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyLabel.Click
UserHasClickedForm()
End SubPrivate Sub MyListView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyListView.Click
UserHasClickedForm()
End Subyou might try some other ListView event, such as MouseClick or MouseDown. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.
-
you might try some other ListView event, such as MouseClick or MouseDown. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.
Damn! Forgot about those... :-\
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
I have a Windows Form which uses a Timer to automatically close itself if no user interaction occurs within a given time. The form contains a Label, a Button and a ListView control. I basically want to run my UserHasClickedForm() sub if the user clicks anywhere in the form before the Timer ticks. I've used the Click event of each of the controls as shown below. The code works for each control except the ListView. If I click on the ListView control at run time, the click event does not get raised. Anyone know why this is, and what I could do to fix the problem?
Private Sub MyPopupDialog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
UserHasClickedForm()
End SubPrivate Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
UserHasClickedForm()
End SubPrivate Sub MyLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyLabel.Click
UserHasClickedForm()
End SubPrivate Sub MyListView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyListView.Click
UserHasClickedForm()
End SubYou can also simplify your code by rolling up all the handlers onto 1 sub e.g.
Private Sub ControlsClicked() handles control1.click, control2.click, control3.click
UserHasClickedForm()
End SubEven if your controls do something in their own click events, both will fire. You could even remove the additional sub and attach the handlers directly onto UserHasClickedForm()
-
I have a Windows Form which uses a Timer to automatically close itself if no user interaction occurs within a given time. The form contains a Label, a Button and a ListView control. I basically want to run my UserHasClickedForm() sub if the user clicks anywhere in the form before the Timer ticks. I've used the Click event of each of the controls as shown below. The code works for each control except the ListView. If I click on the ListView control at run time, the click event does not get raised. Anyone know why this is, and what I could do to fix the problem?
Private Sub MyPopupDialog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
UserHasClickedForm()
End SubPrivate Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
UserHasClickedForm()
End SubPrivate Sub MyLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyLabel.Click
UserHasClickedForm()
End SubPrivate Sub MyListView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyListView.Click
UserHasClickedForm()
End SubThanks everyone for your help. Using Dave Kreskowiak's and Luc Pattyn's suggestions for looking at other ListView events, with daveauld's suggestion for simplifying the code, I've gone from my bloat-code which didn't work, to the following elegant-code which does.
Public Class MyForm
Private Sub UserHasClickedForm() Handles Me.Click, MyLabel.Click, MyListView.MouseDown, MyListView.ColumnClick, MyButton.Click
MyLabel.Text="Clicked"
End Sub
End Class- The MouseDown event is raised if you click in a blank area or Item of the ListView control, but not if you click on a column header if the View property of the ListView is set to Details. - The ColumnClick event is required to handle clicks on the column headers. Thanks again. Paul