Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to check if a ListView control has been clicked?

How to check if a ListView control has been clicked?

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Hasler
    wrote on last edited by
    #1

    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 Sub

    Private Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
    UserHasClickedForm()
    End Sub

    Private Sub MyLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyLabel.Click
    UserHasClickedForm()
    End Sub

    Private Sub MyListView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyListView.Click
    UserHasClickedForm()
    End Sub

    D L D P 4 Replies Last reply
    0
    • P Paul Hasler

      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 Sub

      Private Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
      UserHasClickedForm()
      End Sub

      Private Sub MyLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyLabel.Click
      UserHasClickedForm()
      End Sub

      Private Sub MyListView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyListView.Click
      UserHasClickedForm()
      End Sub

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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...

      P 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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...

        P Offline
        P Offline
        Paul Hasler
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • P Paul Hasler

          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

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          • P Paul Hasler

            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 Sub

            Private Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
            UserHasClickedForm()
            End Sub

            Private Sub MyLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyLabel.Click
            UserHasClickedForm()
            End Sub

            Private Sub MyListView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyListView.Click
            UserHasClickedForm()
            End Sub

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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.


            D 1 Reply Last reply
            0
            • L Luc Pattyn

              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.


              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              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...

              1 Reply Last reply
              0
              • P Paul Hasler

                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 Sub

                Private Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
                UserHasClickedForm()
                End Sub

                Private Sub MyLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyLabel.Click
                UserHasClickedForm()
                End Sub

                Private Sub MyListView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyListView.Click
                UserHasClickedForm()
                End Sub

                D Offline
                D Offline
                DaveAuld
                wrote on last edited by
                #7

                You 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 Sub

                Even 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()

                Dave Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo

                1 Reply Last reply
                0
                • P Paul Hasler

                  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 Sub

                  Private Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
                  UserHasClickedForm()
                  End Sub

                  Private Sub MyLabel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyLabel.Click
                  UserHasClickedForm()
                  End Sub

                  Private Sub MyListView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyListView.Click
                  UserHasClickedForm()
                  End Sub

                  P Offline
                  P Offline
                  Paul Hasler
                  wrote on last edited by
                  #8

                  Thanks 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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups