Solved

Passing an object using Add event handler

Posted on 2010-09-09
3
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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