By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

How to capture all double clicks

P: 10
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:

Expand|Select|Wrap|Line Numbers
  1. if event = double.click and control[1,4] = "tree" then
  2.    msgbox(node.text)
  3.    call program(node.tag)
  4. end if
Thanks,
Jeff
Sep 15 '07 #1
Share this Question
Share on Google+
11 Replies


VBPhilly
P: 95
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:

Expand|Select|Wrap|Line Numbers
  1. if event = double.click and control[1,4] = "tree" then
  2.    msgbox(node.text)
  3.    call program(node.tag)
  4. 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 "doubleclick" 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.
Sep 15 '07 #2

P: 10
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.
Sep 15 '07 #3

Expert 5K+
P: 8,434
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.
Sep 15 '07 #4

VBPhilly
P: 95
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:
http://visualbasic.about.com/od/usin...ctrlarraya.htm
Sep 16 '07 #5

P: 10
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub AnyTree_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick, TreeView2.DoubleClick, TreeView3.DoubleClick
  3.     Dim drQuit As DialogResult 
  4.     drQuit = MessageBox.Show(TreeView1.SelectedNode.Tag, TreeView1.SelectedNode.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
  5. End Sub
  6.  
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
Sep 16 '07 #6

Expert 5K+
P: 8,434
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...
Sep 16 '07 #7

P: 10
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Private Sub AnyTree_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick, TreeView2.DoubleClick, TreeView3.DoubleClick
  4.      Dim b As TreeView = CType(sender, TreeView) 
  5.      MessageBox.Show(b.SelectedNode.Tag, b.SelectedNode.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
  6. End Sub
  7.  
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.
Sep 16 '07 #8

VBPhilly
P: 95
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Private Sub AnyTree_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick, TreeView2.DoubleClick, TreeView3.DoubleClick
  4.      Dim b As TreeView = CType(sender, TreeView) 
  5.      MessageBox.Show(b.SelectedNode.Tag, b.SelectedNode.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
  6. End Sub
  7.  
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
Sep 17 '07 #9

P: 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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub frmMainMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3.      Dim ctrl As Control 
  4.      For Each ctrl In Me.Controls
  5.          AddHandler ctrl.DoubleClick, AddressOf AnyTree_DoubleClick
  6.      Next
  7. End Sub
  8.  
  9. Public Sub AnyTree_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
  10.      Dim b As TreeView = CType(sender, TreeView) 
  11.      MessageBox.Show(b.SelectedNode.Tag & " " & b.Name, b.SelectedNode.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
  12. End Sub
  13.  
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.
Sep 18 '07 #10

P: 10
Got it!

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

Thanks for all the help.
Sep 21 '07 #11

Expert 5K+
P: 8,434
Got it!
Thanks for posting the answer. This will be helpful for anyone else who comes looking for a similar solution.
Sep 21 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.