Coding Thread, vb.net and public subs in Coding and Web Development; I have a tabbed area (unlimited amount of tabs, just keeping adding new if needed - so it's completely dynamic) ...
-
22nd March 2010, 04:49 PM #1 vb.net and public subs
I have a tabbed area (unlimited amount of tabs, just keeping adding new if needed - so it's completely dynamic) in my VB.net 2008 project, and each tabbed area has a text area.
I need to create a sub that watches for a mouse click/key press on this text area and then run some other functions.
With just one text area it's a doddle:
Code:
Private Sub txtMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
FUNCTIONS
End Sub
Thing is, my txtMain's are kept in an array, so if there are 3 tabs open, I will have:
txtMain(0)
txtMain(1)
txtMain(2)
How do I create something that can listen for mouse clicks/key presses on EVERY tab that is created in the application?
Any ideas?
-
-
IDG Tech News
-
22nd March 2010, 05:00 PM #2 Going off the top of my head here, but try this:
Create your sub as normal, i.e.
Code:
Private Sub txtMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
FUNCTIONS
End Sub
When you create your dynamic text box, add a line right after you created it to attach that event to it, i.e.
Code:
AddHandler txtMain(x).KeyUp, AddressOf txtMain_KeyUp
now your txtMain_KeyUp event is triggered whenever one of your text boxes triggers that event.
Not tried it so no guarantees on that code, but I think that's the line you need to go down (try it anyway, can't hurt)
-
Thanks to LosOjos from:
Hightower (23rd March 2010)
-
22nd March 2010, 05:40 PM #3 just going to try this myself am guessing its 2008 express ( vb.net ) that is??
Also am guessing its the bog standard tab control ?
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tbOne.TabPages.Add("Tab One")
tbOne.TabPages.Add("Tab Two")
tbOne.TabPages.Add("Tab Three")
End Sub
Private Sub tbOne_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbOne.MouseClick
tbOne.SelectedTab.Text = tbOne.TabPages.IndexOf(tbOne.SelectedTab)
End Sub not sure if thats what you are after ??
Last edited by mac_shinobi; 22nd March 2010 at 06:47 PM.
-
-
23rd March 2010, 08:34 AM #4 
Originally Posted by
JoshJohnson
Going off the top of my head here, but try this:
Create your sub as normal, i.e.
Code:
Private Sub txtMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
FUNCTIONS
End Sub
When you create your dynamic text box, add a line right after you created it to attach that event to it, i.e.
Code:
AddHandler txtMain(x).KeyUp, AddressOf txtMain_KeyUp
now your txtMain_KeyUp event is triggered whenever one of your text boxes triggers that event.
Not tried it so no guarantees on that code, but I think that's the line you need to go down (try it anyway, can't hurt)
I've just tried that so my code now reads:
Code:
Dim txtMain(iNumTabs) As TextBox
AddHandler txtMain(iNumTabs).KeyUp, AddressOf txtMain_KeyUp in the general declarations section - but I'm getting a syantax error in the error list window. The AddHandler word is the one with the blue squiggle beneath it, but no additional help given so not sure what's going on.
Had a Google around and other peoples syntax match this syntax so not sure what the issue is. Any ideas?
-
-
23rd March 2010, 08:38 AM #5 In fact scratch that last one - shouldn't have put that code in general declarations. Moved it to after the part where I dynamically add the tab page (as you said when I finally read it properly) and it works a treat.
Thank you!
-
-
23rd March 2010, 12:42 PM #6 Always glad to be of service
-
-
24th March 2010, 09:30 AM #7
- Rep Power
- 0
Another way to create a handler dynamically for each control you create, for example:
AddHandler [your control].click, AddressOf Sub_do_my_click
But as I'm sure you've guessed there are many ways to skin a cat!
-
-
24th March 2010, 09:32 AM #8 
Originally Posted by
SteveAJones71
Another way to create a handler dynamically for each control you create, for example:
AddHandler [your control].click, AddressOf Sub_do_my_click
But as I'm sure you've guessed there are many ways to skin a cat!
That's what he did
-
SHARE: 
Similar Threads
-
By maplay in forum MIS Systems
Replies: 23
Last Post: 3rd October 2009, 05:44 PM
-
By SimpleSi in forum *nix
Replies: 6
Last Post: 8th May 2009, 12:27 PM
-
By karldenton in forum EduGeek Joomla 1.5 Package
Replies: 4
Last Post: 7th May 2009, 09:13 AM
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules