Reputation: 103467
When I right-click on my custom UserControl's BackColor
property in the property-grid, then click Reset, I would like the BackColor
property to change to (for example) Color.LightGreen
, and the property value to appear un-bolded, to indicate that it is the default value.
Currently, I know I can do this:
public override void ResetBackColor() {
BackColor = Color.LightGreen;
}
Which works as far as setting it to LightGreen on reset. But it still appears bolded in the property-grid, indicating that the current value is not the default.
I notice that the Control
class has a static read-only property, DefaultBackColor
. Unfortunately, since it's static, I cannot override it.
Is there some way to get all the functionality I want?
Upvotes: 2
Views: 2390
Reputation: 460
In order to make a custom control with a custom backcolor, as you itself mentioned you cannot use porperty defaultbackcolor.
Example in vb.net
<Runtime.InteropServices.DllImport("user32.dll", ExactSpelling:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)>
Public Shared Function GetParent(ByVal hWnd As IntPtr) As IntPtr
End Function
Protected Overrides Sub CreateHandle()
Dim parentHandle As IntPtr = GetParent(Me.Handle)
ParentForm = Control.FromHandle(parentHandle)
ParentForm.TransparencyKey = Me.BackColor
End Sub
Upvotes: 0
Reputation: 158319
You can achieve this by using the DefaultValue
attribute:
public UserControl1()
{
InitializeComponent();
this.BackColor = Color.LightGreen;
}
[DefaultValue(typeof(Color), "LightGreen")]
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
Upvotes: 6