Override AutoSize for derived Label Control in C#

守給你的承諾、 提交于 2019-12-02 11:36:18

I have not done this before, I believe you can theoretically override a property declaration (via the new keyword) and check the orientation before proceeding:

override public bool AutoSize
{
   set 
   {
      if( /* orientation is horizontal */ )
      {
          base.AutoSize = value;
      }
      else
      {
          // do what you need to do
      }    
   }    
}

If think a solution is to override OnResize itself :

protected override void OnResize(EventArgs e)
{
    if (AutoSize)
    {
        // Perform your own resizing logic
    }
    else
        OnResize(e);
}

I know this a a pretty old question, but i stumbled across it today and was wondering how to do the same thing.

My solution to the problem was overriding the GetPreferredSize(Size proposedSize) method. I used a button class that houses an arrow in addition to the text which, of course, was not taken into account using the AutoSize property so i added additional space and it works fine for me.

Given the problem of changing orientation or switching width and height, you could completely change the way the preferred size is calculated.

public override Size GetPreferredSize(Size proposedSize)
{
    Size s = base.GetPreferredSize(proposedSize);
    if (AutoSize)
    {
        s.Width += 15;
    }
    return s;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!