I'm struggling to understand these arguments.
They are automatically generated and I don't need to give them much thought but if I want to call one of these events from some other part of the form then I need to supply these arguments with values - not easy as I'm still unsure what they mean.
Here is the event I'd like to Call
:
Private Sub stringButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stringButton.Click
Dim outString As String
Select Case stringTextBox.Text
Case ""
outString = "You entered an empty string"
Case "10"
outString = "10"
Case Else
outString = "Your string is not covered by the cases"
End Select
strResultTextBox.Text = outString
End Sub
Here is the calling method:
Private Sub stringTextBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles stringTextBox.KeyDown
If e.KeyCode = Keys.Enter Then
Call stringButton_Click(Me, Me) '<<(Me,Me) is my incorrect attempt
End If
End Sub
What arguments should I use and why?
EventArgs e is a parameter called e that contains the event data, see the EventArgs MSDN page for more information. Object Sender is a parameter called Sender that contains a reference to the control/object that raised the event.
The second, called the event argument, contains information specific to the event, if any. For most events, the event argument is of type EventArgs, which does not expose any properties. So, the general prototype for an event in Visual Basic is: Private Sub EventName (ByVal sender As Object, _ ByVal e As EventArgs)
Sender is used to specify which object generated the event, e is the event data itself. For example, usually when you click on a button in a form "sender" would be the button, and e would be the mouse event.
In the general object, the sender is one of the parameters in the C# language, and also, it is used to create the instance of the object, which is raised by the specific events on the application. That event is handled using the Eventhandler mechanism that is mostly handled and responsible for creating the objects.
DON'T call event handlers from other parts of your code! This is very sloppy coding. Create a separate method that does what you need, with the parameters you need, and call it both from the event handler and any other part of your form, eg
Private Function String GetResponseMessage(ByVal input as String)
Select Case input
Case ""
Return "You entered an empty string"
Case "10"
Return "10"
Case Else
Return "Your string is not covered by the cases"
End Select
End Function
Private Sub stringButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stringButton.Click
Dim outString As String=GetResponseMessage(stringTextBox.Text)
strResultTextBox.Text = outString
End Sub
The sender and e arguments are the standard signature of event handlers. Sender is the object that raised the event and e contains the data of the event. All events in .NET contain such arguments.
EventArgs is the base class of all event arguments and doesn't say much about the event. Several events use a derived class to supply more data, eg. a KeyPress event uses the KeyEventArgs class which contains the actual key pressed in its KeyChar property.
This is basic .NET programming stuff and you should spend some time understanding .NET events and Windows Forms instead of trusting auto-generated code
Sender is used to specify which object generated the event, e is the event data itself. For example, usually when you click on a button in a form "sender" would be the button, and e would be the mouse event.
In you case you could simply change
Call stringButton_Click(Me, Me)
To
Call stringButton_Click(Nothing, EventArgs.Empty)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With