How to Capture the Name of the Current Control

  • Thread starter Thread starter Randy
  • Start date Start date
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
 
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
 
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
....
 
Back
Top