2013-05-28 39 views
1

TrackBar控制器的改变方向与它被改变的方向相反: page-up/page-down/arrow-up/arrow-down。 Why does the Trackbar value decrease on arrow up/PgUp?如何在键盘按键被按下时更改TrackBar控制操作?

有没有办法解决/扭转这种行为:

这细节在这里被提及?

+1

漂亮的好办法致命与在他的机器上的任何其他程序使用的所有其他的TrackBar完全相反的工作的控制迷惑用户。将“方向”属性设置为“垂直”以使其明确无误。如果你坚持要改变它,那么你需要从TrackBar类派生你自己的控件并覆盖它的ProcessCmdKey()方法。 –

+0

@HansPassant谢谢,我会看到这个功能,..我已经收到用户关于trackbar功能的负面反馈,这是许多着名应用程序或用户期望的反面。 – sharp12345

回答

2

唉......我从来没有注意到过。这里是我的建议刺的@Hans:

public class MyTrackBar : TrackBar 
{ 

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
    { 
     switch (keyData) 
     { 
      case Keys.Up: 
       this.Value = Math.Min(this.Value + this.SmallChange, this.Maximum); 
       return true; 

      case Keys.Down: 
       this.Value = Math.Max(this.Value - this.SmallChange, this.Minimum); 
       return true; 

      case Keys.PageUp: 
       this.Value = Math.Min(this.Value + this.LargeChange, this.Maximum); 
       return true; 

      case Keys.PageDown: 
       this.Value = Math.Max(this.Value - this.LargeChange, this.Minimum); 
       return true; 
     } 
     return base.ProcessCmdKey(ref msg, keyData); 
    } 

} 
1

Idle_Mind的回答是不错的,实际上帮助了我,但它有一个缺点,那就是它可以防止控制从提高ScrollValueChanged事件时最多DownPageUpPageDown被点击。所以,这里是我的版本:

public class ProperTrackBar : TrackBar 
{ 

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
    { 
     int oldValue = this.Value; 
     switch (keyData) 
     { 
      case Keys.Up: 
       this.Value = Math.Min(this.Value + this.SmallChange, this.Maximum); 
       break; 
      case Keys.Down: 
       this.Value = Math.Max(this.Value - this.SmallChange, this.Minimum); 
       break; 
      case Keys.PageUp: 
       this.Value = Math.Min(this.Value + this.LargeChange, this.Maximum); 
       break; 
      case Keys.PageDown: 
       this.Value = Math.Max(this.Value - this.LargeChange, this.Minimum); 
       break; 
      default: 
       return base.ProcessCmdKey(ref msg, keyData); 
     } 

     if (Value != oldValue) 
     { 
      OnScroll(EventArgs.Empty); 
      OnValueChanged(EventArgs.Empty); 
     } 
     return true; 
    } 
} 
相关问题