Solved

Passing an object using Add event handler

Posted on 2010-09-09
3
330 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now