I have a panel (Windows Forms) and I want to disable a panels horizontal scrollbar. I tried this:
HorizontalScroll.Enabled = false;
But that wouldn't work.
How can I do this?
I have a panel (Windows Forms) and I want to disable a panels horizontal scrollbar. I tried this:
HorizontalScroll.Enabled = false;
But that wouldn't work.
How can I do this?
Try to implement this way, it will work 100%
panel.HorizontalScroll.Maximum = 0;
panel.AutoScroll = false;
panel.VerticalScroll.Visible = false;
panel.AutoScroll = true;
If you feel like desecrating your code you could try this very "hackish" solution:
[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);
}
I'm currently using the code above to prevent a 3rd party UserControl from showing its scrollbars. They weren't exposing any proper ways of hiding them.
I think that you're having this problem because the AutoScroll property of your panel is set to true. I made a test solution (.NET 3.5) and discovered the following:
If you try this:
panel.AutoScroll = true;
panel.HorizontalScroll.Enabled = false;
panel.HorizontalScroll.Visible = false;
the HorizontalScroll.Enabled and .Visible aren't changed to false (assuming the panel has controls within that cause autoscroll to show the horizontal scroll bar). It seems that you must disable AutoScroll to be able to change these properties around manually.
I was having this same type of issue with the horizontal scroll appearing when AutoScroll=true, it only showed up when the vertical scrollbar appeared. I finally figured out that I removed padding from the panel and by adding 20 back to the right padding it allowed the vertical scrollbar to appear and not show the horizontal one.
SuperOli´s answer was great!
I updated the code so it is possible to close the form without any error.
I hope it works for you as well.
[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);
}
Console.BufferHeight = 44;
Set the buffer height equals to the console's window height. In my program I give the static height but you can do like this as well:
Console.BufferHeiht = (Console.WindowHight - 1);
I think it should work.
Managed C++ code to hide HScroll Bar:
// Hide horizontal scroll bar
HWND h = static_cast<HWND> (this->Handle.ToPointer());
ShowScrollBar(h, SB_HORZ, false);
Another solution, which seemed to be the only one I could get working was the rather hacky:
foreach (UserControl control in ListPanel.Controls)
{
if (ListPanel.VerticalScroll.Visible)
control.Width = ListPanel.Width - 23;
else
control.Width = ListPanel.Width-5;
}
Which I call in its OnResize event
I was looking for an answer to the question how to add a scrollbar to a box when needed and remove it when not needed. This is the solution i came up with for a textbox, with maximum allowed width and height, resized for the text displayed.
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;
}
}
I just encountered this same kind of glitch with a scrollable UserControl
in a form when switching from scrolling mode to zoom-to-fit mode. For the latter mode, both scrollbars should be off. Most of the time this change of mode works perfectly, except when either or both scrollbars are at their maximum value, causing one or other of the scrollbars to remain visible but disabled.
Which of the scrollbars remains visible seems to be random, so there must be some kind of race condition in the Windows code.
Curiously, the ClientRectangle
reported by Windows is the full rectangle of the UserControl
without any scrollbars. This means Windows thinks that the scrollbars are not visible, yet it still randomly displays one or other disabled scrollbar.
I solved it by moving the AutoScrollPosition to (0, 0) before performing the switch to zoom-to-fit mode.
AutoScrollPosition = new Point(0, 0); // Bug fix to prevent one or other of the scrollbars randomly remaining visible when switching to zoom-to-fit mode when the scrollbars are at their maximum values.
zoom_factor = CalcZoomFactorBestFit();
SetScrollSizes();
Invalidate();
Following the code proposed by @Guesting, I have written a custom class for the panel control for enabling the double buffer option and also with possibility of enabling / disabling each scrollbar.
Public Class PanelDoubleBuffer
Inherits Panel
'MAIN LAYOUT design scheme
Public Property ShowVerticalScrolBar As Boolean = False
Public Property ShowHorizontalScrolBar As Boolean = False
Public Sub New()
SuspendLayout()
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
SetStyle(ControlStyles.ResizeRedraw, True)
Me.UpdateStyles()
ResumeLayout()
End Sub
<DllImport("user32.dll")>
Private Shared Function ShowScrollBar(ByVal hWnd As IntPtr, ByVal wBar As Integer, ByVal bShow As Boolean) As Boolean
End Function
Public Property SB_HORZ As Integer = ShowHorizontalScrolBar
Public Property SB_VERT As Integer = ShowVerticalScrolBar
Public Property SB_CTL As Integer = 2
Public Property SB_BOTH As Integer = 3
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = &H85 Then
ShowScrollBar(Me.Handle, CInt(SB_BOTH), False)
End If
MyBase.WndProc(m)
End Sub
End Class