vb.net 面板中的垂直和水平滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19210946/
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
Vertical & Horizontal scrollbars in a panel
提问by PaulOTron2000
Scenario:
设想:
- Put a panel on a form.
- Set the panel's BorderStyle to FixedSingle. (Just so you can see it when you run it.)
- Set the panel's AutoScroll=True
- Set the panel's Anchor to Top, Left, Bottom, Right
- Inside the panel, place any sizable control (button, picturebox or whatever).
- Adjust the control's bottom edge to be just a few pixels above the bottom of the panel.
- Adjust the control's right edge should be a few pixels more narrow than the panel MINUS the width of a vertical scrollbar. (That is, it should be just narrow enough to leave room for a vertical scrollbar to may appear.)
- 将面板放在表单上。
- 将面板的 BorderStyle 设置为 FixedSingle。(这样你就可以在运行它时看到它。)
- 设置面板的 AutoScroll=True
- 将面板的锚点设置为顶部、左侧、底部、右侧
- 在面板内,放置任何相当大的控件(按钮、图片框或其他任何东西)。
- 将控件的底部边缘调整为仅高出面板底部几个像素。
- 调整控件的右边缘应该比面板窄几个像素减去垂直滚动条的宽度。(也就是说,它应该足够窄,以便为可能出现的垂直滚动条留出空间。)
Now run it, and vertically resize the form a little shorter so that you'd expect a VERTICAL scrollbar to appear.
现在运行它,并垂直调整窗体的大小,以便您期望出现垂直滚动条。
Problem: BOTH scrollbars appear, because the very existence of the vertical scrollbar reduces the width of the client area, thus forcing a horizontal scrollbar to appear.
问题: BOTH滚动条出现,因为垂直滚动条的存在减少了客户区的宽度,从而迫使水平滚动条出现。
Apparently .NET evaluates whether a vertical scrollbar is necessary first, then evaluates whether the horizontal should appear, which is dependent upon whether the client size is reduced by the presence of a vertical scxrollbar. (i.e. the same experiment doesn't cause unnecessary VERTICAL scrollbars to appear... only horizontal ones.)
显然 .NET 首先评估是否需要垂直滚动条,然后评估是否应该出现水平滚动条,这取决于客户端大小是否因垂直 scxrollbar 的存在而减小。(即相同的实验不会导致出现不必要的垂直滚动条……只有水平滚动条。)
I'm using VB2008 Express but I'm guessing this carries over to later versions.
我正在使用 VB2008 Express,但我猜这会延续到更高版本。
THE SOLUTION I NEED: I need either of: A) A "vertical autoscroll only" panel. B) I need a way to tell the panel to "rethink" whether the horizontal scrollbar is actually necessary. (Refreshes don't seem to do it.)
我需要的解决方案:我需要: A)“仅限垂直自动滚动”面板。B) 我需要一种方法来告诉面板“重新考虑”水平滚动条是否真的有必要。(刷新似乎没有这样做。)
采纳答案by ??ssa P?ngj?rdenlarp
I ran into something that sounds like what you describe. I wanted just a Vertical scroll because it was going to contain many many things eventually, but not a Horizontal scroll. I used a table layout panel; set the Panel's Vertical size so that the VScroll shows; set the width to accommodate what will go in there plus whatever margin or gutter your code will use.
我遇到了听起来像你描述的东西。我只想要一个垂直卷轴,因为它最终会包含很多东西,而不是水平卷轴。我使用了表格布局面板;设置面板的垂直大小,以便 VScroll 显示;设置宽度以容纳将要进入的内容以及您的代码将使用的任何边距或装订线。
Then, in the TableLayoutPanel
set the width of the scrolling panel's to absolute (I used 2 pixels more than the panel.width). If/when the user resizes, all the extra size gets distributed to everything else. Basically dont let the scrolling panel's width change. Might have to/want to set a minimum form size too.
然后,TableLayoutPanel
将滚动面板的宽度设置为绝对(我使用的宽度比 panel.width 多 2 个像素)。如果/当用户调整大小时,所有额外的大小都会分配给其他所有内容。基本上不要让滚动面板的宽度改变。也可能必须/想要设置最小表单大小。
The things I was adding were all the same width, if yours vary, you might have to set it to accommodate the widest.
我添加的东西都是相同的宽度,如果您的宽度不同,您可能需要将其设置为适应最宽的宽度。
Not sure if you are encountering the same thing, but sure sounds like it.
不确定您是否遇到同样的事情,但听起来确实如此。
回答by BossOz
In order to use panel autoscroll property I do that:
为了使用面板自动滚动属性,我这样做:
- panel.AutoScroll = False (is inverse I know :D)
- panel.VerticalScroll.Visible = False or panel.HorizontalScroll.Visible = False
- panel.AutoScroll = False(我知道是相反的:D)
- panel.VerticalScroll.Visible = False 或 panel.HorizontalScroll.Visible = False
In order to know the dimensions of the scroolbars use
为了知道滚动条的尺寸使用
SystemInformation.HorizontalScrollBarHeight
SystemInformation.VerticalScrollBarWidth
So you can change the dimension of the panel when the scroolbar is shown.
因此,您可以在显示滚动条时更改面板的尺寸。
回答by varocarbas
The AutoScroll
property does not allow you to have too much control on the scrollbars (even though you have VerticalScroll
and HorizontalScroll
properties).
该AutoScroll
属性不允许您对滚动条进行过多控制(即使您拥有VerticalScroll
和HorizontalScroll
属性)。
Out of the proposed alternatives, I go for option A; the marked answer in this postgives a quite effective solution to an equivalent problem. Converted & adapted code (where Panel1
is the panel referred in your question):
在提议的替代方案中,我选择了选项 A;这篇文章中的标记答案为等效问题提供了非常有效的解决方案。转换和改编的代码(Panel1
您的问题中提到的面板在哪里):
Private Declare Function ShowScrollBar Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal wBar As Integer, ByVal bShow As Boolean) As Boolean
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Try
ShowScrollBar(Panel1.Handle, 0, False)
Catch ex As Exception
End Try
MyBase.WndProc(m)
End Sub
If you set the AutoScroll
property of your panel to true and add this code, you would get what you are looking for.
如果您将AutoScroll
面板的属性设置为 true 并添加此代码,您将获得所需的内容。
NOTE: the proposed code works but at certain price: I personally intend to avoid Protected Overrides Sub WndProc
codes as much as possible. If getting exactly this functionality is important for you, rely on the proposed methodology; otherwise, you might have to consider other alternatives (e.g., AutoScroll = False
and adding a VScrollBar
to the panel, which will always be there).
注意:建议的代码有效,但要付出一定的代价:我个人打算尽可能避免使用Protected Overrides Sub WndProc
代码。如果准确获取此功能对您很重要,请依赖建议的方法;否则,您可能不得不考虑其他替代方案(例如,AutoScroll = False
向VScrollBar
面板添加,它始终存在)。