Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Windows Development  »  Win Forms  »  Adding Mnemonics (Short Cut Keys) to Your Controls
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...
The Core Class

Lets get started.

 

For this example we’re going to use a custom button control.  The buttons you see in most commercially available applications usually use Mnemonics and have short cut keys, so this will hit close to home and should be pretty easily understood by all, from the rookie programmer to some of the more experienced developers out there.

 

I’m not going to spend too much time on this part, but in a nut shell here is our core button class.  It’s inherited from a UserControl.  Make sure to add a reference to the System.Design.dll.

 

Imports System

Imports System.ComponentModel

Imports System.CompoenetModel.Design

Imports System.Drawing

Imports System.Drawing.Design

Imports System.Drawing.Drawing2D

Imports System.Windows.Forms.Design

 


<DEFAULTEVENT("CLICK"), DefaultProperty(?Text?)>_

Public Class MyBasicButton

            Inherits System.Windows.Forms.UserControl

           

            +[Windows Generated Code]

 


           

            Protected Overrides Sub OnPaint(ByVal e as PaintEventArgs)

                        Dim g as Graphics = e.Graphics

                        g.SmoothingMode = SmoothingMode.AntiAlias

 

                        PaintBorderAndText(g)

 

                        MyBase.OnPaint(e)

            End Sub


            Private Sub PaintBorderAndText(ByVal g as Graphics)

                        Dim rect as New Rectangle(0, 0, Me.Width – 1, Me.Height – 1)

                        Dim rectf as New RectangleF(0, 0, Me.Width, Me.Height)

                        Dim sf as New StringFormat()

 

                        sf.Alignment = StringAlignment.Center

                        sf.LineAlignment = StringAlignment.Center

                        sf.HotKeyPrefix = System.Drawing.Text.HotKeyPrefix.Show

                        sf.Trimming = StringTrimming.EllipsesCharacter

                        sf.FormatFlags = StringFormatFlags.NoWrap

 

                        Dim b as Brush

 

                        ‘Draw the border

                        g.DrawRectangle(New Pen(Color.Black), rect)

 

                        Select Case m_State

                                    Case ButtonState.None

                                                b = New SolidBrush(Color.Black)

                                    Case ButtonState.Over

                                                b = New SolidBrush(Color.Blue)

                                    Case ButtonState.Clicked

                                                b = New SolidBrush(Color.Red)         

                        End Select

 

                        g.DrawString(m_Text, Me.Font, b, rectf, sf)

            End Sub


            Protected Overrides Sub OnMouseEnter(ByVal e as EventArgs)

                        MyBase.OnMouseEnter(e)

                        m_State = ButtonState.Over

                        Invalidate()

            End Sub


            Protected Overrides Sub OnMouseLeave(ByVal e as EventArgs)

                        MyBase.OnMouseLeave(e)

                        m_State = ButtonState.None

                        Invalidate()

            End Sub


            Protected Overrides Sub OnMouseDown(ByVal e as MouseEventArgs)

                        MyBase.OnMouseDown(e)

                        m_State = ButtonState.Clicked

                        Invalidate()

            End Sub


            Protected Overrides Sub OnMouseUp(ByVal e as MouseEventArgs)

                        MyBase.OnMouseUp(e)

                        Select Case m_State

                                    Case ButtonState.Clicked

                                                m_State = ButtonState.Over

                                    Case ButtonState.Over

                                                m_State = ButtonState.None

                        End Select

                        Invalidate()

            End Sub


            Protected Overrides Sub OnMove(ByVal e as EventArgs)

                        MyBase.OnMove(e)

                        Invalidate()

            End Sub


            Protected Overrides Sub OnResize(ByVal e as EventArgs)

                        MyBase.OnResize(e)

                        Invalidate()

            End Sub


            Private m_Text as String

<DESIGNERSERIALIZATIONVISIBILITY(DESIGNERSERIALIZATIONVISIBILITY.VISIBLE), color="#0000ff" Browsable(True)> _

            Public Overloads Overrides Property Text() as String

                        Get

                                    Return m_Text

                        End Get

                        Set (ByVal value as String)

                                    m_Text = value

                                    Invalidate()

                        End Set

            End Property


            Private m_State as ButtonState = ButtonState.None

 

            Public Enum ButtonState as Integer

                        None = 0

                        Over = 1

                        Clicked = 2

            End Enum

End Class

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   So that’s the basic class.  As it stands right now, you should be able to compile the library, add it to your toolbox and a form and run the application.  The text should change colors during the mouse events of the button.  Nothing fantastic, but functional. 

 

   However, stop the debugging on the application and in the Text property type

“&Button”.  The “B” in button should be underlined.  Now run the project again and try to use the ALT + B keys.  Nothing happens right?

 

Sponsored Links