Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Windows Development  »  Win Forms  »  Adding Mnemonics (Short Cut Keys) to Your Controls  »  Adding the Mnemonics
Adding Mnemonics (Short Cut Keys) to Your Controls
by Chris Manning | Published  03/11/2005 | Win Forms | Rating:
Chris Manning
I work as a professional .NET developer specializing in the areas of Windows Applications, Web Applications, and ADO.NET. I spend a good majority of my free time helping developers learn the .NET framework at vbCity and was recently awarded the Microsoft Visual Developer, Visual Basic MVP award. 

View all articles by Chris Manning...
Adding the Mnemonics

Now to the core of this article;  Implementing Mnemonics in Custom Controls.


First we need to Implement an Interface.  So in your button class code, right after the Inherits System.Windows.Forms.UserControl, add


 Implements IButtonControl


There are now three(3) subsequent Subs / Functions we need to implement and one(1) Property.


If you’re like me, you try to keep the properties with the properties, the subs with the subs, etc.. In a class, however you can add these anywhere in the class.


First let's implement the NotifyDefault  Sub.  Nothing will actually happen here, but we have to add it to satisfy the "Implements IButtonControl"


 Public Sub NotifyDefault() Implements IButtonControl.NotifyDefault

            ‘Nothing Happens Here

End Sub


Next we’ll implement the DialogResult property.  The DialogResult property is like the dialog result property for any type of form, button, messagebox, open file dialog…anything.  It simply supplies the UI with a corresponding result for certain actions.


Private m_DialogResult as DialogResult

Public Property DialogResult() as DialogResult Implements IButtonContro.DialogResult


                        Return m_DialogResult

            End Get

            Set (ByVal value as DialogResult)

                        If [Enum].IsDefined(GetType(DialogResult), value) Then

                                    m_DialogResult = value

                        End If

            End Set

End Property

We now need to Implement the PerformClick method.  This is what allows the Mnemonic call to perform any actions associated with the OnClick method and the subsequent Click Event.


 Public Sub PerformClick() Implements IButtonControl.PerformClick


End Sub


And last but not least, the Function that does it all:-  ProcessMnemonic.  This is the function that determines which key combinations are being pressed while the UI the control is on is active.  .NET has a couple of built in functions that help us determine two things.

  1. Is the key that’s being pressed the Key that we want to process and therefore perform our actions.
  2. Is there a control key that’s being held.  Control keys are keys like Alt, Ctrl, Return, etc.

 Public Overrides Function ProcessMnemonic(ByVal c as Char) as Boolean

            If (Control.IsMnemonic(c, Me.Text) and (Control.ModifierKeys = Keys.Alt)) Then


                        Return true


            Return False

End If

End Function


And that’s it.  You should now be able to recompile your control. Add it to a form, run the form and press “Alt + ‘YourMnemonicKey’”.  Barring you have something in the Click event of the control, that event should fire.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.33333333333334 out of 5
 15 people have rated this page
Article Score19575
Sponsored Links