How to Capture the Name of the Current Control

R

Randy

Hi,
I'm trying to capture the name of the current control (which is a text
box) in the validating procedure as well as the text in the control,
which I intend to pass to another subroutine for coding efficiency.
I've tried two different options, neither works:

Private Sub thisTB_Validating....
Dim strTest as String = Me.Text
Dim strName as String = Me.Name
' this yields values for the form, not the control

'also tried
Dim strTest as String = ActiveControl.Text
Dim strName as String = ActiveControl.Name
' this fails as it yields values for the control that the user clicks
to, not the one being validated.

I'm guessing that this is pretty simple. Can anyone help?

Thanks,
Randy
 
R

rowe_newsgroups

Can anybody help me with this?

Cast the sender into a textbox - then it will expose the textbox's
properties and methods.

i.e.

Private Sub TextBoxes_Validating(ByVal sender As System.Object,
ByVal e As System.ComponentModel.CancelEventArgs) Handles
TextBox2.Validating, TextBox1.Validating
MessageBox.Show(DirectCast(sender, TextBox).Name)
End Sub

Thanks,

Seth Rowe
 
H

Hal Rosser

Randy said:
Hi,
I'm trying to capture the name of the current control (which is a text
box) in the validating procedure as well as the text in the control,
which I intend to pass to another subroutine for coding efficiency.
I've tried two different options, neither works:

Private Sub thisTB_Validating....
Dim strTest as String = Me.Text
Dim strName as String = Me.Name
' this yields values for the form, not the control

'also tried
Dim strTest as String = ActiveControl.Text
Dim strName as String = ActiveControl.Name
' this fails as it yields values for the control that the user clicks
to, not the one being validated.

I'm guessing that this is pretty simple. Can anyone help?

Thanks,
Randy
+++++++++
(assuming you have "sender as Object" as an Arg) :
dim tb as TextBox = CType(sender, TextBox)
Dim strNameOfTextBox as String = tb.Name
....
 

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

Top