【问题标题】:How to disable a Horizontal Scrollbar in C# System.Windows.Forms.Panel如何在 C# System.Windows.Forms.Panel 中禁用水平滚动条
【发布时间】:2014-07-25 09:42:53
【问题描述】:

我尝试添加上一个线程中的代码,但它对我不起作用。

How do I disable the horizontal scrollbar in a Panel

我只是想删除它,因为无论它是否需要出现在我的屏幕上,它都会显示出来。

【问题讨论】:

    标签: c# user-interface


    【解决方案1】:

    除非通过Panel.AutoScroll = falsepanel1.HorizontalScroll.Visible = true 静态设置,否则面板不会显示条形图。我建议您验证没有控件超出面板,而不是强制状态。

    将以下内容插入表单的某个部分。这将验证您没有超出面板两侧的控件。将 panel1 更改为有问题的面板的名称。

            foreach (Control comp in panel1.Controls)
            {
                if (comp.Right >= panel1.Width || comp.Bottom >= panel1.Height)
                {
                    System.Diagnostics.Debugger.Break();
                }
            }
    

    如果您仍然找不到问题,Panel.AutoScroll = falsepanel1.HorizontalScroll.Visible = false 应该可以解决问题。

    【讨论】:

      【解决方案2】:

      我发现这个解决方案是首选。

      public class MongoDataPanel : Panel
      {
      
          [DllImport("user32.dll")]
          static public extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);
          private const int SB_HORZ = 0;
          private const int SB_VERT = 1;
      
      
          protected override void OnResize(EventArgs eventargs)
          {
              base.OnResize(eventargs);
              if (this.Parent != null)
              {
                  if (this.Parent.Width > this.PreferredSize.Width - 10)
                  {
                      try
                      {
                          ShowScrollBar(this.Handle, SB_HORZ, false);
                      }
                      catch (Exception e) { }
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-26
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 2012-04-18
        • 2014-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多