UserControl Font Property Hiding

UserControl Font Property Hiding

Post by Evan Ston » Fri, 02 Sep 2005 07:37:09


Is it possible to hide or mask the Font property on a UserControl?

I'd prefer to show a couple of specialized font settings instead, in
lieu of displaying a generic "Font" property.

I didn't know if this was an ambient property or whatever, and one that
I couldn't get rid of even if I tried...

Thanks in advance.

-Evan
 
 
 

UserControl Font Property Hiding

Post by Tim Wilso » Sat, 03 Sep 2005 01:11:54

You can hide the Font property from the Properties window by overriding it
and specifying the "BrowsableAttribute" attribute:

[BrowsableAttribute(false)]
public override Font Font
{
...
}


--
Tim Wilson
.Net Compact Framework MVP

 
 
 

UserControl Font Property Hiding

Post by Evan Ston » Sun, 04 Sep 2005 07:04:45

Hi Tim,

Thanks for the suggestion. Unfortunately, the property is still showing
up in the properties inspector window, so I don't know what's going on
at this point.

So far I've tried the following, after which the property still was
visible, so I'm still looking for suggestions:

1. Evan's version 1:

[Browsable(false)]
private new Font Font
{
//get{}
set{}
}

2. Evan's version 2

[Browsable(false)]
public override Font Font
{
//get{}
set{}
}

3. Tim's version:

[BrowsableAttribute(false)]
public override Font Font
{
//get{}
set{}
}

...all three resulted in the same thing. The Font property was still
visible in the Properties window.

The following version didn't compile, because you can't change access
levels with override:

// note that this is private instead of public
[Browsable(false)]
private override Font Font
{
//get{}
set{}
}

So, at this juncture, any further suggestions/recommendations would be
greatly appreciated. I'm sure I'm doing something goofy, but I just
can't figure out what... :(

Thanks!

-Evan
 
 
 

UserControl Font Property Hiding

Post by Tim Wilso » Sun, 04 Sep 2005 07:59:12

Try the following simple UserControl. This removes the Font property from
the Properties window when I compile and add it to the toolbox and drop it
onto a form.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace CustomUserControl
{
public class UserControl1 : System.Windows.Forms.UserControl
{
public UserControl1() { }

[BrowsableAttribute(false)]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
}
}
}

--
Tim Wilson
.Net Compact Framework MVP
 
 
 

UserControl Font Property Hiding

Post by Evan Ston » Sun, 04 Sep 2005 08:27:25

Hey Tim,

Great news! I went ahead and added your code snippet to my existing
UserControl and the Font property went bye-bye in the list, and I think
it may have to do with the fact that you supplied the get{} whereas I
did not (perhaps it's looking at the base UserControl class for that,
finding it and then publishing the property anyway?).

To illustrate, if I comment out the get{} like so:

[BrowsableAttribute(false)]
public override Font Font
{
/*
get
{
return base.Font;
}
*/
set
{
base.Font = value;
}
}

...the Font property becomes _visible_ again. Remove the comment, the
Font property disappears. Additionally - just for the record -
[Browsable(false)] works too, which is actually the original solution I
found in MSDN.

And here's another interesting thing - changing "public override" to
"private new" did not hide the property either, so there must be some
weird behavior going on behind the scenes with the IDE.

At any rate, the last code bit you posted worked great, so I think I'm
good to go.

Thanks!

:)

-Evan
 
 
 

UserControl Font Property Hiding

Post by Tim Wilso » Sun, 04 Sep 2005 09:16:06

Yeah, I would assume that you'd need to supply the "getter." I usually
override the entire property (both get and set). As for the
"[Browsable(false)]" working too, any attribute can also be used without the
word "Attribute" at the end, this is a shortcut. So, to the compiler, the
following are exactly the same:

[BrowsableAttribute(false)]
public override Font Font
{
...
}

[Browsable(false)]
public override Font Font
{
...
}

It's just my preference to explicitly specify the word "Attribute", since
after all that is the proper class name of the type.

--
Tim Wilson
.Net Compact Framework MVP