C# Windows:如何使面板滚动条在加载表单时位于顶部位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1141778/
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
Windows : how to make the panel scroll bar to be at the TOP position on loading the form
提问by
In windows form - have a panel in which a text control were added. On opening the form , I need the panel scroll bar to be at the TOP position.ie., near to the first control. Now when the form is opened , the panel scroll bar is at the BOTTOM.ie., at the last control.
在 Windows 窗体中 - 有一个面板,其中添加了一个文本控件。打开表单时,我需要面板滚动条位于顶部位置,即靠近第一个控件。现在,当打开表单时,面板滚动条位于最后一个控件的 BOTTOM.ie. 处。
回答by tzup
On load, try to set the panel's VerticalScroll property to 0
在加载时,尝试将面板的 VerticalScroll 属性设置为 0
panel1.VerticalScroll.Value = 0;
panel1.VerticalScroll.Value = 0;
EDIT: Assuming you have panel1.AutoScroll = true
编辑:假设你有 panel1.AutoScroll = true
Setting the vertical scroll value to 0 will scroll to the top, however the vertical slider won't.
将垂直滚动值设置为 0 将滚动到顶部,但垂直滑块不会。
Also, maybe you have some control at the bottom of the panel that gets the focus (that should force the panel to scroll down). Setting the focus to the text box at the top of the panel will keep the scroll on top. (but then again maybe you don't want to set the focus to a control at the top)
此外,也许您在面板底部有一些控件可以获取焦点(应该强制面板向下滚动)。将焦点设置到面板顶部的文本框将使滚动保持在顶部。(但话又说回来,也许您不想将焦点设置在顶部的控件上)
回答by monteirobrena
You need select the first element of your panel for the vertical scrollbar appear at top:
您需要选择面板的第一个元素,垂直滚动条出现在顶部:
firstElement.Select();
回答by Christopher Townsend
I had the same problem, but with my own user control. 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 alwaysabeginner
If there are many controls added in a form, then set the tabIndex property sequentially from top to bottom. the scroll bar will set at top if any of the top control's tab index is lesser than tab index of any of the bottom control's tab index. This resolved my issue.
如果在一个表单中添加了许多控件,则从上到下依次设置 tabIndex 属性。如果任何顶部控件的选项卡索引小于任何底部控件的选项卡索引的选项卡索引,则滚动条将设置在顶部。这解决了我的问题。
回答by Matt Gregory
I had a case where I was working on a shopping app, I had an item list UserControl
that contained a vertical list of line item UserControl
s (all Dock=DockStyle.Top
, if that paints a picture), and I was sending a list of items to a method in the item list control and just creating and adding all the item controls to a panel.
我有一个案例,我正在开发一个购物应用程序,我有一个项目列表UserControl
,其中包含一个行项目UserControl
的垂直列表(所有Dock=DockStyle.Top
,如果它绘制了图片),并且我正在将项目列表发送到项目列表控件,只需创建所有项目控件并将其添加到面板。
Not only was the control appearing scrolled all the way to the bottom, but the items were all in reverse order. The list was in the correct sorted order, but when I sent each one to the ItemListControl.Controls.Add()
method, it put them in reverse order. I fixed it by using SetChildIndex()
to place them in reverse order.
不仅控件出现一直滚动到底部,而且项目都以相反的顺序排列。该列表的排序顺序正确,但是当我将每个列表都发送到该ItemListControl.Controls.Add()
方法时,它会将它们按相反的顺序排列。我通过使用SetChildIndex()
以相反的顺序放置它们来修复它。
public void Add(ItemControl itemControl)
{
_itemPanel.Controls.Add(itemControl);
_itemPanel.Controls.SetChildIndex(itemControl, 0);
}
Now that I think about it, DockStyle.Top
might actually be why they're getting added in reverse order. It's hard to tell because if I change the DockStyle to something else, the controls are not all showing up.
现在我考虑了一下,DockStyle.Top
实际上可能就是为什么它们以相反的顺序添加。很难说,因为如果我将 DockStyle 更改为其他内容,则控件不会全部显示。
Anyway, getting the controls inserted in the correct order fixed the scrolling problem.
无论如何,以正确的顺序插入控件解决了滚动问题。
回答by Saif Eddine
In my case am using the followin' code
就我而言,我正在使用以下代码
//to initialize the position of the panel
panel5.AutoScrollPosition = new Point(panel5.AutoScrollPosition.X, 0);
//to initialize the position of the scroll bar
VScrollBar1.Value = 0;