如何在按下键盘键时更改TrackBar控件操作?

时间:2013-05-28 20:39:16

标签: c# .net vb.net winforms

TrackBar控件的变化方向与更改时的变化方向相反: 向上翻页/向下翻页/向上翻页/向下翻页。

这里详细说明: Why does the Trackbar value decrease on arrow up/PgUp?

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

2 个答案:

答案 0 :(得分: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 :(得分:2)

Idle_Mind的答案很好,实际上对我很有帮助,但它有一个缺点,就是当 Up <时,它会阻止控件引发ValueChangedpublic 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; } } 事件单击/ kbd>,向下 PageUp PageDown 。所以,这是我的版本:

$('#idItem').value
相关问题