Search code examples
c#autosize

C# AutoSize Label Issue


In my program I have label controls which users can add at will, the labels can be bound to data source too. I added the AutoSize property to a property grid I designed and there is the option to turn this on and off at will. The problem I am running into is that if the AutoSize flag is set to true in the property grid it will AutoSize to the current cell contents which is correct but if I move ahead one record in my data source and the text is longer then the label the label will not resize again. Am I doing something wrong or this the way AutoSize works?

Also does anyone know if it possible to make snap lines available at run time to align controls?

Thanks.


Solution

  • if I'm understanding your question correctly you're creating a custom component "label" and the problem is that is doesn't adjust its size when its text is changed. Take a look at the Control.OnTextChanged method, you can put your size adjusting logic there. Smth like this:

    public class TestLabel : Control
    {
      protected override void OnTextChanged(EventArgs e)
      {
       // adjust size here
       base.OnTextChanged(e);
      }
    }
    

    as for snap lines, those are drown by designer, in run time you can draw them yourself by overriding your form or parent control OnPaint method