你好,我有一个表格布局面板,我需要动态地将控件绑定到它上面。当项目数量超过面板的高度时,垂直滚动条显然会出现,这没有问题。
但与此同时,即使项目的宽度小于面板的宽度,水平滚动条也会出现。我该如何防止这种情况发生?
你好,我有一个表格布局面板,我需要动态地将控件绑定到它上面。当项目数量超过面板的高度时,垂直滚动条显然会出现,这没有问题。
但与此同时,即使项目的宽度小于面板的宽度,水平滚动条也会出现。我该如何防止这种情况发生?
int vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
tableLayoutPanel1.Padding = new Padding(0, 0, vertScrollWidth, 0);
今天在解决这个问题时,我花费了一些时间,但最终得出了以下结果:
Create a new class that inherits from TableLayoutPanel (lets call it MyTableLayoutPanel), and override the MaximumSize property like this:
public override Size MaximumSize
{
get
{
if (Parent != null)
return new Size(Parent.Width, 0);
else
return base.MaximumSize;
}
set
{
base.MaximumSize = value;
}
}
You could of course make it more general purpose by adding another property that decides whether or not you should return the altered MaximumSize or not, but hopefully that much is obvious to anyone that's reading this.
Change the TableLayoutPanel that you've got to the new MyTableLayoutPanel type.
Add it to a regular Panel. Enable AutoScroll on this Panel instead of the MyTableLayoutPanel (disable it there if you haven't already).
Set the MyTableLayoutPanel AutoSize property to true and its Anchor property to Left, Right and Top.
我知道这个问题已经存在很长时间了。但可能其他人也可以从我的解决方案中受益。
诀窍在于,如果自动滚动属性为true,则禁用水平滚动条不起作用。因此,只需禁用自动滚动,禁用水平滚动条,然后再将自动滚动切换为true即可。这对我有效。
只需将以下代码添加到某个位置,例如构造函数或窗体加载事件。
tableLayoutPanel1.AutoScroll = false;
tableLayoutPanel1.HorizontalScroll.Enabled = false;
tableLayoutPanel1.AutoScroll = true;
我曾经遇到过这个问题。
很多人在Datagrid中也遇到了同样的问题。但是,对于这个问题没有确切的解决方案,你将不得不根据使用手动决定面板的尺寸。
tableLayoutPanel1.HorizontalScroll.Enabled = false;
这样做将禁用水平滚动条,但您必须手动调整表布局面板的尺寸。
另一种方法可能是在运行时计算tablelayoutpanel的可能宽度,如果大于您设置的值,则可以启用它。
tableLayoutPanel1.HorizontalScroll.Enabled = true;
if (this.TableLayoutPanel1.HorizontalScroll.Visible)
{
int newWid = this.TableLayoutPanel1.Width -
(2 * SystemInformation.VerticalScrollBarWidth);
//this.TableLayoutPanel1.Padding = new Padding(0, 0, newWid, 0);
foreach (Control ctl in this.TableLayoutPanel1.Controls)
{
ctl.Width = newWid;
}
}
你的问题是你的项目恰好与布局面板的宽度相同,因此当垂直滚动条出现时,它会稍微切入你的控件,强制水平滚动吗?如果是这样,你可以通过减小控件的宽度来考虑滚动条的可能性,或者在滚动条出现时尝试调整它们。
static MethodInfo funcSetVisibleScrollbars;
static EventHandler ehResized;
public static void DisableHorizontalScrollBar(this ScrollableControl ctrl)
{
//cache the method info
if(funcSetVisibleScrollbars == null)
{
funcSetVisibleScrollbars = typeof(ScrollableControl).GetMethod("SetVisibleScrollbars",
BindingFlags.Instance | BindingFlags.NonPublic);
}
//init the resize event handler
if(ehResized == null)
{
ehResized = (s, e) =>
{
funcSetVisibleScrollbars.Invoke(s, new object[] { false, (s as ScrollableControl).VerticalScroll.Visible });
};
}
ctrl.Resize -= ehResized;
ctrl.Resize += ehResized;
}
这些解决方案并非在所有情况下都有效;我最终需要将它们结合使用:
public void hideHorizontalScrollBar(ref TableLayoutPanel pan)
{
if (!pan.HorizontalScroll.Visible)
return;
pan.Padding = new Padding(0, 0, 0, 0);
while (!!pan.HorizontalScroll.Visible || pan.Padding.Right >= SystemInformation.VerticalScrollBarWidth * 2)
pan.Padding = new Padding(0, 0, pan.Padding.Right + 1, 0);
}
重要提示:此函数可以在窗体加载时调用一次,但也必须在布局调整事件中调用。
AutoScroll = false
。希望这能帮助到有需要的人! - Cullub