Results 1 to 5 of 5

Thread: [RESOLVED] Disabling Home.End keys on TrackBar control

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Resolved [RESOLVED] Disabling Home.End keys on TrackBar control

    I have a TrackBar control that is part of a custom control. It functions fine, except I need to disable keyboard control of the TrackBar, i.e. the user should be able to drag the value of the trackbar with the mouse but not set it with the Home, End, or arrow keys. The reason for this is that the custom control is intended to be placed into a scrollable list. In other words, there can be 15 - 50 (or even more) of these controls in a single scroll list, and I want the Home, End, and arrow keys to ONLY control focus in the list.

    What I have now works, so long as the user doesn't have focus on the TrackBar. When he does, then focus still shifts, but only after the value of the TrackBar on the currently selected control is adjusted.

    In order to implement the focus change, I'm trapping the KeyUp event on the custom control and every sub-control on it. When any of those keys are pressed, I set e.Handled and e.SuppressKeyPress to true, but it isn't stopping the TrackBar from processing the keystroke to adjust the value.

    Any ideas on how I can disable the TrackBar from processing these keystroke commands to alter the value?

    I'm using .NET 4.0 under Windows 7.

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Disabling Home.End keys on TrackBar control

    Does this work?, maybe you already do this;

    Code:
        Private Sub TrackBar1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) _
            Handles TrackBar1.KeyDown
            e.Handled = True
        End Sub

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Disabling Home.End keys on TrackBar control

    As far as you are using many TrackBar control then you have to create custom TrackBar and implement OnKeyDown event to set SuppressKeyPress = True when Home, End and arrows keys pressed.

    * Add new class, name it MyTrackBar (or what you like)
    * Paste the following code
    * Build your project

    A new control will added to the ToolBox, use it instead of the built-in TrackBar.

    Code:
    Option Strict On
    Option Explicit On
    
    Public Class MyTrackBar
        Inherits Windows.Forms.TrackBar
    
        Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
            Select Case e.KeyCode
                Case Keys.Home, Keys.End, Keys.Left, Keys.Right, Keys.Up, Keys.Down
                    e.SuppressKeyPress = True
    
                    MyBase.OnKeyDown(e) ' Comment this line if you want to unfire KeyDown event when those keys pressed.
                Case Else
                    MyBase.OnKeyDown(e)
            End Select
    
        End Sub
    End Class



  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: Disabling Home.End keys on TrackBar control

    Okay, I solved the issue.

    I kept what I have with the KeyUp event handler, but then I declared a KeyDown handler and set e.Handled = true and e.SuppressKeyPress = true for each of the specified keys. I suppose I had to suppress the keys at the first event to fire, KeyDown, even though I only want to process the KeyUp event for those keys.

    So thanks for the suggestions... it works now.

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Disabling Home.End keys on TrackBar control

    2HongKongCV

    You said
    there can be 15 - 50 (or even more) of these controls in a single scroll list,
    Handling KeyDown event for 15~50 controls is really tough, creating a custom TrackBar as discribed above will save your time.



Tags for this Thread

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