Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to prevent the IDE from generating code in your .designer.vb file?

Hi guys/gals :WAVE:

I'm a relative newbie to .NET developing (9 months) and I've given up figuring out everything on my own :) I need help from the gurus hehe

I was wondering if it's possible to prevent the VB IDE from generating those "default value" entries in the form1.designer.vb. The code below shows a bunch of properties that the IDE keeps generating code for in the form in which I instantiated it. Ex:

Me.btnCancel.GradientBorderPadding = 3.0!
Me.btnCancel.GradientColor1 = System.Drawing.SystemColors.Window
Me.btnCancel.GradientColor1Transparency = CType(128, Byte)
Me.btnCancel.GradientColor2 = System.Drawing.SystemColors.GradientActiveCaption
Me.btnCancel.GradientColor2Transparency = CType(128, Byte)
Me.btnCancel.GradientEnable = False
Me.btnCancel.GradientTextBorderPadding = 5.0!
Me.btnCancel.GradientTextColor = System.Drawing.SystemColors.ControlText
Me.btnCancel.GradientType = System.Drawing.Drawing2D.LinearGradientMode.Vertical

But I DON'T want it to generate the code specially when it's only repeating the default values that I already specified in the base class. Is this possible to disable?

Thanks

Base Class Button code follows:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows

<Drawing.ToolboxBitmap(GetType(System.Windows.Forms.Button))> _
Public Class Button
Inherits System.Windows.Forms.Button

'MDK 3/29/06 Generic BillManager specific property
Private strBillManagerComment As String = ""
<ComponentModel.Category("BillManager"), _
ComponentModel.Description("Use this property for anything BillManager related or for just notation for fellow developers. Example: BillManager RULES!"), ComponentModel.DefaultValue("")> _
Public Property BillManagerComment() As String
Get
Return strBillManagerComment
End Get
Set(ByVal value As String)
strBillManagerComment = value
End Set
End Property

Sub New()
MyBase.New()
InitializeComponent()
End Sub

Private Sub InitializeComponent()
'MDK 3/29/06
'Empty till a property is set manually via code or via the properties window
End Sub

#Region "Gradient" 'MDK 11/15/2006

Private m_OriginalForeColor As Color = Me.ForeColor

Private m_GradientEnable As Boolean = False
<ComponentModel.Category("BillManager"), _
ComponentModel.Description("Gets or sets if the Gradient is on or off."), ComponentModel.DefaultValue(False)> _
Public Property GradientEnable() As Boolean
Get
Return m_GradientEnable
End Get
Set(ByVal value As Boolean)
m_GradientEnable = value
If value = True Then
m_OriginalForeColor = Me.ForeColor
Me.ForeColor = Color.Transparent
Else
Me.ForeColor = m_OriginalForeColor
End If
Me.Refresh()
End Set
End Property

Private m_GradientType As LinearGradientMode = LinearGradientMode.Vertical
<ComponentModel.Category("BillManager"), _
ComponentModel.Description("Gets or sets the direction of the Gradient")> _
Public Property GradientType() As LinearGradientMode
Get
Return m_GradientType
End Get
Set(ByVal value As LinearGradientMode)
m_GradientType = value
Me.Refresh()
End Set
End Property

Private m_GradientBorderPadding As Single = 3
<ComponentModel.Category("BillManager"), _
ComponentModel.Description("Gets or sets the number of pixels between the gradient boundary and the control client area")> _
Public Property GradientBorderPadding() As Single
Get
Return m_GradientBorderPadding
End Get
Set(ByVal value As Single)
m_GradientBorderPadding = value
Me.Refresh()
End Set
End Property

Private m_GradientColor1 As Color = SystemColors.Window
<ComponentModel.Category("BillManager"), _
ComponentModel.Description("Gets or sets the color of one end of the gradient.")> _
Public Property GradientColor1() As Color
Get
Return m_GradientColor1
End Get
Set(ByVal value As Color)
m_GradientColor1 = value
Me.Refresh()
End Set
End Property

Private m_GradientColor2 As Color = SystemColors.GradientActiveCaption
<ComponentModel.Category("BillManager"), _
ComponentModel.Description("Gets or sets the color of one end of the gradient.")> _
Public Property GradientColor2() As Color
Get
Return m_GradientColor2
End Get
Set(ByVal value As Color)
m_GradientColor2 = value
Me.Refresh()
End Set
End Property

Private m_GradientColor1Transparency As Byte = 128
<ComponentModel.Category("BillManager"), _
ComponentModel.Description("Gets or sets the amount of visibility of the first color. (0=transparent, 255=opaque)")> _
Public Property GradientColor1Transparency() As Byte
Get
Return m_GradientColor1Transparency
End Get
Set(ByVal value As Byte)
m_GradientColor1Transparency = value
Me.Refresh()
End Set
End Property

Private m_GradientColor2Transparency As Byte = 128
<ComponentModel.Category("BillManager"), _
ComponentModel.Description("Gets or sets the amount of visibility of the second color. (0=transparent, 255=opaque)")> _
Public Property GradientColor2Transparency() As Byte
Get
Return m_GradientColor2Transparency
End Get
Set(ByVal value As Byte)
m_GradientColor2Transparency = value
Me.Refresh()
End Set
End Property

Private m_GradientTextBorderPadding As Single = 5
<ComponentModel.Category("BillManager"), _
ComponentModel.Description("Gets or sets the number of pixels between the text boundary and the control client area")> _
Public Property GradientTextBorderPadding() As Single
Get
Return m_GradientTextBorderPadding
End Get
Set(ByVal value As Single)
m_GradientTextBorderPadding = value
Me.Refresh()
End Set
End Property

Private m_GradientTextColor As Color = Me.ForeColor
<ComponentModel.Category("BillManager"), _
ComponentModel.Description("Gets or sets the color of the text inside the control.")> _
Public Property GradientTextColor() As Color
Get
Return m_GradientTextColor
End Get
Set(ByVal value As Color)
m_GradientTextColor = value
Me.Refresh()
End Set
End Property
[7380 byte] By [MikeDeKeyser] at [2007-11-11 10:07:55]
# 1 Re: How to prevent the IDE from generating code in your .designer.vb file?
Hi, Mike! I don't know of any way to control the code that's generated by the Visual Studio Forms Designer.
Phil Weber at 2007-11-11 20:48:29 >
# 2 Re: How to prevent the IDE from generating code in your .designer.vb file?
Thanks Phil,

Apparently, for simple string properties, setting the DefaultValue attribute and ALSO giving the module level variable for the property will prevent that entry being generated by the IDE. I also noticed that by setting both of those 2 values, my property in the Properties Tab was not in Bold anymore. BUT, it seems that objects, or ones that required me to use the overloaded DefaultValue method with (type AS System.Type, value AS String) didnt remove the code generated by the IDE and it remained Bold in the Properties Tab.

Any thoughts on why it works on simple string, boolean or integer type properties but not on the others like Byte, Single or Color?

Thanks
MikeDeKeyser at 2007-11-11 20:49:29 >
# 3 Re: How to prevent the IDE from generating code in your .designer.vb file?
From further testing, here is what I have found out:

The following property will NOT be generated with a default value when an instance of it is placed on a form.

Private m_GradientColor1Transparency As Integer = 128
<ComponentModel.Category("BillManager"), _
ComponentModel.DefaultValue(128), _
ComponentModel.Description("Gets or sets the amount of visibility of the first color. (0=transparent, 255=opaque)")> _
Public Property GradientColor1Transparency() As Integer
Get
Return m_GradientColor1Transparency
End Get
Set(ByVal value As Integer)
m_GradientColor1Transparency = value
Me.Refresh()
End Set
End Property

The following property WILL be generated with a default value by the IDE when an instance of the object is placed on the form:

Private m_GradientColor1 As Color = SystemColors.Window
<ComponentModel.Category("BillManager"), _
ComponentModel.DefaultValue(GetType(Color), "SystemColors.Window"), _
ComponentModel.Description("Gets or sets the color of one end of the gradient.")> _
Public Property GradientColor1() As Color
Get
Return m_GradientColor1
End Get
Set(ByVal value As Color)
m_GradientColor1 = value
Me.Refresh()
End Set
End Property

So why would a string, boolean and integer (maybe others too) type be any different from Color, other than its a more complex type? I have treated both properties the same but why would the IDE not do what it did for the integer type property and just NOT generate code?

I am trying to minimize the impact of using my controls on someone elses code.

Thanks again
MikeDeKeyser at 2007-11-11 20:50:33 >
# 4 Re: How to prevent the IDE from generating code in your .designer.vb file?
Both of the following prevents code generation BUT prevents serialization, so is probably not my preferred solution:

ComponentModel.DesignerSerializationVisibility(ComponentModel.DesignerSerializationVisibil ity.Hidden)
ComponentModel.DesignerSerializationVisibility(ComponentModel.DesignerSerializationVisibil ity.Content) _
MikeDeKeyser at 2007-11-11 20:51:39 >
# 5 Re: How to prevent the IDE from generating code in your .designer.vb file?
I don't know the answers to your questions, but you may find this article helpful: http://msdn.microsoft.com/msdnmag/issues/03/04/Design-TimeControls/
Phil Weber at 2007-11-11 20:52:38 >
# 6 Re: How to prevent the IDE from generating code in your .designer.vb file?
Thanks Phil! Very helpful article :)

So based on that article then :

Code Serialization

Unlike the DefaultEventAttribute and DefaultPropertyAttribute, the DefaultValueAttribute serves a dual purpose. Not only does it affect a property's appearance in the property browser, but it also influences what code the designer serializes to InitializeComponent. Those with a default value are only included if the property's value is different than the default. The primary rule of thumb is that your initial property values should match the value set by the DefaultValueAttribute.

In which case, either:

1) the above code sample I wrote might seem to assign the same value as the default value but underneath is not EXACTLY the same
OR
2) there is a bug in VB

Thanks again for your help.
MikeDeKeyser at 2007-11-11 20:53:42 >