Prevent Designer Generated Code

S

steve

I've created a button class (MyButton) that derives from
System.Windows.Forms.Button.
In the constructor, I set the ForeColor and BackColor properties of the
button with a static color like this:

this.ForeColor = StaticForeColor
this.BackColor = StaticBackColor

It's really important that the Designer Generated Code doesn't set the
ForeColor and BackColor at design time. Is there a way to prevent the
Designer Generated Code from doing this?
Let me explain why. My idea behind creating a class this way, is so I can
set the colors of the button for the whole class dynamically in my project
when the project is loaded like this:

MyButton.StaticForeColor = Color.Blue;

The problem is, the Designer Generated Code automatically puts in lines
like this:

this.MyButton1 = new MyNameSpace.MyButton();

//
// MyButton1
//
...
this.btnBuildingView.BackColor = System.Drawing.Color.Black;
this.btnBuildingView.ForeColor =
System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(102)),
((System.Byte)(255)));

And even though the button class (MyButton) the StaticForeColor is not
initialized, it's just:

public static Color StaticForeColor;

The Designer Still generates code for the ForeColor and BackColor in the
//
// MyButton1
//
section because I set those values in the constructor.

So, during runtime, no matter what I change the StaticForeColor to, the
button will have it's ForeColor set to the StaticForeColor but immediately
afterwards, it will be reset to:

this.btnBuildingView.ForeColor =
System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(102)),
((System.Byte)(255)));

or something like it.

Thanks
Steve
 
O

Opher Shachar

In your class (MyButton) you can override the 'set' function of the
ForeColor & BackColor properties somthing like:
public override Color BackColor {
set {}
}
and in your constructor use 'base' to set the color:
base.ForeColor = StaticForeColor
base.BackColor = StaticBackColor
This way the user of your control can't change the color in code.
 
S

steve

Sijin

I took a look at this article:
http://msdn.microsoft.com/library/en-us/dndotnet/html/custcodegen.asp?frame=true

Thanks for your input.

Never new about Custom Code Generation and the
DesignerSerializationVisiblity.Hidden Attribute. That would probably work
fine, but I figured out that my solution worked fine. Because the DLL was a
separate project from the project I was using it in, I think I just got an
earlier version which didn't work. Briefly I'll show how it works:

public class MyButton: System.Windows.Forms.Button
{
private Color m_foreColor;
private Color m_backColor;

public static Color StaticForeColor;
public static Color StaticBackColor;

public MyButton()
{
m_foreColor = StaticForeColor;
m_backColor = StaticBackColor;

if (!m_foreColor.IsEmpty)
this.ForeColor = m_foreColor;
if (!m_backColor.IsEmpty)
this.BackColor = m_backColor;
}
}

So when I add a MyButton to a Form, the IDE creates the Designer Generated
Code, it sees that m_foreColor indeed IsEmpty, and doesn't put in a line
like:

this.btnBuildingView.ForeColor = Color.whatever.

But, in my application, that has buttons (of the MyButton class) on a Form,
I'll write a statement like:
MyButton.StaticForeColor = Color.Red;
MyButton.StaticBackColor = Color.Blue;
now, all of my buttons of type MyButton will have a Red forcolor and a Blue
backcolor. The MyButton Class becomes much more dynamic this way.

Steve
 

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