2011-03-30 53 views
20

我有面板(Windows窗體),我想禁用面板水平滾動條。我試過這個:如何禁用面板中的水平滾動條

HorizontalScroll.Enabled = false; 

但那不行。

我該怎麼做?

+0

類似的問題:http://stackoverflow.com/questions/1325049/winform-panel-scrolling-without-a-scrollbar – 2011-03-30 16:43:17

回答

46

嘗試實施這種方式,它會工作100%

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

這應該是被接受的答案。沒有其他人爲我工作,但這個簡單的代碼實際上修復了它! – 2015-08-04 20:18:08

+0

@AnonymousPi嘗試了很多得到這個固定。一天浪費我實際上修復它很簡單。 – 2015-11-30 12:04:41

+0

這是一個非常棒的,簡單的答案,用於擺脫水平滾動條。我包含這個解決方案(信貸給@Kbv Subrahmanyam!)在一個更一般的解決方案,我在這裏有自動滾屏問題... http://stackoverflow.com/a/35065017/2175233 – 2016-01-28 15:13:41

13

我認爲你有這個問題,因爲面板的AutoScroll屬性設置爲true。我做了一個測試解決方案(.NET 3.5),發現了以下內容:

如果你試試這個:

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

的Horizo​​ntalScroll.Enabled和。可見更改爲false(假設面板在該自動滾動中具有控件以顯示水平滾動條)。看起來您必須禁用AutoScroll才能夠手動更改這些屬性。

+0

Visual Studio中指出,面板可是沒有HScrollProperties,你確定它確實有它? – 2011-03-30 16:45:12

+0

Blah,對不起,試試HScroll = false; – 2011-03-30 16:46:28

+0

也沒有它,即時嘗試將其從面板中刪除它 - 而不是一個窗體, – 2011-03-30 16:47:39

20

如果你覺得褻瀆你的代碼,你可以試試這個非常「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); 
} 

我目前使用上面的代碼,以防止第三方用戶控件無法顯示其滾動條。他們沒有揭露任何適當的方法來隱藏它們。

+0

以及我不認爲它用於我,我做了一個小測試並使用它,一個面板剛剛消失,另一個面板仍然有滾動條。謝謝anayway :) – 2011-03-31 11:24:39

+5

偉大的作品!雖然'WndProc'部分絕對不需要。只需調用: 'ShowScrollBar(panel1.Handle,(int)ScrollBarDirection.SB_HORZ,false);' 從任何時候你想隱藏滾動條的方法! – BrainSlugs83 2011-11-24 00:26:55

+0

它在關閉表單時顯示win 2008服務器上的錯誤! – 2012-12-07 09:40:10

0

託管C++代碼隱藏HSCROLL酒吧:

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

我是有出現橫向滾動此同一類型的問題時,自動滾屏= true,則僅表現了當垂直滾動條出現。我終於想通了,我從面板中刪除了填充,並且通過將20填充回正確的填充,它允許垂直滾動條出現並且不顯示水平滾動條。

+0

我有一個可滾動的TableLayoutPanel停靠到填充它的父容器,並且剛剛添加20px正確的填充到TableLayoutPanel工作(水平滾動條不再出現)。 – Rachel 2017-01-11 11:28:04

0

另一種解決方案,這似乎是唯一一個我能得到的工作是相當哈克:

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

其中我調用其OnResize事件

0

我一直在尋找一個問題的答案,如何在需要時將滾動條添加到框中,並在不需要時將其移除。這是我爲文本框提出的解決方案,具有最大允許的寬度和高度,爲顯示的文本調整大小。

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; 
     } 
    } 
0

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); 
}