1

I am trying to add a new property to existing label .NET control, like AutoSize(existing label property),

something like IsWordWrap(new custom property)=true. (so that the words can auto wrap)

Any thoughts? so that i can give LabelName.IsWordWrap=true;

Textbox has wordwrap propery, is there any way i can inherit that to label, by any means?

Ed S.
  • 122,712
  • 22
  • 185
  • 265
Sharpeye500
  • 8,775
  • 25
  • 95
  • 143

2 Answers2

5

You would derive a new class from Label and add the logic you require. It would be much easier to simply style a TextBox to look like a label though.

using System.Windows.Forms;
// ...

class WrappingLabel : Label
{
    private bool _isWordWrap
    public bool IsWordWrap
    {
        get { return _isWordWrap; }
        set 
        {
            if( _isWordWrap != value )
            {
                _isWordWrap = value;                    
                FormatText( value );
            }
        }
    }

    private void FormatText( bool wrapped )
    {
        // logic to wrap or un-wrap text goes here.
        // you will need to call this when the text changes as well.
    }
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Yes i agree with you on textbox, but can you please show a quick sample if you have for that derivation of new class and how to pin up the new property? – Sharpeye500 Mar 01 '12 at 01:33
  • 1
    @Sharpeye500: Added an example. I didn't actually implement the wrapping logic as I don't have time for that. Still though... just use a TextBox. You are wasting your time if you can get the look and behavior you want using a standard control (and you can). – Ed S. Mar 01 '12 at 01:35
1

You could also check out the solution listed here (there are a couple of other ones as well).

Community
  • 1
  • 1
Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55