Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2004

Tip: Looking for answers? Try searching our database.

Prevent Designer Generated Code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
steve - 03 Sep 2004 21:00 GMT
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
Opher Shachar - 04 Sep 2004 05:15 GMT
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.

> I've created a button class (MyButton) that derives from
> System.Windows.Forms.Button.
[quoted text clipped - 50 lines]
> Thanks
> Steve
Sijin Joseph - 04 Sep 2004 08:07 GMT
You might want to take a look at this
http://msdn.microsoft.com/library/en-us/dndotnet/html/custcodegen.asp?frame=true

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

> I've created a button class (MyButton) that derives from
> System.Windows.Forms.Button.
[quoted text clipped - 50 lines]
> Thanks
> Steve
steve - 09 Sep 2004 21:01 GMT
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

> You might want to take a look at this

http://msdn.microsoft.com/library/en-us/dndotnet/html/custcodegen.asp?frame=true

> Sijin Joseph
> http://www.indiangeek.net
[quoted text clipped - 54 lines]
> > Thanks
> > Steve

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.