• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 625
  • Last Modified:

Need help passing value using AddHandler

Hello

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
        Beep()
        Dim ctrlName As String
        ctrlName = DirectCast(sender, Control).Name()
        strBut = ctrlName.Substring(4, ctrlName.Length - 4)
        MsgBox(strBut)
    End Sub

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

How do I do that?

Thanks

PBLack
0
PBLack
Asked:
PBLack
  • 5
  • 4
1 Solution
 
drichardsCommented:
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?
0
 
PBLackAuthor Commented:
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.

Thanks

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

    tabPage.text

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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PBLackAuthor Commented:
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.
0
 
drichardsCommented:
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.
0
 
PBLackAuthor Commented:
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.

PBLack
0
 
drichardsCommented:
That works, too!  Good idea.
0
 
PBLackAuthor Commented:
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

PBLAck
0
 
drichardsCommented:
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now