Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the event arguments "sender" and "e"

Tags:

vb.net

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?

like image 823
whytheq Avatar asked Dec 18 '12 09:12

whytheq


People also ask

What is sender and event args?

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.

What are event arguments?

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)

What is sender and E in VB net?

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.

What is the object sender?

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.


2 Answers

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

like image 53
Panagiotis Kanavos Avatar answered Nov 03 '22 01:11

Panagiotis Kanavos


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)
like image 35
major-mann Avatar answered Nov 02 '22 23:11

major-mann