473,394 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Raise button click event from inherited form

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
3 3407
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: asr | last post by:
I've a form with a button on it. How to raise (I mean to run the code within the click event of the button) without clicking the button? Is it possible to raise the events programatically.
2
by: (Pete Cresswell) | last post by:
I've been perusing a "real-life" application and notice that for instance, in a button's Click() event, they don't write the processing code. Instead, they raise an event like AddNewRecord and...
3
by: Cary Linkfield | last post by:
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...
6
by: Li Weng | last post by:
Hi, I have a button (btnSearch) and its Click event sub btnSearch_Click. Clicking the button will trigger the event. But how do I trigger this event in code? I tried RaiseEvent btnSearch.Click...
2
by: Mauricio Rendon | last post by:
Hi, If I have a Label in a Form and need to raise the MouseUp event on the label to the Form ( Parent Control ), What must I do ? Thanks !!!
2
by: Oney | last post by:
I want to open a popup window when user click the web server button control When user click the button, only OnClik="OpenWindow(12)" is sent but no event happend no popup opened.After...
3
by: moondaddy | last post by:
I wrote my own List class which I use to bind to list controls. this class inherits CollectionBase and implements IBindingList. This class contains a list of business classes such as customers...
10
by: Michael Maes | last post by:
Hi, I want to have a Button.Click event 'raise' a shortcut. If eg Button1 is Clicked, I want to have a shortcut 'CTRL + F12' activated. What's the best way to acchieve this. TIA, Michael
1
by: Claire | last post by:
I'm writing an application with custom skinned controls. I have a text editor form with a rich edit control. Because there's no toolbuttons in the skinned control set, I have to use normal ones for...
2
by: William | last post by:
Hi I've created a page with only 'n blank table control on it. When clicking a button, the table populates with various textboxes and dropdown lists to make up a complete form. In the last row...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.