Subclassing question

M

Michael Rodriguez

How can you subclass a control and override just a sub-property like
Font.Size?

I was trying to do something like this:

namespace Compeat.Common.Controls
{
public class MyTextBox : TextBox
{
public MyTextBox()
{
this.Font.Size = 12;
}

[DefaultValue(12)]
public override float Font.Size
{
get { return base.Font.Size; }
set { base.Font.Size = value; }
}
}
}

TIA,

Mike Rodriguez
 
T

Tim Wilson

AFAIK, you can't. You'll need to override the entire Font property and pick
up the other values from base.Font.
 
M

Michael Rodriguez

Hi Tim,

I could do that, but how would I then set a default Font value? The
DefaultValue attribute seems to only want to accept static values.

Thanks,

Mike


Tim Wilson said:
AFAIK, you can't. You'll need to override the entire Font property and
pick
up the other values from base.Font.

--
Tim Wilson
.NET Compact Framework MVP

Michael Rodriguez said:
How can you subclass a control and override just a sub-property like
Font.Size?

I was trying to do something like this:

namespace Compeat.Common.Controls
{
public class MyTextBox : TextBox
{
public MyTextBox()
{
this.Font.Size = 12;
}

[DefaultValue(12)]
public override float Font.Size
{
get { return base.Font.Size; }
set { base.Font.Size = value; }
}
}
}

TIA,

Mike Rodriguez
 
T

Tim Wilson

There are a couple ways to do this. Let's assume, for example, that you
wanted to have a default value of Microsoft Sans Serif, 12 pt, Bold. Then
you could do something like this.

private Font _font = new Font("Microsoft Sans Serif", 12.0F,
FontStyle.Bold);

[DefaultValue(typeof(Font), "Microsoft Sans Serif, 12pt, style=Bold")]
public override Font Font
{
get
{
return _font;
}
set
{
_font = value;
}
}

If you could not get the DefaultValue attribute to work in some situation,
then you could always use the ShouldSerializeXXX and ResetXXX methods.
http://msdn.microsoft.com/library/d...guide/html/cpconshouldpersistresetmethods.asp

--
Tim Wilson
..NET Compact Framework MVP

Michael Rodriguez said:
Hi Tim,

I could do that, but how would I then set a default Font value? The
DefaultValue attribute seems to only want to accept static values.

Thanks,

Mike


Tim Wilson said:
AFAIK, you can't. You'll need to override the entire Font property and
pick
up the other values from base.Font.

--
Tim Wilson
.NET Compact Framework MVP

Michael Rodriguez said:
How can you subclass a control and override just a sub-property like
Font.Size?

I was trying to do something like this:

namespace Compeat.Common.Controls
{
public class MyTextBox : TextBox
{
public MyTextBox()
{
this.Font.Size = 12;
}

[DefaultValue(12)]
public override float Font.Size
{
get { return base.Font.Size; }
set { base.Font.Size = value; }
}
}
}

TIA,

Mike Rodriguez
 
M

Michael Rodriguez

Thanks!!

Mike


Tim Wilson said:
There are a couple ways to do this. Let's assume, for example, that you
wanted to have a default value of Microsoft Sans Serif, 12 pt, Bold. Then
you could do something like this.

private Font _font = new Font("Microsoft Sans Serif", 12.0F,
FontStyle.Bold);

[DefaultValue(typeof(Font), "Microsoft Sans Serif, 12pt, style=Bold")]
public override Font Font
{
get
{
return _font;
}
set
{
_font = value;
}
}

If you could not get the DefaultValue attribute to work in some situation,
then you could always use the ShouldSerializeXXX and ResetXXX methods.
http://msdn.microsoft.com/library/d...guide/html/cpconshouldpersistresetmethods.asp

--
Tim Wilson
.NET Compact Framework MVP

Michael Rodriguez said:
Hi Tim,

I could do that, but how would I then set a default Font value? The
DefaultValue attribute seems to only want to accept static values.

Thanks,

Mike


"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in
message
AFAIK, you can't. You'll need to override the entire Font property and
pick
up the other values from base.Font.

--
Tim Wilson
.NET Compact Framework MVP

How can you subclass a control and override just a sub-property like
Font.Size?

I was trying to do something like this:

namespace Compeat.Common.Controls
{
public class MyTextBox : TextBox
{
public MyTextBox()
{
this.Font.Size = 12;
}

[DefaultValue(12)]
public override float Font.Size
{
get { return base.Font.Size; }
set { base.Font.Size = value; }
}
}
}

TIA,

Mike Rodriguez
 

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