How to capture all double clicks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff G
    New Member
    • Sep 2007
    • 10

    How to capture all double clicks

    Hi. I am very new to VB. Using VB 2005.

    I have worked for 25 years with procedural progamming, but never object or event driven.

    I have a form with a tab control and 10 tabs. Each tab has a tree. Each branch of the tree is a menu selection. The parameter for a generic subroutine is contained in the node tag.

    I would like to capture ANY double click from ANY tree with a single piece of code.
    Something like:

    Code:
    if event = double.click and control[1,4] = "tree" then
       msgbox(node.text)
       call program(node.tag)
    end if
    Thanks,
    Jeff
  • VBPhilly
    New Member
    • Aug 2007
    • 95

    #2
    Originally posted by Jeff G
    Hi. I am very new to VB. Using VB 2005.

    I have worked for 25 years with procedural progamming, but never object or event driven.

    I have a form with a tab control and 10 tabs. Each tab has a tree. Each branch of the tree is a menu selection. The parameter for a generic subroutine is contained in the node tag.

    I would like to capture ANY double click from ANY tree with a single piece of code.
    Something like:

    Code:
    if event = double.click and control[1,4] = "tree" then
       msgbox(node.text)
       call program(node.tag)
    end if
    Thanks,
    Jeff
    Events will be key to your solution.
    Double-click the control having the event you need to code,
    in code view, click the upper-right drop-down box to list the available events for the control.
    You might see "doubleclic k" event. Click it and you will now be able to code the event. In other words,code what happens when the double-click occurs.

    See where you get with that.

    Comment

    • Jeff G
      New Member
      • Sep 2007
      • 10

      #3
      Thanks for the quick reply, but thats not the part I have a problem with.

      I'll try to clarify what I meant.

      I have 10 treeViews with 20 nodes (or more) on each.
      Each tree is named "treeXXXX" where XXXX is different for each.

      I don't want to write the same event for each node.
      I would like to write just one event that responds to a double click on any tree on the form and returns the node tag & name that was clicked.

      Thanks again.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        What's really sad is that if you were using VB6 you could just create them as a control array, and it would work exactly the way you suggested - one routine for all of them.

        Don't think it's possible to simplify it to quite that degree in VB2005, though. MS do love to make great leaps backward in functionality, sometimes.

        As it is, I think you'll just have to code a routine for each. Keep in mind though, they could be just little stubs which all call the same piece of code somewhere.

        Comment

        • VBPhilly
          New Member
          • Aug 2007
          • 95

          #5
          Originally posted by Killer42
          What's really sad is that if you were using VB6 you could just create them as a control array, and it would work exactly the way you suggested - one routine for all of them.

          Don't think it's possible to simplify it to quite that degree in VB2005, though. MS do love to make great leaps backward in functionality, sometimes.

          As it is, I think you'll just have to code a routine for each. Keep in mind though, they could be just little stubs which all call the same piece of code somewhere.
          That is disappointing. Apparently, it is a huge disadvantage for vb6 folks used to control arrays. I did not know this until now.

          Found this article that might also help the OP:
          Last edited by VBPhilly; Sep 16 '07, 03:00 AM. Reason: added link

          Comment

          • Jeff G
            New Member
            • Sep 2007
            • 10

            #6
            Thanks for the 2 replys.
            I am not sure if I fully understand the control array issue but, the link got me in the right direction. From the article I got this:

            Code:
             
            Private Sub AnyTree_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick, TreeView2.DoubleClick, TreeView3.DoubleClick
            	Dim drQuit As DialogResult 
            	drQuit = MessageBox.Show(TreeView1.SelectedNode.Tag, TreeView1.SelectedNode.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
            End Sub
            I originally did not understand the "handles" clause but the example helped alot and was really what I needed.

            This got me to where it works from any tree, but now I have the small problem of the data returned. My test returns a message box for any tree, but the correct info only for TreeView1. What is needed to return properties of the node selected?

            I will likely use this same code in a few places, so the second question is can it return more info, such as object name that was selected? My thought is a generic right-click routine for an entire form.

            I really appreciate the quick responses from the forum!!!

            Thanks,
            Jeff

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by VBPhilly
              That is disappointing. Apparently, it is a huge disadvantage for vb6 folks used to control arrays.
              Oh, it is.

              I haven't had a chance to check out the link yet, but I know there are ways to simulate controls arrays. They're not as good as the real thing, but what can one do...

              Comment

              • Jeff G
                New Member
                • Sep 2007
                • 10

                #8
                OK. I've gotten almost all of it figured out. (It only took the repsonses from this forum, about 3 hours of Google searches and 100 websites!)

                Just one more question on this subject.

                Here is my sample code.

                Code:
                 
                 
                Private Sub AnyTree_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick, TreeView2.DoubleClick, TreeView3.DoubleClick
                	 Dim b As TreeView = CType(sender, TreeView) 
                	 MessageBox.Show(b.SelectedNode.Tag, b.SelectedNode.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                End Sub
                The only part I'm not happy with is having to list the multiple events after the "handles" on line 1.
                Is there a way to set something like "Handles any.DoubleClick "?
                I've tried using my. me. myBase. and I think a few others.

                Comment

                • VBPhilly
                  New Member
                  • Aug 2007
                  • 95

                  #9
                  Originally posted by Jeff G
                  OK. I've gotten almost all of it figured out. (It only took the repsonses from this forum, about 3 hours of Google searches and 100 websites!)

                  Just one more question on this subject.

                  Here is my sample code.

                  Code:
                   
                   
                  Private Sub AnyTree_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick, TreeView2.DoubleClick, TreeView3.DoubleClick
                  	 Dim b As TreeView = CType(sender, TreeView) 
                  	 MessageBox.Show(b.SelectedNode.Tag, b.SelectedNode.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                  End Sub
                  The only part I'm not happy with is having to list the multiple events after the "handles" on line 1.
                  Is there a way to set something like "Handles any.DoubleClick "?
                  I've tried using my. me. myBase. and I think a few others.
                  Not sure if this is what you need but check out AddHandler, which allows you to specify a handler without having a "Handles <object.event >" syntax after the event declaration:
                  Reference: http://msdn2.microsoft.com/en-us/lib...93(VS.71).aspx

                  Comment

                  • Jeff G
                    New Member
                    • Sep 2007
                    • 10

                    #10
                    Everything I've read says AddHandler is the answer, BUT I still can not get it to work. Here is what I have so far:

                    Code:
                     
                    Private Sub frmMainMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                    	 Dim ctrl As Control 
                    	 For Each ctrl In Me.Controls
                    		 AddHandler ctrl.DoubleClick, AddressOf AnyTree_DoubleClick
                    	 Next
                    End Sub
                     
                    Public Sub AnyTree_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
                    	 Dim b As TreeView = CType(sender, TreeView) 
                    	 MessageBox.Show(b.SelectedNode.Tag & " " & b.Name, b.SelectedNode.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                    End Sub
                    I assume I am either putting the AddHandler code in the wrong place or I need to do something to make it available throuhout the program.
                    Thanks.

                    Comment

                    • Jeff G
                      New Member
                      • Sep 2007
                      • 10

                      #11
                      Got it!

                      The problem was needing to loop through all controls at every level.

                      Thanks for all the help.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by Jeff G
                        Got it!
                        Thanks for posting the answer. This will be helpful for anyone else who comes looking for a similar solution.

                        Comment

                        Working...