How to disable horizontal scrollbar for table panel in winforms

前端 未结 9 1083
栀梦
栀梦 2020-12-10 11:54

Hi I\'ve a tablelayoutpanel and I\'m binding controls to it dynamically. When the item count exceeds the height of panel obviously vertical scroll bar appearing there is no

相关标签:
9条回答
  • 2020-12-10 12:35

    I have experienced this problem.

    A lot of people also get same problem in Datagrid. But, there is no exact solution for this question you will have to manually decide the the dimension of the panel according to use.

    tableLayoutPanel1.HorizontalScroll.Enabled = false;
    

    this will disable the horizontal scroll bar but you will have to manually adjust the dimension of the table layout panel.

    Another way could be calculating the possible width of tablelayoutpanel during run time and if it greater than the value you have set then you can enable it.

    tableLayoutPanel1.HorizontalScroll.Enabled = true;
    
    0 讨论(0)
  • 2020-12-10 12:36

    This worked perfectly in .NET 3.5 where the other solutions did not give me exactly what I wanted:

      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;
        }
      }
    
    0 讨论(0)
  • 2020-12-10 12:38

    I found a perfect solution about this problem by using reflect. You can try following codes:

    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;
    }
    
    0 讨论(0)
提交回复
热议问题