c# - ScrollableControl 滚动条始终归零

标签 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;

我看到了滚动位置的新值。然而,滚动条总是返回到零。我做错了什么?

最佳答案

我无法对该代码试图执行的操作进行逆向工程。自动滚动是你的 friend 。这是一个例子:

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

关于c# - ScrollableControl 滚动条始终归零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7019877/

相关文章:

c# - HttpWebRequest 调用页面两次

c# - Windows 窗体 - ToolStripItem 可见属性始终设置为 false

c# - 多行自动调整大小的可滚动 FlowLayoutPanel

c# - 如何知道TextEdit中当前的可见性偏移量?

c# - 如何删除字符串开头或结尾的所有空格?

c# - 业务层基于角色的授权

c# - 2 GB 真的是我的最大值吗?

c# - VS2010 上的 TFS,合并代码问题

c# - 如何将列表中间的项目放在首位?

c# - winform图片框图像显示空c#