如何让2个"Panel"对象同时滚动?

the*_*ser 1 .net c# panel

Panel在彼此相邻的表格上有2个对象.当我滚动第一个面板时,我希望另一个滚动完全相同的数量.

像这样的东西.

private void Panel1_Scroll(object sender, ScrollEventArgs e)
{
    Panel2.ScrollPosition() = Panel1.ScrollPosition();
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*Var 7

我同意scottm,但添加一些有所作为的东西:

private void ScorePanel_Scroll(object sender, ScrollEventArgs e)
{
    var senderPanel = sender as Panel;

    if (senderPanel == null)
    {
        // Might want to print to debug or mbox something, because this shouldn't happen.
        return;
    }

    var otherPanel = senderPanel == Panel1 ? Panel2 : Panel1;

    otherPanel.VerticalScroll.Value = senderPanel.VerticalScroll.Value;
}
Run Code Online (Sandbox Code Playgroud)

另一方面,你总是将Panel1更新为Panel2的滚动偏移,所以如果你滚动Panel2,它实际上不会滚动任何东西.

现在您已经拥有了这个方法,您应该同时使用这两个面板进行订阅,如下所示:

Panel1.Scroll += ScorePanel_Scroll;
Panel2.Scroll += ScorePanel_Scroll;
Run Code Online (Sandbox Code Playgroud)

这可能最好在包含面板的形式的ctor中完成.