Results 1 to 5 of 5

Thread: How to disable arrow keys on radio buttons?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    How to disable arrow keys on radio buttons?

    I have a group of radio buttons inside a groupbox and I don't want the user to be able to move the focus away from a button by using the arrow keys. I've tried the following keydown event, but it never gets fired:

    Code:
     Private Sub rbCode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles rbCode.KeyDown
            ' If an arrow key is pressed, maintain focus on this radio button   
            If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then
                e.SuppressKeyPress = True
                rbCode.Focus()
            End If
    
        End Sub
    Does anyone how to do this, if it's possible? thanks

  2. #2
    Member cyberM's Avatar
    Join Date
    Mar 2009
    Location
    Croatia , Zagreb
    Posts
    61

    Re: How to disable arrow keys on radio buttons?

    I give you something usefull with A key..
    You find all in this peace of code..

    Private Sub RadioButton1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RadioButton1.KeyDown

    If e.KeyCode = Keys.A Then
    RadioButton1.TabStop = False
    End If

    End Sub
    If you satisfied add to reputation





    PhotoResizer GDI+Shape form,shape inside

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to disable arrow keys on radio buttons?

    Hey,

    As per the documentation here:

    http://msdn.microsoft.com/en-us/libr...l.keydown.aspx

    You will see the following:

    Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true.
    i.e. you will have to do some extra work to prevent this happening.

    Also, just as one other suggestion, make sure you have set the KeyPreview property for the form to true.

    Gary

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: How to disable arrow keys on radio buttons?

    After posting this question, I did find that microsoft link and realize that's what I need to do. I just haven't figured out how to do it yet. If anyone has an example of how to apply that override to my situation, I would appreciate it. - This is their example code:

    Code:
    Imports System.Windows.Forms
    
    Public Class Form1
        Inherits Form
    
        Public Sub New()
    
            Dim panel As New FlowLayoutPanel()
    
            Dim tabTextBox1 As New TabTextBox()
            tabTextBox1.Text = "TabTextBox"
            panel.Controls.Add(tabTextBox1)
    
            Dim textBox1 As New TextBox()
            textBox1.Text = "Normal TextBox"
            panel.Controls.Add(textBox1)
    
            Me.Controls.Add(panel)
    
        End Sub
    
    End Class
    
    Class TabTextBox
        Inherits TextBox
    
        Protected Overrides Function IsInputKey( _
            ByVal keyData As System.Windows.Forms.Keys) As Boolean
    
            If keyData = Keys.Tab Then
                Return True
            Else
                Return MyBase.IsInputKey(keyData)
            End If
    
        End Function
    
        Protected Overrides Sub OnKeyDown( _
            ByVal e As System.Windows.Forms.KeyEventArgs)
    
            If e.KeyData = Keys.Tab Then
                Me.SelectedText = "    "
            Else
                MyBase.OnKeyDown(e)
            End If
    
        End Sub
    
    End Class
    Last edited by dkahn; Sep 6th, 2009 at 08:46 PM.

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to disable arrow keys on radio buttons?

    Hey,

    The sample that they provides shows you how to create your own TextBox, which derives from TextBox, and then overrides the standard functionality of the IsInputKey and OnKeyDown Members.

    You would need to do this for the Radio Button that you are trying to use, i.e. create a class which inherits from RadioButton, and then override these members.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width