ScrollableControl滚动条始终返回零

时间:2011-08-11 01:31:55

标签: c# winforms

希望这很简单!

我来自UserControl,而我正在压倒OnPaint。我希望这个控件可以滚动,但我想要的是能够获得滚动条的值并处理自己绘制所有内容的位置。我已经设置了以下内容:

// How the hell do I get this scrolling to not return to zero?
this.Scroll += new ScrollEventHandler(TimelineControl_Scroll);

this.HorizontalScroll.Enabled = true;
this.HorizontalScroll.Visible = true;
this.HorizontalScroll.Minimum = 0;

this.VerticalScroll.Enabled = false;
this.VerticalScroll.Visible = true;

this.AutoScroll = false;

我看到滚动位置的新值。但是,滚动条始终返回零。我做错了什么?

1 个答案:

答案 0 :(得分:2)

我无法对该代码尝试执行的操作进行反向工程。 AutoScroll是你的朋友。这是一个例子:

public partial class UserControl1 : UserControl {
    public UserControl1() {
        InitializeComponent();
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.AutoScroll = true;
        this.AutoScrollMinSize = new Size(1000, 0);
    }
    protected override void OnScroll(ScrollEventArgs se) {
        base.OnScroll(se);
        this.Invalidate();
    }
    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
        e.Graphics.DrawLine(Pens.Black, 0, 0, 1000, this.ClientSize.Height);
        base.OnPaint(e);
    }
}
相关问题