2

当通过以下TrackBar方式更改时,控件的更改方向与它应该更改的方向相反:向上翻页/向下翻页/向上箭头/向下箭头。

此处详细提到了这一点: 为什么 Trackbar 值在向上箭头/PgUp 时减小?

有没有办法修复/扭转这种行为?

4

2 回答 2

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);
    }

}
于 2013-05-28T23:20:01.903 回答
2

Idle_Mind 的回答很好,实际上对我有所帮助,但它有一个缺点,即它会阻止控件在单击、、或时引发ScrollValueChanged事件。所以,这是我的版本:UpDownPageUpPageDown

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;
    }
}
于 2017-08-23T08:28:22.243 回答