C# 如何以编程方式滚动面板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17752970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Programmatically Scroll a Panel
提问by Marwan ?????
I have a System.Windows.Forms.Panel
with some content.
我有System.Windows.Forms.Panel
一些内容。
I am trying to programmatically scroll the panel (vertically) either up or down.
我正在尝试以编程方式向上或向下滚动面板(垂直)。
I have tried setting the AutoScrollPosition
property to a new Point
on the panel but that doesn't seem to do it.
我曾尝试在面板上将该AutoScrollPosition
属性设置为新的Point
,但这似乎并没有做到。
I have the AutoScroll
property set to true.
我已将该AutoScroll
属性设置为 true。
I even tried to set the VerticalScroll.Value
twice as suggested here, but that doesn't seem to work either.
我什至尝试VerticalScroll.Value
按照此处的建议设置两次,但这似乎也不起作用。
This is what I am currently doing:
这就是我目前正在做的事情:
//I have tried passing both positive and negative values.
panel.AutoScrollPosition = new Point(5, 10);
The X and Y values on AutoScrollPosition remain 0 and 0.
AutoScrollPosition 上的 X 和 Y 值保持为 0 和 0。
Any help or direction on this would be greatly appreciated it.
对此的任何帮助或指导将不胜感激。
Thanks in advance,
提前致谢,
Marwan
马万
采纳答案by King King
Here is a solution. I guess you can scroll your Panel
by arbitrary position using Win32
however there is a simple trick to help you achieve your requirement here:
这是一个解决方案。我想您可以Panel
使用任意位置滚动您,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);
Or use extension method for convenience:
或者为了方便使用扩展方法:
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
更新
If you want to set the exact position, modifying the code above a little can help:
如果你想设置准确的位置,稍微修改上面的代码会有所帮助:
//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, Height = 1, 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++);
}
Another solution you may want to use is using Panel.VerticalScroll.Value
. However I think you need more research to make it work as you expect. Because I can see once changing the Value
, the scrollbar position and control position don't sync well. Notice that Panel.VerticalScroll.Value
should be between Panel.VerticalScroll.Minimum
and Panel.VerticalScroll.Maximum
.
您可能想要使用的另一个解决方案是使用Panel.VerticalScroll.Value
. 但是,我认为您需要进行更多研究才能使其按预期工作。因为我可以看到一旦更改Value
,滚动条位置和控件位置就不能很好地同步。请注意,Panel.VerticalScroll.Value
应该在Panel.VerticalScroll.Minimum
和之间Panel.VerticalScroll.Maximum
。
回答by suneel ranga
Use @King King Answered Code and if you want to hide horizontal and vertical scroll bar, just apply the below code in the constructor or initialization.
使用@King King Answered Code,如果你想隐藏水平和垂直滚动条,只需在构造函数或初始化中应用以下代码。
yourPanel.AutoScroll = false;
yourPanel.HorizontalScroll.Maximum = 0;
yourPanel.HorizontalScroll.Visible = false;
yourPanel.VerticalScroll.Maximum = 0;
yourPanel.VerticalScroll.Visible = false;
yourPanel.AutoScroll = true;
回答by Ron
If you have a class that derives from Panel
, then call these two protected methods to scroll the panel:
如果您有一个派生自 的类Panel
,则调用这两个受保护的方法来滚动面板:
// The bottom is off screen; scroll down. These coordinates must be negative or zero.
SetDisplayRectLocation(0, AutoScrollPosition.Y - item.BoundingRect.Bottom + ClientRectangle.Bottom);
AdjustFormScrollbars(true);
In my example, item.BoundingRect.Bottom
is the Y coordinate of the bottom of a thumbnail, and I need to scroll the panel down so that the whole thumbnail is visible.
在我的示例中,item.BoundingRect.Bottom
是缩略图底部的 Y 坐标,我需要向下滚动面板以便整个缩略图可见。
@King King's solution of creating a temporary Control just so that scrolling could be done seemed "heavy" to me. And @Hans Passant's suggestion of setting AutoScrollMinSize
and AutoScrollPosition
didn't work for me.
@King King 创建临时控件以便滚动的解决方案对我来说似乎“沉重”。而设置的@Hans帕桑特的建议AutoScrollMinSize
,并AutoScrollPosition
没有为我工作。
Leave AutoScroll
to its default value of 'true'.
保留AutoScroll
其默认值“true”。
回答by Sagnik Majumder
Try this:- panel.ScrollControlIntoView(childcontrol);
试试这个:- panel.ScrollControlIntoView(childcontrol);
This should work. childcontrol is the particular control that you want to show in your display area.
这应该有效。childcontrol 是您要在显示区域中显示的特定控件。
回答by Christopher Townsend
I had an issue where I couldnt get my panel to scroll back to top . I tried many things to try and get the panel to scroll back to the top after populating it with many controls.
我有一个问题,我无法让我的面板滚动回顶部。我尝试了很多方法来尝试让面板在使用许多控件填充后滚动回顶部。
Nomatter what I did it always put the VScroll bar to the bottom.
不管我做了什么,它总是把 VScroll 栏放在底部。
After exhaustive testing I found it was because my controls had the TabStopproperty set to true (default on user controls) was causing the issue.
经过详尽的测试,我发现这是因为我的控件将TabStop属性设置为 true(用户控件的默认值)导致了问题。
Setting TabStop to falsefixed it.
将 TabStop 设置为false修复了它。
回答by Milan ?vec
This surprisingly works! NOTE THE MINUS SIGN in the code. There is strange behavior in setting scroll position. If you set the position to exact value (50), it goes negative when you read it next time (-50). So you have to invert it before setting new scroll value.
这令人惊讶的工作!注意代码中的减号。设置滚动位置有奇怪的行为。如果您将位置设置为精确值 (50),则下次读取时它会变为负值 (-50)。所以你必须在设置新的滚动值之前反转它。
Scroll down:
向下滚动:
private void ButtonScrollDown_OnClick(object sender, EventArgs e)
{
Point current = yourScrollPanel.AutoScrollPosition;
Point scrolled = new Point(current.X, -current.Y + 10);
yourScrollPanel.AutoScrollPosition = scrolled;
}
Scroll up similarly, (-current.Y - 10)
类似地向上滚动, (-current.Y - 10)
回答by Marc
Create an control that sits slightly outside the visible area (so -1 at the top and clientsize+1 ) and then call ScrollControlIntoView:
创建一个稍微位于可见区域之外的控件(因此 -1 在顶部和 clientsize+1 ),然后调用 ScrollControlIntoView:
public static class PanelExtension {
public static void ScrollDown(this Panel p)
{
using (Control c = new Control() { Parent = p, Height = 1, Top = p.ClientSize.Height + 1 })
{
p.ScrollControlIntoView(c);
}
}
public static void ScrollUp(this Panel p )
{
using (Control c = new Control() { Parent = p, Height = 1, Top = -1})
{
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.
private void buttonUp_Click(object sender, EventArgs e)
{
yourPanel.ScrollUp();
}
private void buttonDown_Click(object sender, EventArgs e)
{
yourPanel.ScrollDown();
}
with yourpanel.SetAutoScrollMargin(1, 1); you can set very fine scrolling steps and then take a timer to call the srolling when buttons are down
与 yourpanel.SetAutoScrollMargin(1, 1); 您可以设置非常精细的滚动步骤,然后在按钮按下时使用计时器来调用滚动