+ Post New Thread
Results 1 to 8 of 8
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) ...
  1. #1

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242

    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?

  2. #2

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,771
    Thank Post
    1,511
    Thanked 1,294 Times in 884 Posts
    Rep Power
    813
    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)

  3. Thanks to LosOjos from:

    Hightower (23rd March 2010)

  4. #3

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    10,052
    Thank Post
    3,583
    Thanked 1,123 Times in 1,025 Posts
    Rep Power
    377
    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 07:47 PM.

  5. #4

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    Quote Originally Posted by JoshJohnson View Post
    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?

  6. #5

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    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!

  7. #6

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,771
    Thank Post
    1,511
    Thanked 1,294 Times in 884 Posts
    Rep Power
    813
    Always glad to be of service

  8. #7

    Join Date
    May 2009
    Location
    Towyn
    Posts
    6
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    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!

  9. #8

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,771
    Thank Post
    1,511
    Thanked 1,294 Times in 884 Posts
    Rep Power
    813
    Quote Originally Posted by SteveAJones71 View Post
    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:
+ Post New Thread

Similar Threads

  1. Replies: 23
    Last Post: 3rd October 2009, 06:44 PM
  2. copying whole diretory and subs
    By SimpleSi in forum *nix
    Replies: 6
    Last Post: 8th May 2009, 01:27 PM
  3. Public Module
    By karldenton in forum EduGeek Joomla 1.5 Package
    Replies: 4
    Last Post: 7th May 2009, 10: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
  •