In vb.net, during a validation, how do I determine the name of the control calling the validation? The ActiveControl command returns the name of the NEXT control getting the focus, not the control being validated.
I assume you're handling the Validating
or Validated
event? Those events always provide the object that triggered them so you simply have to do :
Private Sub DoValidating(sender As Object, e As CancelEventArgs) _
Handles SomeControl.Validating
Dim controlName As String = DirectCast(sender, Control).Name
' etc...
End Sub
The standard event handlers always return untyped objects as the sender
parameter. When you do DirectCast(sender, Control)
you're making an assumption that the event was raised by a Control
type object - could be anything - text box, button, slider, label, whatever. To get the name of the control you don't have to be more specific since all Control
s will have a .Name
property. You have to cast the object since basic objects do not have a .Name
property. To be safer you can check to be sure that the sender
is in fact a Control
like :
If TypeOf sender Is Control Then
Dim controlName As String = DirectCast(sender, Control).Name
' etc...
End If
After you've cast the object you can access any of the properties of the type you've cast it to. DirectCast(sender,Control).Name
is the same as saying myTextBox1.Name
.
Now, to answer the second question, the above approach is handy when you want one event handler to handle events generated by multiple types of controls which all share a common base property that you are interested in examining. The base Control
type, in this case, also has a .Text
property, but you'll notice that the TextBox
type overrides the functionality of the .Text
property so, while you could simply do DirectCast(sender, Control).Text
, it is better to cast to the correct type. In this way you would simply do :
If TypeOf sender Is TextBox Then
Dim textBoxText As String = DirectCast(sender, TextBox).Text
' etc...
End If
This allows you to build generic handlers for a variety of events, but also allows you to take specific actions based on the type of the object that generated the event. Say, for example, you want to perform some common validation tasks for radio buttons, textboxes, comboboxes, etc; but that you also want to do some validation specific to each type you can check the type of the sending object (as above) and then work with it after casting it to its proper type. If you're doing multiple things with the object you can also save repeated work by setting the cast to a local variable :
If TypeOf sender Is TextBox Then
Dim senderTxtBox As TextBox = DirectCast(sender, TextBox)
Dim senderTxt As String = senderTxtBox.Text
Dim controlName As String = senderTxtBox.Name
senderTxtBox.ForeColor = Color.Blue
' etc...
End If
Not sure if that clears things up? I'm guessing you wanted the name of the textbox so that you could reference it in code - what is key to observe here is that you don't need to know the name of the object to work with it. The name is just a string property that is used by the compiler to translate your code. The name is just a means of obtaining a reference to the actual underlying object. In the case of an event handler, the object reference is handed to you via the sender
property, so you don't need to know its name to work with it.