c# – 如何以编程方式滚动面板
发布时间:2020-12-15 17:42:30 所属栏目:百科 来源:网络整理
导读:我有一个System. Windows.Forms.Panel有一些内容. 我试图以编程方式向上或向下滚动面板(垂直). 我已经尝试将AutoScrollPosition属性设置为面板上的新Point,但似乎没有这样做. 我将AutoScroll属性设置为true. 我甚至试图将VerticalScroll.Value设置为建议的he
我有一个System.
Windows.Forms.Panel有一些内容.
我试图以编程方式向上或向下滚动面板(垂直). 我已经尝试将AutoScrollPosition属性设置为面板上的新Point,但似乎没有这样做. 我将AutoScroll属性设置为true. 我甚至试图将VerticalScroll.Value设置为建议的here两倍,但这似乎也不起作用. 这正是我目前正在做的: //I have tried passing both positive and negative values. panel.AutoScrollPosition = new Point(5,10); AutoScrollPosition上的X和Y值保持为0和0. 对此的任何帮助或指导将不胜感激. 提前致谢, 马尔万 解决方法
这是一个解决方案.我想你可以使用Win32通过任意位置滚动您的面板,但是有一个简单的技巧可以帮助您实现您的要求:
public void ScrollToBottom(Panel p){ using (Control c = new Control() { Parent = p,Dock = DockStyle.Bottom }) { p.ScrollControlIntoView(c); c.Parent = null; } } //use the code ScrollToBottom(yourPanel); 或使用扩展方法方便: public static class PanelExtension { public static void ScrollToBottom(this Panel p){ using (Control c = new Control() { Parent = p,Dock = DockStyle.Bottom }) { p.ScrollControlIntoView(c); c.Parent = null; } } } //Use the code yourPanel.ScrollToBottom(); UPDATE 如果要设置确切位置,请稍后修改代码可以帮助您: //This can help you control the scrollbar with scrolling up and down. //The position is a little special. //Position for scrolling up should be negative. //Position for scrolling down should be positive public static class PanelExtension { public static void ScrollDown(this Panel p,int pos) { //pos passed in should be positive using (Control c = new Control() { Parent = p,Height = 1,Top = p.ClientSize.Height + pos }) { p.ScrollControlIntoView(c); } } public static void ScrollUp(this Panel p,int pos) { //pos passed in should be negative using (Control c = new Control() { Parent = p,Top = pos}) { p.ScrollControlIntoView(c); } } } //use the code,suppose you have 2 buttons,up and down to control the scrollbar instead of clicking directly on the scrollbar arrows. int i = 0; private void buttonUp_Click(object sender,EventArgs e) { if (i >= 0) i = -1; yourPanel.ScrollUp(i--); } private void buttonDown_Click(object sender,EventArgs e) { if (i < 0) i = 0; yourPanel.ScrollDown(i++); } 您可能想要使用的另一个解决方案是使用Panel.VerticalScroll.Value.不过,我认为你需要更多的研究,使它按照你的期望工作.因为我可以看到一旦改变了值,滚动条位置和控制位置就不能很好的同步.请注意,Panel.VerticalScroll.Value应位于Panel.VerticalScroll.Minimum和Panel.VerticalScroll.Maximum之间. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |