By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Raise button click event from inherited form

P: n/a
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
Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
"Cary Linkfield" <clinkfield@[NOSPAM]newtondigital.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
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

Nov 22 '05 #2

P: n/a
On Fri, 1 Oct 2004 15:23:54 -0400, Cary Linkfield wrote:
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.
Nov 22 '05 #3

P: n/a
duh! you're right - so much for the ButtonClick method :)

"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:12*****************************@40tude.net...
On Fri, 1 Oct 2004 15:23:54 -0400, Cary Linkfield wrote:
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.

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.