Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Not able to Override text property of label in derived class
 

Not able to Override text property of label in derived class

Hi,

I am Overiding a label class and i tried overriding the text property but it did not work. Even after overrding the text property it generates a default text in the visual studi designer

class Class1Tongue Tiedystem.Windows.Forms.Label

{

private string _text = "";

public Class1()

{

base.Text = _text;

}

public override string Text

{

get

{

return base.Text;

}

set

{

base.Text = value;

}

}

}

I tried different ways but it does not work, can anyone suggest me any other way to override the text property of label control for designer

c__junkie  Tuesday, September 02, 2008 12:26 PM

This is because the Text is being set by the designer for the Label control. Label has a LabelDesigner which inherits from ControlDesigner. ControlDesigner.InitializeNewComponent sets the Text property to the Name value if the Component (your derived label) has a Text property of type string, which is not ReadOnly and IsBrowsable.

So you can set the Text property to ReadOnly or to not be Browsable.

Alternatively you can write your own designer which overrides InitializeNewComponent. In here you would first call base.InitializeNewComponent and then set the Text property to whatever you want.

Unfortunately LabelDesigner is not public, so you would have to inherit from ControlDesigner meaning you will miss out on the SnapLines and SelectionRules that LabelDesigner sets.

Hope that helps,

Martin

Martin Thorsen - MSFT  Wednesday, September 03, 2008 7:10 PM

You can't override in design mode unless you create your own custom control. Remember overriding is a run-time approach.

A.D.T.

BeforeAndAfter1974  Tuesday, September 02, 2008 12:52 PM

Yes indeed i am creating a custom control here by deriving from Label Class.

class Class1 : System.Windows.Forms.Label

c__junkie  Tuesday, September 02, 2008 1:17 PM

This is because the Text is being set by the designer for the Label control. Label has a LabelDesigner which inherits from ControlDesigner. ControlDesigner.InitializeNewComponent sets the Text property to the Name value if the Component (your derived label) has a Text property of type string, which is not ReadOnly and IsBrowsable.

So you can set the Text property to ReadOnly or to not be Browsable.

Alternatively you can write your own designer which overrides InitializeNewComponent. In here you would first call base.InitializeNewComponent and then set the Text property to whatever you want.

Unfortunately LabelDesigner is not public, so you would have to inherit from ControlDesigner meaning you will miss out on the SnapLines and SelectionRules that LabelDesigner sets.

Hope that helps,

Martin

Martin Thorsen - MSFT  Wednesday, September 03, 2008 7:10 PM

You can use google to search for other answers

Custom Search

More Threads

• PropertyDescriptor, IsBrowsable and the PropertyGrid
• How to use Toolstrip control in C#.Net?
• Create custom UI type editor for control property ?
• UserControl With Multiple Panels
• How to add Backgroung Image of panel at particuler position
• How to create table with merged cells
• How to change control 's property at run time
• How to access Method of control created at runtime?
• SmartTag with custom UIType Editor
• I want to have the first column in the ListView as a LinkLabel in Windows Forms.