5

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?

whytheq
  • 34,466
  • 65
  • 172
  • 267

4 Answers4

10

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

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • thanks - had a feeling that a separate private method might be required. There may still be occassions in the future when I need to call an event handler from somewhere else in my code though, or can it be avoided 100% of the time? – whytheq Dec 18 '12 at 11:23
  • ...I'm studying `VB.NET` over the next couple of years for an undergraduate qualification hence this question (& lots of future one). Thanks for the help. – whytheq Dec 18 '12 at 11:25
4

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)
major-mann
  • 2,602
  • 22
  • 37
1

You can do this by making a new method. Do not call the event handler.

Public sub abcd()
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

Call this method from where you want

Private Sub stringButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stringButton.Click
abcd()
End Sub

and from this

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
    abcd()
End If
End Sub
syed mohsin
  • 2,948
  • 2
  • 23
  • 47
1

It's better to move your code into a separate function, as already spoken above, but here's a workaround: call stringButton_Click(Nothing,Nothing)

user6218508
  • 200
  • 1
  • 9