向面板添加垂直滚动条
我正在尝试使 Panel
可滚动,但只能垂直滚动(因此 AutoScroll
不起作用,因为子控件必须越过左边缘)。
那么这是如何做到的呢?
I am trying to make a Panel
scrollable, but only vertically (so AutoScroll
won't work because the child controls go past the left edge and must).
So how is this done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试“仅”垂直滚动。
(自动滚动需要为 false 才能接受更改)
Try this instead for 'only' scrolling vertical.
(auto scroll needs to be false before it will accept changes)
假设您使用的是 winform,默认面板组件不会为您提供禁用水平滚动组件的方法。解决方法是禁用自动滚动并自行添加滚动条:
详细讨论 此处。
Assuming you're using winforms, default panel components does not offer you a way to disable the horizontal scrolling components. A workaround of this is to disable the auto scrolling and add a scrollbar yourself:
Detailed discussion here.
AutoScroll
确实是解决方案!您只需将
AutoScrollMargin
设置为0, 1000
或类似的值,然后使用它向下滚动并在那里添加按钮和项目!AutoScroll
is really the solution!You just have to set
AutoScrollMargin
to0, 1000
or something like this, then use it to scroll down and add buttons and items there!Panel
有一个AutoScroll
属性。只需将该属性设置为 True,面板就会在需要时自动添加滚动条。Panel
has anAutoScroll
property. Just set that property toTrue
and the panel will automatically add a scroll bar when needed.下面是实现自定义垂直滚动条的代码。这里重要的细节是通过计算添加到面板的控件消耗了多少空间来了解何时需要滚动条。
Below is the code that implements custom vertical scrollbar. The important detail here is to know when scrollbar is needed by calculating how much space is consumed by the controls that you add to the panel.
3 个步骤:
1- 只需将 AutoScroll 属性设置为 true
2- 在 Form load() 中添加以下内容:
3- 在我的面板控件 Add(item) 之后添加以下内容:
无效();
完毕!
3 steps:
1- just set AutoScroll property to true
2- in Form load()add the following:
3- after my Panel controls Add(item) add the following:
Invalidate();
Done!
添加 Kamgman 的答案确实有效。
假设我们要添加一个标签作为面板的子控件:
AutoScroll
设置为 True,将AutoSize
设置为 False。MinimumSize
宽度,以便它至少保持其水平“形状”。Anchor
设置为仅 Top。删除其上的左锚点。这可确保标签仅垂直滚动而不是水平滚动。如果您采用这条路线,则不必添加线条来隐藏水平滚动条。
如果您使用
System.ComponentModel.ComponentResourceManager.ApplyResources
从.resx
文件而不是.Designer.cs< 加载它也可能会更好/代码> .因为就我而言,每当我对此特定表单进行编辑时,我都会丢失
Designer.cs
文件中的更改。但这取决于你的项目是如何设置的Adding on to the answer by Kamgman which does work.
Let's say we were adding a label as the child control to the panel:
AutoScroll
to True andAutoSize
to False.AutoSize
to true. You could give it aMinimumSize
for the Width if you like so that it at least keeps its "shape" horizontally.Anchor
for the label control to Top only. Remove the Left anchor on it. This ensures the label only scrolls vertically but not horizontally.If you go this route you don't have to add the lines to hide the horizontal scroll bar.
It might also be better if you're using
System.ComponentModel.ComponentResourceManager.ApplyResources
to load it from the.resx
file instead of the.Designer.cs
. Because in my case whenever I make edits to this particular form I lose the changes in theDesigner.cs
file. But that will come down to how your project is set up添加到面板的样式代码中,如下所示:
Add to your panel's style code something like this: