如何在Panel中禁用水平滚动条

我有一个面板(Windows窗体),我想禁用面板水平滚动条。 我试过这个:

HorizontalScroll.Enabled = false; 

但那不行。

我怎样才能做到这一点?

尝试以这种方式实现,它将100%工作

 panel.HorizontalScroll.Maximum = 0; panel.AutoScroll = false; panel.VerticalScroll.Visible = false; panel.AutoScroll = true; 

如果你想亵渎你的代码,你可以试试这个非常“hackish”的解决方案:

 [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow); private enum ScrollBarDirection { SB_HORZ = 0, SB_VERT = 1, SB_CTL = 2, SB_BOTH = 3 } protected override void WndProc(ref System.Windows.Forms.Message m) { ShowScrollBar(panel1.Handle, (int)ScrollBarDirection.SB_BOTH, false); base.WndProc(ref m); } 

我目前正在使用上面的代码来阻止第三方UserControl显示其滚动条。 他们没有暴露任何隐藏它们的正确方法。

我认为您遇到此问题是因为面板的AutoScroll属性设置为true。 我制作了一个测试解决方案(.NET 3.5)并发现了以下内容:

如果你试试这个:

 panel.AutoScroll = true; panel.HorizontalScroll.Enabled = false; panel.HorizontalScroll.Visible = false; 

Horizo​​ntalScroll.Enabled和.Visible 不会更改为false(假设面板中有控件导致autoscroll显示水平滚动条)。 您似乎必须禁用AutoScroll才能手动更改这些属性。

我遇到了同样类型的问题,当AutoScroll = true时出现水平滚动,它只出现在垂直滚动条出现时。 我终于想通了我从面板上移除了填充,并通过向右边填充添加20,它允许垂直滚动条出现而不显示水平滚动条。

托管C ++代码以隐藏HScroll Bar:

 // Hide horizontal scroll bar HWND h = static_cast (this->Handle.ToPointer()); ShowScrollBar(h, SB_HORZ, false); 

另一个解决方案,似乎是我唯一可以工作的解决方案是相当hacky:

  foreach (UserControl control in ListPanel.Controls) { if (ListPanel.VerticalScroll.Visible) control.Width = ListPanel.Width - 23; else control.Width = ListPanel.Width-5; } 

我在其OnResize事件中调用它

我正在寻找一个问题的答案,如何在需要时将滚动条添加到框中,并在不需要时将其删除。 这是我为文本框提出的解决方案,具有允许的最大宽度和高度,为显示的文本resize。

 private void txtOutput_TextChanged(object sender, EventArgs e) { // Set the scrollbars to none. If the content fits within textbox maximum size no scrollbars are needed. txtOutput.ScrollBars = ScrollBars.None; // Calculate the width and height the text will need to fit inside the box Size size = TextRenderer.MeasureText(txtOutput.Text, txtOutput.Font); // Size the box accordingly (adding a bit of extra margin cause i like it that way) txtOutput.Width = size.Width + 20; txtOutput.Height = size.Height + 30; // If the box would need to be larger than maximum size we need scrollbars // If height needed exceeds maximum add vertical scrollbar if (size.Height > txtOutput.MaximumSize.Height) { txtOutput.ScrollBars = ScrollBars.Vertical; // If it also exceeds maximum width add both scrollbars // (You can't add vertical first and then horizontal if you wan't both. You would end up with just the horizontal) if (size.Width > txtOutput.MaximumSize.Width) { txtOutput.ScrollBars = ScrollBars.Both; } } // If width needed exceeds maximum add horosontal scrollbar else if (size.Width > txtOutput.MaximumSize.Width) { txtOutput.ScrollBars = ScrollBars.Horizontal; } } 

SuperOli的答案很棒!

我更新了代码,因此可以关闭表单而不会出现任何错误。
我希望它对你也有用。

 [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow); private enum ScrollBarDirection { SB_HORZ = 0, SB_VERT = 1, SB_CTL = 2, SB_BOTH = 3 } protected override void WndProc(ref System.Windows.Forms.Message m) { if (m.Msg == 0x85) // WM_NCPAINT { ShowScrollBar(panel1.Handle, (int)ScrollBarDirection.SB_BOTH, false); } base.WndProc(ref m); }