We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Need help passing value using AddHandler

Medium Priority
Last Modified: 2013-11-07

Using VB.net 2005 SP1 Compact Framework 2.0

I have a sub that is called as follows:

AddHandler tabAttBut.Click, AddressOf AutoButton. This is located on every tab page of the tab 'tabProj' located on Form7

From that I have the following code:

    Public Sub AutoButton(ByVal sender As Object, ByVal e As EventArgs)
        Dim strBut As String
        Dim ctrlName As String
        ctrlName = DirectCast(sender, Control).Name()
        strBut = ctrlName.Substring(4, ctrlName.Length - 4)
    End Sub

I need to also have the value of  tabPage.text value passed to this module as well.

How do I do that?


Watch Question

Assuming the handler is also part of the tab page (seems to be from your posted code) and that tabPage.text is a member of the tab page, you should be able to access it directly from the handler.

What exactly is tabPage.text?


sorry tabPage.text was supposed to represent the text of the Tab Page from which ctrlName comes from.

It could be any tab page within the tab called tabProj on Form7. I do not know how to pass both so was hoping that someone could show me hot to do this by example.


Isn't your event handler on the tab page?  You should be able to do


directly from your handler.  Have you tried it?  The instance of AutoButton should be different each time you do AddHandler because it is on a different page.

If you have only a single handler, which given your code it doesn't seem like that is the case, you can cast the 'sender' parameter to a Control and get its Parent property which should be the tab page containing the button whose event is being handled.  Cast the parent to your tab page class and you can access the text property.


Actually the AutoButton sub is located in a module external to the tab and being rather new to this I do not know how to do what you are describing.
Then the easiest thing would be to put the AutoButton sub on the tab page itself and then call the external module from the button handler.  That way you have direct access to the text value and you can pass it out to the external module via the button handler.  There really isn't a way to pass the text into the button handler through the event.  This is easiest because the handler is in the tab page and you have direct access to all components of the page.

The alternative I mentioned previously is to to something like (in AutoButton as you currently have it):

        Dim ctl As Control = CType(sender, Control) ' This is the button that was clicked
        Dim tbx As TextBox = CType(ctl.Parent.Controls("TextBox1"), TextBox) ' Find the textbox control that has the text
        tbx.Text ' This is the text value you want

This assumes that on each tab page there is a textbox with a common name that has the text you want.  Because there is only one handler handling the buttons from all tab pages, this is a bit more complex because you need to give the important controls the same name in all the pages or you need to be able to select based on the type of ctl.Parent.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thanks for all of your help. Much appreciated. Although I did not use your idea it did inspire me to come up with my solution. I simply added the text I needed to the tag property of the button and called it as follows:

strTag = DirectCast(sender, Control).Tag()

And it worke fine. Again thank you.

That works, too!  Good idea.


Yeah I am still learning VB.Net on the fly. Very powerful yet I get overwhelmed at times with it...having come from an Access and vba background.

Thanks again

If you have time, you might still want to play around with my suggestions (during 'play' time) as they may be useful sometime later if you need more access to the originating form than the single piece of data.

The two basic ideas are:

1) In the Module where you currently have your handler, replace the button handler with a method that takes the desired string as a parameter.  The put the handler method on your tab pages and register each button with the handler from that specific page.

2) This is just an extension of what you just did.  After casting the sender to a control, you can access the Parent control, which will be the tab page form.  Then you can access the controls through Parent.Controls("controlName"), where controlName is the name of the control you want.  Better yet, derive your tab pages from some base form and cast Parent to that base class.  In that base form, you can put methods to extract the information you need.

Both of these are actually quite simple and will let you explore .NET capability and may come in handy if your problem is more complex than this turned out to be.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.