对于SplitPanel控件,AutoScrollPosition始终返回(0,0)

时间:2012-01-17 14:58:56

标签: c# winforms

我正在尝试同步分割面板控件中两个splitcontainer的滚动。我有以下代码:

    Point mPrevPan1Pos = new Point(); 
    Point mPrevPan2Pos = new Point(); 

    void PanelPaint(object sender, System.Windows.Forms.PaintEventArgs e) 
    { 
       if (splitContainer1.Panel1.AutoScrollPosition != mPrevPan1Pos) 
       { 
          splitContainer1.Panel2.AutoScrollPosition = new System.Drawing.Point(-splitContainer1.Panel1.AutoScrollPosition.X, -splitContainer1.Panel1.AutoScrollPosition.Y); 
          mPrevPan1Pos = splitContainer1.Panel1.AutoScrollPosition; 
       } 
       else if (splitContainer1.Panel2.AutoScrollPosition != mPrevPan2Pos) 
       { 
          splitContainer1.Panel1.AutoScrollPosition = new System.Drawing.Point(-splitContainer1.Panel2.AutoScrollPosition.X, -splitContainer1.Panel2.AutoScrollPosition.Y); 
          mPrevPan2Pos = splitContainer1.Panel2.AutoScrollPosition; 
       } 
    } 

但AutoScrollPosition总是(0,0)。我为两个拆分容器启用了AutoScroll。为什么是这样?我该怎么做才能获得滚动位置?

1 个答案:

答案 0 :(得分:0)

看起来您复制了此答案的代码:Scroll 2 panels at the same time

你搞定了事件:

this.splitContainer1.Panel1.Paint += new PaintEventHandler(PanelPaint);
this.splitContainer1.Panel2.Paint += new PaintEventHandler(PanelPaint);