Hightower Posted March 22, 2010 Posted March 22, 2010 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: Private Sub txtMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) [i]FUNCTIONS[/i] 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?
LosOjos Posted March 22, 2010 Posted March 22, 2010 Going off the top of my head here, but try this: Create your sub as normal, i.e. 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. 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) 1
mac_shinobi Posted March 22, 2010 Posted March 22, 2010 (edited) just going to try this myself am guessing its 2008 express ( vb.net ) that is?? Also am guessing its the bog standard tab control ? 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 ?? Edited March 22, 2010 by mac_shinobi
Hightower Posted March 23, 2010 Author Posted March 23, 2010 Going off the top of my head here, but try this: Create your sub as normal, i.e. 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. 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: 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?
Hightower Posted March 23, 2010 Author Posted March 23, 2010 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!
SteveAJones71 Posted March 24, 2010 Posted March 24, 2010 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!
LosOjos Posted March 24, 2010 Posted March 24, 2010 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
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now