简体   繁体   English

同时滚动两个面板c#winForms

[英]scrolling two panels at same time c# winForms

yea so I have 2 panels with the same width and the same width of data in them. 是的,所以我有2个面板,它们具有相同的宽度和相同的数据宽度。 the top panel has autoscroll enabled. 顶部面板启用了自动滚动。 I would like to be able to scroll both panels, with the top panel scrollbar. 我希望能够使用顶部面板滚动条滚动两个面板。 which means that the bottom panel doesn't have a scroll bar. 这意味着底部面板没有滚动条。 How would I do that? 我该怎么做?

alt text http://members.multimania.co.uk/jeff1524/pics/scrolling.jpg 替代文字http://members.multimania.co.uk/jeff1524/pics/scrolling.jpg

EDIT: I tried panel2.AutoScrollPosition = panel1.AutoScrollPosition; 编辑:我尝试了panel2.AutoScrollPosition = panel1.AutoScrollPosition; nothing 没有

I also tried 我也试过

e.Graphics.DrawRectangle(new Pen(Color.Pink,3), 10, 10, 30, 20);
        e.Graphics.TranslateTransform(panel1.AutoScrollPosition.X, 0);

no movement on the rectangle. 矩形上没有移动。 What am i doing wrong? 我究竟做错了什么?

Easy peasy. 十分简单。 Implement the Scroll event for the 1st panel and have it Invalidate() the 2nd. 在第一个面板上实现Scroll事件,并在第二个面板上使Invalidate()生效。 Draw the text in the 2nd panel's Paint event, using the scroll position of the 1st: 使用第一个面板的滚动位置在第二个面板的Paint事件中绘制文本:

    private void panel1_Scroll(object sender, ScrollEventArgs e) {
        panel2.Invalidate();
    }

    private void panel2_Paint(object sender, PaintEventArgs e) {
        Point pos = new Point(panel1.AutoScrollPosition.X, 0);
        TextRenderer.DrawText(e.Graphics, "nobugz waz here", panel2.Font, pos, Color.Black);
        // Draw something
        e.Graphics.TranslateTransform(pos.X, pos.Y);
        e.Graphics.DrawLine(Pens.Black, 0, 0, 100, 100);
    }

Even easier. 甚至更容易。

Just place the panels inside another panel that has the scroll bar (AutoScroll = true). 只需将面板放在具有滚动条的另一个面板中(AutoScroll = true)。 I've used this strategy. 我已经使用了这种策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM