How to disable horizontal scrollbar for table panel in winforms

前端 未结 9 1104
栀梦
栀梦 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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;
    }
    

提交回复
热议问题