whytheq
whytheq

Reputation: 35577

Running event x from event y

(Newbie VB.NET question)

Here's the specific code behind my simple winforms that I don't fully understand:

Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
    'do something
End Sub

Private Sub MainForm_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Enter
    okButton_Click(Me, e) '<=== argumanets must be wrong 
End Sub

What I'm trying to achieve: If the user hits Enter when they have the winforms active then I'd like the Click event handler of the okButton to fire.

Obviously from the above my understanding of the arguments I need to supply to the event called okButton_Click is lacking; what are the correct arguments and why?

Upvotes: 0

Views: 129

Answers (2)

nikeee
nikeee

Reputation: 10724

I think you might use the AcceptButton property of the form. Just set it to the desired button and it should do the trick. Note that there is also a CancelButton property.

Answering your event-question: The sender argument marks the sender of the event. Mostly, this is the Me instance of the class. In my opinion, Me seems to be absolutely correct. The e argument contains the EventArgs of the specific event. If you're not using this argument in your function body, the content of this variable doesn't matter. You could use Nothing or just route the EventArgs (that's what you've done).

Refering to your comment: EventArgs is a base class for event-specific data. For example, if you're subscribing to a mouse event, ewill be a MouseEventArgs. The MouseEventArg class offers you the mouse buttons that have been pressed and the coordinates of the pointer when the event was fired.

In your case, the events only have EventArgs which provide only basic information about the event. There does not seem to be special information about it.

Note: If you want to combine multiple events into one callback, you can make e of type EventArgs because every e should inherit from EventArgs following the Microsoft guidelines. Therefore, you can combine a Button-Click with a Mouse-Move into one callback because the signature of the delegates match.

A nicer way than just passing Nothing to the target Sub, is to combine two callbacks into one. You can do this in VB.NET using multiple Handles like this:

Private Sub SomeSub(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click, MyBase.Enter
    'this is getting called on a okButton.Click and MyBase.Enter
End Sub

(Scroll to the right to see the Handles)

Note that you don't need a second Sub which calls the first one. Everything is in one Sub.

Upvotes: 4

user1693593
user1693593

Reputation:

Try this instead:

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
    If (keyData And Keys.KeyCode) = Keys.Enter Then
        okButton.PerformClick()
        Return True
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

You might need to exclude some controls in the check if you want to use enter key with other controls, ie:

If (keyData And Keys.KeyCode) = Keys.Enter AndAlso Not Textbox1.Focused Then

Upvotes: 1

Related Questions