Default designer values of base class control properties

F

Fred Iannon

I am developing an inherited control (i.e. subclassed) and
I am having trouble figuring out how to set the default
values for BASE CLASS properties which appear in the
designer.

In other words with this definition:
public class MyTextBox :
System.Windows.Forms.TextBox

is there a way to make MyTextBox have the following
DEFAULT values in designer mode:
Enbaled = false;
Size = new Size(10, 15);
TabStop = false;

Typically, I would like to be allow the user to be able
change these value, however I want these to be the
defaults.

Furthermore, is there a way to hide a BASE class property
in the designer mode (so the user canNOT change it in
designer more, only in code??), or to at least make the
value NOT editable in designer mode (but still display
it)??

I know how to do all of this with my own properties in the
new control, however I canNOT figure out how to change
attributes on base classes properties....one idea was
placing code in the classes CTOR BEFORE the call to
InitializeComponenet, but that does not seem to work....

Thanks for any help,
Fred Iannon
 
C

Claes Bergefall

Override the property that you want to change the attributes for
and apply the DefaultValue attribute to change the default value
and the Browsable attribute to hide it in the designer

For the DefaultValue attribute you'll also need to provide
the new default values to the base class in your constructor

This works well for most of the properties, however I didn't
get it to work for the Enabled property. Eventhough I set
it to false in the constructor and specified false in the DefaultValue
attribute it still came up as true in the designer (in bold). Go figure??!

BTW, the height of a textbox must be 20 unless multiline is true, so
Size(10,15) won't work as default value

/claes
 
F

Fred Iannon

were you able to get this to work with the TabStop
property?? I canNOT override this property, and if I try
to hide the base class impl (via "new") it does not seem
to show up correctly in designer??

IF you do override the base class versions of the
properties should you just call the base class versions in
the get/set methods?

Where should these values be set in the CTOR....BEFORE the
call to InitializeComponent or after??? The placement
seems to effect whether the user entered values are
overwritten or not

Thanks for any help,
F
 
C

Claes Bergefall

Fred Iannon said:
were you able to get this to work with the TabStop
property?? I canNOT override this property, and if I try
to hide the base class impl (via "new") it does not seem
to show up correctly in designer??

I hid the base class impl. (via "new") and it worked just
fine in the designer. You do need to set it to false
in the constructor though. Like this:

public class TextBoxEx : System.Windows.Forms.TextBox
{
public TextBoxEx()
{
base.TabStop = false;
}
[DefaultValue(false)]
public new bool TabStop
{
get
{
return base.TabStop;
}
set
{
base.TabStop = value;
}
}
}
IF you do override the base class versions of the
properties should you just call the base class versions in
the get/set methods?
Yes.


Where should these values be set in the CTOR....BEFORE the
call to InitializeComponent or after??? The placement
seems to effect whether the user entered values are
overwritten or not

The constructor to use is the one in your inherited class
(see sample above). Do you really have an
InitializeComponent in that?


/claes
 
K

Karen Martinez

You can also make your own custom designer to set the defaults. Here's an
example of a custom textbox (called TextBoxIt) with a designer that sets the
default text to "" and the default width to 90:

[
Designer(typeof(TextBoxIt.TextBoxItDesigner))
]
public class TextBoxIt : System.Windows.Forms.TextBox
{
public TextBoxIt()
{
}

public class TextBoxItDesigner :
System.Windows.Forms.Design.ControlDesigner
{
public override void OnSetComponentDefaults()
{
base.OnSetComponentDefaults ()
base.Control.Text = "";
base.Control.Width = 90;
}
}
}


Claes Bergefall said:
Fred Iannon said:
were you able to get this to work with the TabStop
property?? I canNOT override this property, and if I try
to hide the base class impl (via "new") it does not seem
to show up correctly in designer??

I hid the base class impl. (via "new") and it worked just
fine in the designer. You do need to set it to false
in the constructor though. Like this:

public class TextBoxEx : System.Windows.Forms.TextBox
{
public TextBoxEx()
{
base.TabStop = false;
}
[DefaultValue(false)]
public new bool TabStop
{
get
{
return base.TabStop;
}
set
{
base.TabStop = value;
}
}
}
IF you do override the base class versions of the
properties should you just call the base class versions in
the get/set methods?
Yes.


Where should these values be set in the CTOR....BEFORE the
call to InitializeComponent or after??? The placement
seems to effect whether the user entered values are
overwritten or not

The constructor to use is the one in your inherited class
(see sample above). Do you really have an
InitializeComponent in that?


/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top