Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing an object using Add event handler

Posted on 2010-09-09
3
Medium Priority
?
343 Views
Last Modified: 2012-05-10
I have a form were there a number of textboxes which are created on a form dynamically, the number of which varies according to initial parameters.

Due to the nature of data that the user has to input the text must be formatted to a set format on the 'textbox.leave' event

the attached code works perfectly for this textbox as there is no requirement for information from the textbox that raised the event. Is there some way to use a method like the 'Directcast()' method to obtain properties from the textbox which triggered the event?

Thanks,
Thomas

'' This code in in the form.load eventArg after the code that creates the synamic textboxes

        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is TextBox Then
                AddHandler ctl.Leave, AddressOf tbleave
            End If
        Next

    End Sub

    Sub tbleave()
        MessageBox.Show("The user has left this textbox")
    End Sub

Open in new window

0
Comment
Question by:_Tommy_b
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 33640650
Just change the signature of your handler to the following then you can access the corresponding control via "sender":
Private Sub tbleave(ByVal sender As System.Object, ByVal e As System.EventArgs)

Open in new window

0
 

Author Comment

by:_Tommy_b
ID: 33640721
Cant belive how simple that was... some time you look for something complicated when infact its so simple.

Thanks Carl :)
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33641073
To be honest I'm a little surprised the complier didn't pick up on it, the C# compiler would have complained about the method not matching the expected signature.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question