How to disable horizontal scrollbar for table panel in winforms

前端 未结 9 1087
栀梦
栀梦 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:31

    Lost a few hairs on this today, but I solved it and this is what I ended up with:

    1. Create a new class that inherits from TableLayoutPanel (lets call it MyTableLayoutPanel), and override the MaximumSize property like this:

      public override Size MaximumSize
      {
          get
          {
              if (Parent != null)
                  return new Size(Parent.Width, 0);
              else
                  return base.MaximumSize;
          }
          set
          {
              base.MaximumSize = value;
          }
      }
      

      You could of course make it more general purpose by adding another property that decides whether or not you should return the altered MaximumSize or not, but hopefully that much is obvious to anyone that's reading this.

    2. Change the TableLayoutPanel that you've got to the new MyTableLayoutPanel type.

    3. Add it to a regular Panel. Enable AutoScroll on this Panel instead of the MyTableLayoutPanel (disable it there if you haven't already).

    4. Set the MyTableLayoutPanel AutoSize property to true and its Anchor property to Left, Right and Top.

提交回复
热议问题