[ Windows .Net controls - Creating property templates ]
Assume that all text box controls in my .Net Windows application created by dragging a TextBox control onto the Form editor should have the following default properties (some text box instances can override these properties) :
Text Align: Centre
Resize : As per content
Dock: Left, Top and Right
Now, it becomes cumbersome and manually boring task to set these properties every time it is needed. Is there any way by which these properties can be defaulted at an "Assembly level". I know that User controls will solve the problem, but that I believe that it is a costly solution, and I think that there should be a simpler and more elegant solution for this problem. Thanks
You have two options here.
The first is to create a method that iterates over all nested controls on a form, and picks the textboxes and change the properties, then call this property in the form's initialization code.
The other is to inherit the textbox control in question, and change the properties to your liking in its constructor (and maybe shadowing the properties to expose different defaultvalues to the propertygrid). To make the control available in the form designer, it must be created in a separate propject from where it is supposed to be used, and then referenced in the first project.
Yes, inherit in a custom control and set the default properties in the default constructor.
this.TextAlign = HorizontalAlignment.Center;
this.Dock = DockStyle.Left;
In my project I have a similar need. What I did was listen to the ControlAdded event, and check that if that control is of the desired type... I set those properties.
To this be really automatic... you should place such logic in the Form Base Class. In my case, ALL my Forms inherits from a base one with the logic of painting a gradient background and set standard properties for my forms (Icon, borders, etc.
In the following example, I use a control called "MGButton" and set its properties. You can do a CASE here to customize all your controls. I also use a custom property to know when NOT to use the defaults.
Private Sub FormBase_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles MyBase.ControlAdded
If e.Control.GetType().ToString = "CommonUI.MGButton" Then
Dim boton As CommonUI.MGButton = CType(e.Control, CommonUI.MGButton)
If CType(.Tag, String) <> "OverrideDefaults" Then
.ColorBorde = System.Drawing.Color.Black
.ColorBordeFocus = System.Drawing.Color.Transparent
.ColorFinal = System.Drawing.Color.NavajoWhite
.ColorFinalDisabled = System.Drawing.Color.WhiteSmoke
.ColorFinalOver = System.Drawing.Color.Sienna
.ColorInicial = System.Drawing.Color.Peru
.ColorInicialDisabled = System.Drawing.Color.SeaShell
.ColorInicialOver = System.Drawing.Color.Sienna