Change custom control properties at design time

gajus

Member
Joined
Nov 13, 2006
Messages
7
Location
Brussels
Programming Experience
1-3
How to change properties from custom controls that inherit from System.Windows.Forms.Control at design time?

Problem: You can change the properties BackColor and Text only at runtime and not at designtime.

Question: What do I need to change or add to be be able to change properties at design time?
(if possible without having to change the base class properties of the inherited controls)

Many thanks!


VB.NET:
Public MustInherit Class MyControlBase
Inherits System.Windows.Forms.Control
Protected MustOverride ReadOnly Property Control() As System.Windows.Forms.Control
Public Shadows Property BackColor() As System.Drawing.Color
Get
Return Control.BackColor
End Get
Set(ByVal value As System.Drawing.Color)
Control.BackColor = value
End Set
End Property
Public Shadows Property Text() As String
Get
Return Control.Text
End Get
Set(ByVal value As String)
Control.Text = value
End Set
End Property
'Public shadows property ...
'...
Protected Overridable Sub MyControlBase_Resize(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Me.Resize
Me.Control.Size = Me.Size
Me.Size = Me.Control.Size
End Sub
Public Sub New()
InitControl()
InitClass()
End Sub
Protected MustOverride Sub InitControl()
Protected Sub InitClass()
Me.Control.Enabled = True
Me.Control.Visible = True
Me.Control.Parent = Me
MyBase.Controls.Add(Me.Control)
End Sub
Public MustOverride Overloads Function [GetType]() As Type
End Class
 
Public MustInherit Class MyButtonBase
Inherits MyControlBase
Private m_btnMain As System.Windows.Forms.Button
Protected NotOverridable Overrides ReadOnly Property Control() As System.Windows.Forms.Control
Get
Return m_btnMain
End Get
End Property
'Public property ...
'...
Protected NotOverridable Overrides Sub InitControl()
m_btnMain = New System.Windows.Forms.Button
End Sub
End Class
 
Public MustInherit Class MyTextboxBase
Inherits MyControlBase
Private m_txtMain As System.Windows.Forms.TextBox
Protected NotOverridable Overrides ReadOnly Property Control() As System.Windows.Forms.Control
Get
Return m_txtMain
End Get
End Property
'Public property ...
'...
Protected NotOverridable Overrides Sub InitControl()
m_txtMain = New System.Windows.Forms.TextBox
End Sub
End Class
 
'Public MustInherit Class My...
'...
 
Public Class MyButton
Inherits MyButtonBase
Public Overloads Overrides Function [GetType]() As Type
Return GetType(MyButton)
End Function
End Class
 
Public Class MyTextbox
Inherits MyTextboxBase
Public Overloads Overrides Function [GetType]() As Type
Return GetType(MyTextbox)
End Function
End Class
 
'Public Class My...
'...
 
Last edited:
I don't get it, properties inherited form the base class control so long as they have a public modifier can be changed at design time from within the designer. You may need to add the

VB.NET:
<Browsable(True)> _

Attribute, but i can't see why.
 
Adding the attribute doesn't help!

This is what I do:
1) Compile a dll containing the custom controls
2) Open a new project and add a reference to the dll
3) Open a form and add the 'MyTextbox' and 'MyButton' to the form at design time
4) try to change the properties 'Text' and 'BackColor' at design time using the properties window
5) then I have a look at the sub 'InitializeComponent' on the form that contains the 'MyTextbox' and notice that the properties have not been updated:


<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.MyTextbox1 = New ClassLibrary1.MyTextbox
Me.MyButton1 = New ClassLibrary1.MyButton
Me.SuspendLayout()
'
'MyTextbox1
'
Me.MyTextbox1.ForeColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer))
Me.MyTextbox1.Location = New System.Drawing.Point(12, 41)
Me.MyTextbox1.Name = "MyTextbox1"
Me.MyTextbox1.Size = New System.Drawing.Size(75, 20)
Me.MyTextbox1.TabIndex = 1
'
'MyButton1
'
Me.MyButton1.Location = New System.Drawing.Point(12, 12)
Me.MyButton1.Name = "MyButton1"
Me.MyButton1.Size = New System.Drawing.Size(75, 23)
Me.MyButton1.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.Color.FromArgb(CType(CType(224, Byte), Integer), CType(CType(224, Byte), Integer), CType(CType(224, Byte), Integer))
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.MyTextbox1)
Me.Controls.Add(Me.MyButton1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub


When I recompile, the changes in design time have disappeared!
 
VB.NET:
Protected MustOverride ReadOnly Property Control() As System.Windows.Forms.Control
Public Shadows Property BackColor() As System.Drawing.Color
Get
Return Control.BackColor
End Get
Set(ByVal value As System.Drawing.Color)
Control.BackColor = value
End Set
End Property
Public Shadows Property Text() As String
Get
Return Control.Text
End Get
Set(ByVal value As String)
Control.Text = value
End Set
End Property

I must admit, whilst looking at your code again i cannot understand why you are shadowing these properties only to pass a different controls backcolor, seemingly a textbox you have created inside your control that inherits from control. The serializers won't be able to generate code for these properties because they no longer apply to that control. I think i need to understand what exactly it is you are trying to do here because as it is you have an abstract base class that shadows two properties fromt hhe base class control, you then derive another abstract base class from that class and add a button to it. Then do the same with textbox. I just can't understand what you are doing here.
 
I'm trying to create 20 or more different customized usercontrols (each with many customized properties and methods) that inherit from the same customized base class. I have already created 9 controls successfully to use in some larger project. They all work just fine because in this larger project I change their properties at runtime only. Still it would be nice to get my problem solved.

The code I posted is just a small piece of what I've done sofar. But it shows what problem I cannot figger out. I don't really know why I need to shadow the properties (all of them). What I do know is that when I override the base class (System.Windows.Forms.Control) properties instead, Visual Studio crashes abruptly as soon as I drag the controls (MyButton, ByTextbox, ...) on a form. So that's why I'm shadowing them.

You tell me that the serializers are not able to generate the code for these properties and that makes sense. But that's exactly what I'm trying to solve!

Many thanks for you swift reply. But I'm still can't figure it out.

Note: In this larger project the member variables m_btnMain, m_txtMain, etc. are my actual customized controls that inherit from System.Windows.Forms.Button, System.Windows.Forms.Textbox, etc. and define all individual customized properties and methods I need. The base class (MyBaseClass) defines the framework's implemented common properties and methods (like Value, IsChanged, IsDefault, IsErroneous, etc.)
 
In that case just create a new property called something like..

VB.NET:
Public Property MyControlBackColor
Get
Return me.control.backcolor
End Get
..
...


Then just shadow the other properties and hide them from the designer and intellisense

VB.NET:
<Browsable(False), EditorBrowsable(EditorBrowsableState = EditorBrowsableState.Never)> _
Public Shadows Property BackColor() As System.Drawing.Color
Get
Return Mybase.Backcolor
End Get
Set(ByVal value As System.Drawing.Color)
mybase.backcolor =value
End Set
End Property


Apologies if the attribute code isn't quite correct but it's hard to remember everything. But you get the idea. Is that any good to you?
 
Back
Top