如何向分组框添加滚动条? C#4.0
那么...有人知道如何制作它吗?...
在面板中很容易,因为我们可以将“AutoScroll”属性设置为 true...但 groupbox 没有它。
无论如何...存在某种方法吗?提前感谢;-)。
So... did someone know how to make it?...
In a panel is easy, because we can set the "AutoScroll" property, to true... but groupbox doesn't have it.
Anyways... exists some way for it?, thanks in advance ;-).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
非常简单...在组框中添加一个面板。
Quite simple... Add a panel inside a group box.
声明组框对象和面板对象,默认情况下将包含滚动条和一些我的复选框对象;我在某处读到,出于美观原因,组框没有滚动条(我希望这不是真的,为什么不让用户拨打该电话)。解决方案很简单,一旦您知道您将创建一个面板,该面板将位于组框顶部,只是为了获取滚动条。
Declare Group box object and Panel object which will contain the scroll bar by default and some of my Check box objects;I read somewhere that Group box's don't have scroll bar for aesthetic reasons(i hope it's not true, why not let the user make that call). Solution is simple, once you know that you will create a panel that will lay on top of your group box just to get your scroll bar.
GroupBox
无法显示滚动条。如果您需要类似于可以包含滚动条的GroupBox
的控件,请参阅Panel
控件阅读本文 - 查找(GroupBox 无法显示滚动条)文本
The
GroupBox
cannot display a scroll bar. If you need a control similar to aGroupBox
that can contain a scroll bar, see thePanel
controlRead This Article - Find (The GroupBox cannot display a scroll bar) Text
如果需要在 GroupBox 内添加面板,请将面板停靠在 GroupBox 内,并将停靠面板上的 AutoScroll 属性设置为 true。然后,您可以将所需的任何控件放置在面板上的 GroupBox 内,必要时面板会滚动。
如果您不喜欢上面所说的方式,那么您有两种可能的选择:
可能有一种方法可以通过调用 GroupBox 控件来破解(在这种情况下更像是“滥用”)GroupBox 控件。用于添加滚动条的本机 Win32 API。我很少在托管控件上使用本机调用,但在某些情况下我会这样做,例如,我需要禁用 ListView 上的滚动条,因为 ListView 不公开此属性。下面我公开了在 C# 中使用的本机 Win32 函数,只需调用 SetScrollBarVisible 即可根据需要从代码中启用或禁用滚动条(我还没有在 GroupBox 上测试过这一点):
如果美观对您来说那么重要(不是坏事,用户体验在应用程序开发世界的许多领域都被大大低估了)并且向 GroupBox 添加滚动条对您来说不起作用/看起来不错,您将需要找到另一个解决方案。我想最好的解决方案是从头开始创建自己的控件以满足您的期望(或修改滚动条本身,不知道如何做到这一点),尽管这可能比它可能值得做的工作多得多.
以下是我如何从 C# 代码公开并调用 Win32 函数 SetScrollBar(抱歉,由于某种原因,DllImport 不会格式化为代码块):
[DllImport ("user32")]
私有静态外部长ShowScrollBar(长hwnd,长wBar,长bShow);
If you need to add a panel inside of your GroupBox, Dock a Panel inside of the GroupBox and set the AutoScroll property on the docked Panel to true. You can then place any controls you need inside of the GroupBox on the Panel, which will scroll when necessary.
If you don't like the way that looks as you stated above, then you have two possible options:
There may be a way to hack (more like "abuse" in this situation) the GroupBox control by making calls into the native Win32 API to add a scrollbar. I rarely use native calls on managed controls, but I've done this in situations where, for instance, I need to disable the scroll bar on a ListView as ListView doesn't expose this property. Below I expose the native Win32 function for use in C#, just call SetScrollBarVisible to enable or disable the scrollbar as needed from your code (I have not tested this on a GroupBox):
If aesthetics is THAT important to you (not a bad thing, user experience is greatly underappreciated in many areas of the application development world) and adding a scroll bar to the GroupBox doesn't work/look good to you, you will need to find another solution. I would imagine the best solution would be to make your own control from the ground up that meets your expectations (or modify the scrollbar itself, no idea how to do this), though this is can be a lot more work than it might be worth.
Here is how I expose and call the Win32 function SetScrollBar from my C# code (sorry, the DllImport won't format as a code block for some reason):
[DllImport ("user32")]
private static extern long ShowScrollBar (long hwnd , long wBar, long bShow);
为此,您必须向组框添加 1 个面板并将 autoscroll 属性设置为 true。
然后您将添加第二个面板,该面板比第一个面板大。
在第二个面板(下面代码中的 StringPanel)上,您将添加控件。
To do this you would have to add 1 panel to the groupbox and set the autoscroll property to true.
Then you would add a second panel which would be large then the first panel.
On this second panel (StringPanel in the beleow code) you would add controls.
水平滚动条提示
如果您有一个面板,其中包含的所有控件都固定在顶部(以便它们居中),您将永远不会看到水平滚动条。您需要至少有一个锚定在左侧和左侧的控件。当面板太小而无法显示以显示水平滚动条时,顶部会消失。我在面板上放置了一个带有隐藏文本的标签来完成此操作。
这个小窍门花了我很长时间才发现!希望它有帮助!
Horizontal Scroll Bar Tip
If you have a panel where all the controls contained within it are anchored to the top (so that they are centered) you will never see the horizontal scroll bar. You need to have at least one control that is anchored left & top that disappears when the panel is too small to show it in order for the horizontal scroll bar to appear. I put a label with hidden text on the panel to accomplish this.
This little tidbit took me quite a while to discover! Hope it's helpful!
如果您不需要滚动条,但希望 GroupBox 增大,您可以从“布局”部分编辑以下属性,如下所示。
If you dont want scrollbars, but want your GroupBox to grow, you can edit these below properties from the Layout section as shown below.