Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

What is the correct way to do this. I have a windows forms app, and I want several of the forms to close when I press escape.

 

So I have the general sub-routine in say 6 different forms, all with the same code:

 

   Private Sub CloseFormOnEscape(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmbSkin.KeyPress
       If Asc(e.KeyChar) = Keys.Escape Then Me.Close()
   End Sub

 

In order to make my app more manageable, I could put the one subroutine in a general class, and just use AddHandler in the Form_Load() sub of each form.

 

I was wondering if there was some better way that I could do it, using Inherits or something like that.

 

This is a simple example, the actual code that I want to do this with is a class that is used to drag a skinned form, based on some specific controls.

 

Cheers,

 

Pinky

Posted

I'm slightly confused, the first time I read your post I thought that with a single press of escape you wanted to close several forms. But the second time I read it I just got the impression you wanted several forms with similar behaviour to each other, closing each one individually when escape is pressed. If the latter is true then as you can say this could be handled by either attaching events in the manner your doing or by inheritance.

 

To do the inheritance method you simply create a class (called ExtendedForm or something) that inherits from System.Windows.Form and add/override key events. Then any form you wish to have this behaviour you inherit from MyNamespace.ExtendedForm rather than System.Windows.Form.

Anybody looking for a graduate programmer (Midlands, England)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...