Results 1 to 6 of 6

Thread: [RESOLVED] Cancel arrow buttons event tabcontrol

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    33

    Resolved [RESOLVED] Cancel arrow buttons event tabcontrol

    Hi,

    When I click the left or right arrow buttons on the keyboard my tabcontrol tabpage changes from one tab to another.

    How can I cancel the arrow buttons from changing the tab pages?

    In other words how can I disable the arrow buttons when the tabpage has focus?

    Is it possible to do this without extending the base class?

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Cancel arrow buttons event tabcontrol

    Set the form's keypreview property to true, and add this code for the keydown handler :

    Code:
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    
            If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then
                e.SuppressKeyPress = True
            End If
        End Sub
    NB this will trap the left and right arrows for all controls, you may want to check that the form's currently selected control first
    Last edited by keystone_paul; May 23rd, 2009 at 10:34 AM. Reason: added proviso about the selected control

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Cancel arrow buttons event tabcontrol

    This would trap it only for the specific control :

    Code:
            If Me.ActiveControl Is TabControl1 Then
                If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then
                    e.SuppressKeyPress = True
                End If
            End If

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Cancel arrow buttons event tabcontrol

    Paul's solution is correct, although you would be better off just handling the KeyDown event of the TabControl rather than the form. I'd always recommend using OrElse rather than Or too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    33

    Re: Cancel arrow buttons event tabcontrol

    Awesome, thanks heaps guys that worked a treat.

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Cancel arrow buttons event tabcontrol

    Quote Originally Posted by jmcilhinney View Post
    Paul's solution is correct, although you would be better off just handling the KeyDown event of the TabControl rather than the form. I'd always recommend using OrElse rather than Or too.
    Quite right - for some reason I had in the back of my mind that there was some issue with trapping special keys on the tabcontrol but I've just tried it and I was mistaken.

    You would indeed be better off just applying the event to the tabcontrol rather than the form.

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