Raise button click event from inherited form

C

Cary Linkfield

I have a standard form I use. It inherits from Windows.Forms.Form. I
usually add a Cancel button the form in the designer. I want to raise the
Cancel button's Click event when the user presses the escape key.

Public Class myStandardForm
Inherits Windows.Forms.Form
Private WithEvents _btnCancel As Button

Private Sub _Load(ByVal sender As Object, ByVal e As EventArgs) Handles
MyBase.Load
MyBase.KeyPreview = True
If Not IsNothing(MyBase.CancelButton) Then
_btnCancel = MyBase.CancelButton
End If
End Sub

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal
keyData As Keys) As Boolean
If keyData = Keys.Escape Then
' ******* here is where I would like to raise _btnCancel.Click
End if
End Function
End Class

Public Class myCustomForm
Inherits myStandardForm
Friend WithEvents btnCancel As System.Windows.Forms.Button
Me.CancelButton = Me.btnExit
Me.Controls.Add(Me.btnExit)

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
' ************ Sometimes I do special things here.
' ************ I want this code to execute as a result
' ************ of raising the Click event, which happens
' ************ as a result of pressing the Escape key
Me.Close()
End Sub
End Class
 
I

Imran Koradia

Take a look at the Button.PerformClick method.

Ofcourse, you could also do something like this:

Protected Overrides Function ProcessCmdKey( _
ByRef msg As Message, _
ByVal keyData As Keys) As Boolean
If keyData = Keys.Escape Then
ClickProcedure( )
Me.Close()
End if
End Function

Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
ClickProcedure( )
Me.Close()
End Sub

Private Sub ClickProcedure( )
' put your special code in here..
End Sub

Ofcourse, this is if you don't need to use the sender and e arguments of the
button click event.
There are probably other ways as well. I guess you can decide what suites
your app best.


hope that helps..
Imran.
 
C

Chris Dunaway

I have a standard form I use. It inherits from Windows.Forms.Form. I
usually add a Cancel button the form in the designer. I want to raise the
Cancel button's Click event when the user presses the escape key.

Correct me if I'm wrong, but if you set the CancelButton property of the
form to the button in question, I think pressing ESC will cause it to be
clicked.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
I

Imran Koradia

duh! you're right - so much for the ButtonClick method :)

Chris Dunaway said:
I have a standard form I use. It inherits from Windows.Forms.Form. I
usually add a Cancel button the form in the designer. I want to raise the
Cancel button's Click event when the user presses the escape key.

Correct me if I'm wrong, but if you set the CancelButton property of the
form to the button in question, I think pressing ESC will cause it to be
clicked.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top