Composite Control font problem!

P

pnp

I have created a coposite user control of a text box and a label. I have
exposed the font of the textbox property through a property of the
control but every time I try to change that through the property grid of
the designer when I use the composite control in a form, after a rebuild
the changes seem to get lost! I don't see the Font property anywhere in
the inner code... What am I doing wrong?
 
T

Tim Wilson

Can you post the code for the custom property that exposes the inner TextBox
Font property?
 
P

pnp

Tim said:
Can you post the code for the custom property that exposes the inner TextBox
Font property?


[Category("InputBox"), Browsable(true),
RefreshProperties(RefreshProperties.None)]
public override Font Font
{
get
{
return this.textbox.Font ;
}
set
{
this.textbox.Font = value;
}
}
 
T

Tim Wilson

You should be blowing the stack in your get accessor. When the child TextBox
Font property is queried it will attempt to return the Parent Font property
value - this is due to the Font property being an ambient property. So when
you get the TextBox Font property from within the Parent Font property, the
child attempts to get the parents value, but the parent is getting the
childs value -- stack overflow. Another way to do this is to set the Label
Font in the constructor.

public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
this.label1.Font = this.Font;
}

Now, since the Label Font has been explicitly set, the Label will not pick
up the Font of the parent and so the Font property will affect the TextBox
only.

If you want to override the attributes for the Font property, just make sure
that you're using the base Font to get and set.

[Category("InputBox")]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
}

--
Tim Wilson
..Net Compact Framework MVP

pnp said:
Tim said:
Can you post the code for the custom property that exposes the inner TextBox
Font property?


[Category("InputBox"), Browsable(true),
RefreshProperties(RefreshProperties.None)]
public override Font Font
{
get
{
return this.textbox.Font ;
}
set
{
this.textbox.Font = value;
}
}
 

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