Search Amazon:

Disable the .NET DataGrid's TAB, Arrow, Home/End, Page Up and Page Down keys

Derive a new DataGrid and override the ProcessCmdKey method to trap for the various navigation keys.

Add this module level variable:

    ' Disable all navigation keys.
    Private myDisableAllNavKeys As Boolean = False

Add this property:

    Public Property DisableAllNavKeys() As Boolean
        '
        ' Get/set property indicating if naviagation keys are allowed.
        '
        Get
            DisableAllNavKeys = myDisableAllNavKeys
        End Get

        Set(ByVal theValue As Boolean)
            myDisableAllNavKeys = theValue
        End Set

    End Property

Override this method:

    Protected Overrides Function ProcessCmdKey(_
            ByRef msg As System.Windows.Forms.Message, _
            ByVal keyData As System.Windows.Forms.Keys) As Boolean
        '
        ' Disable the navigation keys.
        '
        On Error Resume Next

        If myDisableAllNavKeys Then
            Select Case msg.WParam.ToInt32()

                Case CInt(Keys.Down)
                    Return True

                Case CInt(Keys.Up)
                    Return True

                Case CInt(Keys.Right)
                    Return True

                Case CInt(Keys.Left)
                    Return True

                Case CInt(Keys.PageDown)
                    Return True

                Case CInt(Keys.PageUp)
                    Return True

                Case CInt(Keys.Home)
                    Return True

                Case CInt(Keys.End)
                    Return True

                Case CInt(Keys.Tab)
                    Return True
            End Select
        End If

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

Now all you need to do is set your derived grid's DisableAllNavKeys property. In real life I use multiple properties so I can disable individual keys such as the TAB key, groups of keys such as the arrow keys, all keys,...

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Comments: 1 Items to Show:     

     
Id:  1 Posted:  10/21/2008 12:36:19 PM By:  mynameisjoe
are this code works in VB 2005?
i try this but nothing happen..
thx
 
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2009 TheScarms