Problem with Inheriting from Controls in WinForms (C#)

  • Thread starter Thread starter J Carey
  • Start date Start date
J

J Carey

I'm trying to create a custom control by inheriting from an existing
one. My actual situation is a little more complex but is similar to
this:

1) You put a Button on a form and in the InitializeComponent() for the
form the only code it really adds is the location, tabindex, name, and
text. It has several properties it doesn't throw in there, such as
the BackColor. I'm assuming that this is because it just uses the
default value. If I'm wrong correct me.

2) Given this, let's say I want to create a control called RedButton
that inherits from Button. In my inherited class I set the BackColor
to Color.Red and compile it. When I go to put this new RedButton in a
form, I don't want to see its BackColor being set in the
InitializeComponent() of the form it belongs to. Is there any way to
do this?

Thanks for any advice,
Jeff
 
It sounds like you want to create a new control inherited from button
I dont think button is sealed (Double Check) so you can add a usercontrol
and instead of inheriting from controlbase you can inherit from button.
Then you can save your usercontrol as RedButton and add it to any form you
want.

HTH
JB
 
I believe theres an attribute you can set (Browseable???) that can
prevent the ide from seeing your property.
 
If you look at my point #2, I am already inheriting from Button. What
I'm looking for is this:

1. Set the BackColor to Red in the control.
2. What I want: When I create an instance of this control on a Form
I don't want to see it's backcolor being set in that form's
InitializeComponent(), since this should have already taken place
inside the inherited button's InitializeComponent().
 
If you are trying to avoid having the line appear in InitializeComponent, it
sounds as though the real issue might be that you do not want the BackColor
to be visible at design time. Is that the case?

There are two ways to do this. I'll go over both, but I think that only one
is suitable for your needs.

1. If you are defining the property, you can decorate it with the Browsable
attribute:
[System.ComponentModel.Browsable(false)]

This will work if you define the property or if the property is virtual (so
that you can override it and decorate it). Since the BackColor example is a
contrived example, maybe this will work in your real implementation, not
sure

2. In the case of BackColor, I'm pretty sure that it is not virtual, so you
can't override it. In that case, you need to create a custom designer for
your button, and in the custom designer, you can filter the properties that
are displayed by the IDE (and that should, in turn, result in suppressing
the serialization of the Color.Red assignment in InitializeComponent, but
I'm not positive, as I haven't tested).

Creating the Designer is pretty trivial in this case:

public class MyButtonDesigner: System.Windows.Forms.Design.ControlDesigner
{
protected override void
PostFilterProperties(System.Collections.IDictionary properties)
{
// hide the BackColor property by removing it from the properties
dictionary
properties.Remove("BackColor");
}
}


Now, you just need to associate your button class with the designer. You do
that with the System.ComponentModel.Designer attribute:

[Designer(typeof(MyButtonDesigner))]
public class MyButton : Button
{ /* your code */ }
 
Back
Top