getting the ID of the current server control

T

TB

Hi All:

What would be the right way to get the ID of a server control inside an
event for that same server control?

For example:

I have this textbox called textbox1 which the following event code attached:

Private Sub textbox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handlestextbox1.TextChanged
if textbox1.Text.ToString() = "This is it" then
' something happens
end if
End Sub

Now, since I have lot of these textboxes with similar events, I would like
to make a generic code line (maybe for a sub), replacing "textbox1.Text"
with something like (pseudocode) "CurrentControl.Text"

What would be the right reference?

Thanks

TB
 
C

Ceberhard

Hi TB:

The sender parameter is your TextBox... all you need to do is cast it
to a TextBox DataType:
Dim oText As TextBox
oText = CType( sender, TextBox )

That (or something like it) should do it for you.
 
T

TB

Thanks a lot for your advice.

TB

Peter Bromberg said:
The "Sender" parameter of the event gives you a reference to the control
that
sent it.

in VB.NET the "Me" keyword provides a reference to the current class
instance, in C#, "this".

So if you cast "Sender" to the control's type, you can get its ID and
every
other property inside your single "generic" eventhandler's code.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top