How To Hide Control Attributes In Visual Studio 2005
Why anyone would want to do that is not the point. But actually, it's pretty easy to hide attributes (i.e. properties and events) of any Control in Visual Studio. The same thing also works for Forms, but there it's a little different.
***
This code snippet shows how it is done for System.Form.Control:
public class ButtonAttributes : System.Windows.Forms.Design.ControlDesigner {
protected override void PreFilterProperties( System.Collections.IDictionary properties ) {
properties.Remove( "Tag" );
base.PreFilterProperties( properties );
}
protected override void PreFilterEvents( System.Collections.IDictionary events ) {
events.Remove( "MouseClick" );
base.PreFilterEvents( events );
}
}
[System.ComponentModel.Designer( typeof( ButtonAttributes ) )]
// doubleclick will open code editor instead of designer
[System.ComponentModel.DesignerCategory( "Code" )]
public class SimpleButton : System.Windows.Forms.Button {
}
Now, whenever you need a button with neither a MouseClick event nor a Tag, you can use an instance of MyButton. Note that the attributes are only hidden in the designer. In code, they can still be accessed.
***
Doing the same thing for Forms is a little more tricky. You must derive your own subclass, remove the attributes and derive from that subclass again. Here's the code:
public class HiddenFormAttributes : DocumentDesigner {
public override DesignerVerbCollection Verbs {
get {
base.Verbs.Add( new DesignerVerb( "Click Me", delegate { System.Windows.Forms.MessageBox.Show( "Hello world! ;-)" ); } ) );
return base.Verbs;
}
}
protected override void PreFilterProperties( System.Collections.IDictionary properties ) {
properties.Remove( "Tag" );
base.PreFilterProperties( properties );
}
protected override void PostFilterEvents( System.Collections.IDictionary events ) {
events.Remove( "MouseClick" );
base.PostFilterEvents( events );
}
}
[System.ComponentModel.Designer( typeof( HiddenFormAttributes ), typeof( System.ComponentModel.Design.IRootDesigner ) )]
[System.ComponentModel.DesignerCategory( "Form" )]
public partial class ModalDialog : System.Windows.Forms.Form {
public ModalDialog() {
InitializeComponent();
}
}
Any instance of the ModalDialog class will still have a Tag and a MouseClick event in the designer. You need to derive a subclass to get rid of it. The good thing is that you can set the properties of ModalDialog in the designer.
The verb is just another cool thing the designer can do.

3 comments:
Now that's interesting! Doing a websearch for "hide attribute from designer" yields Marc's Blog on P3, being the first result really "on topic"!
Now I should call you "Sir Marc", shouldn't I?
(Although I have to admit that the post is rather old. Back then you were still here with us. So it's some kind of late answer for a question I should have asked when you were sitting only three feet away from me *grin*)
Lord Hoschi!
Pleased to hear though foundst it useful.
Kind Regards,
Sir Marc ;-)
Oh great, I just introduced the only Google occurrence of "though foundst". It should have been "thou foundst", I guess ^^;
Post a Comment