• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

how to prevent UP key from moving focus to the next button in vb.net


Hi,

    I am developing a Windows Forms application in VB.NET. I assigned UP and DOWN Arrow keys as Hot keys for some Buttons. I am handling them in KeyUP event as follows:
 
   Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.Up Then
            Button1_Click(Button1, e)
            Exit Sub
        End If
        If e.KeyCode = Keys.Down Then
            Button2_Click(Button2, e)
            Exit Sub
        End If
    End Sub

         When I press UP or DOWN keys the event is firing but the focus is moving to next or previous control in the Form according to the Tab sequence. How can I prevent moving focus from One control to another control when UP and DOWN arrows pressed.

Thanx,
0
LioElectronic
Asked:
LioElectronic
  • 4
  • 3
1 Solution
 
manchCommented:
'Some Corrections are required for ur case
[Set the KeyPreview Property of Form to True]

Correct the Event Statement (as specified by u)

Your Statement :
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

Correct the statement as
[write Handles MyBase.KeyUp instead of Handles Me.KeyUp in the above mentioned statement]

and then execute
0
 
LioElectronicAuthor Commented:


     Thanx for your reply.

     I made the corrections suggested but the problem remains same.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Note that this will disable Up/Down no matter what control has the focus.  For instance, you wouldn't be able to navigate up/down in a multiline textbox.

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.Up, Keys.Down, Keys.Left, Keys.Right ' (Left/Right also navigate controls)
                If keyData = Keys.Up Then
                    Button1.PerformClick()
                Else
                    Button2.PerformClick()
                End If
                Return True

            Case Else
                Return False

        End Select
    End Function



Or you could test the ActiveControl to see if it is a Button and disable the keys then:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Dim ctl As Control = Me.ActiveControl
        If Not IsNothing(ctl) Then
            If TypeOf ctl Is Button Then
                Select Case keyData
                    Case Keys.Up
                        Button1.PerformClick()
                        Return True

                    Case Keys.Down
                        Button2.PerformClick()
                        Return True

                End Select
            End If
        End If

        Return False
    End Function

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LioElectronicAuthor Commented:

 Hi,

     I am not clear about your post. Can u explain me clearly. Here I am explaining my problem once again.

    I am having some Buttons and Radio Buttons on my Form. I am using UP and DOWN arrow keys as Hot keys for my Buttons. What's happening is when I press UP/DOWN arrow my event is firing and my code is working fine but the problem is the focus of the control is moving to the next control i.e, Button or Radio Button on the Form. I want to maintain the Focus on the same control before and after pressing UP/DOWN keys. How can I do this?


Thanx,
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
(1) Delete your Form1_KeyUp() code.

(2) Add the following code to your Form:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.Up, Keys.Down, Keys.Left, Keys.Right ' (Left/Right also navigate controls)
                If keyData = Keys.Up Then
                    Button1.PerformClick()
                ElseIf keyData = Keys.Down Then
                    Button2.PerformClick()
                End If
                Return True

            Case Else
                Return False

        End Select
    End Function

(3) This will cancel the action of the arrow keys on your form so that the control won't be changed.  Button1 will be clicked when the Up arrow is pressed via Button1.PerformClick().  Button2 will be clicked when the Down array is pressed via Button2.PerformClick().

(4) If you have controls on your form that need the arrow keys to work (such as a TextBox), then we need to use something like the second variation of the code I posted so that we only filter the arrow keys when certain controls have focus.
0
 
LioElectronicAuthor Commented:

  Hi,

      Thank you Its working fine now, and also I got one more doubt. How can I use Combinational keys such as SHIFT+UP or SHIFT+DOWN as Hot Keys using above function.

ThanX,
Sujanakar
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.Net you using?
0
 
LioElectronicAuthor Commented:

  I am working on Visual Studio 2005.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now