Persisting Color Properties in Custom Controls at Design Time.

G

Guest

Guys

Hope someone can help me! I'm having real problems getting properties I type against a control I have written at design time

I have written a control by inheriting from the Button control. I am trying to add a feature whereby the control changes to a different color when the mouse floats over it, or when the button has the focus. The color changing works if the 'LightColor' is set at runtime in code. However, if I draw the control on a form at design time, change the 'LightColor' property in the Property viewing window, save the form, then close and reopen the form, the property value (as seen in the window) goes back to the default value for the control. It's driving me mad, since I can't see what I've done wrong, although I suspect that the problem is to do with colors not being serialized?

Here is the code for my control
-------------------------------------------------------------------------------
using System
using System.Collections
using System.ComponentModel
using System.Drawing
using System.Data
using System.Windows.Forms

namespace HCS.Components.Control

public class LightButton : Butto

#region Field

private System.ComponentModel.Container components = null
private Color mBackColor
private Color mLightColor
private bool mIsMouseOver

#endregio

#region Constructo

public LightButton(

// This call is required by the Windows.Forms Form Designer
InitializeComponent()

mBackColor = base.BackColor
mLightColor = base.BackColor



#endregio

#region Clean Up Cod

/// <summary>
/// Clean up any resources being used
/// </summary
protected override void Dispose( bool disposing

if( disposing

if(components != null

components.Dispose()


base.Dispose( disposing )


#endregio

#region Component Designer generated cod
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor
/// </summary
private void InitializeComponent(

components = new System.ComponentModel.Container()

#endregio

#region Propertie

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
[DefaultValue(typeof(System.Drawing.Color), "System.Drawing.SystemColors.Window")
public new Color BackColo

ge

return mBackColor

se

mBackColor = value
SetColour();





[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
[DefaultValue(typeof(bool), "true")
public new bool Enable

ge

return base.Enabled

se

base.Enabled = value
SetColour()




[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
[DefaultValue(typeof(System.Drawing.Color), "System.Drawing.SystemColors.Window")
public Color LightColo

ge

return mLightColor

se

mLightColor = value
SetColour()




#endregio

#region Protected Override Method

protected override void OnMouseEnter(EventArgs e

base.OnMouseEnter (e)
mIsMouseOver = true
SetColour()



protected override void OnMouseLeave(EventArgs e

base.OnMouseLeave (e)
mIsMouseOver = false
SetColour()




protected override void OnGotFocus(EventArgs e

base.OnGotFocus (e)
SetColour()



protected override void OnLostFocus(EventArgs e

base.OnLostFocus (e)
SetColour()




#endregio

#region Private Method

private void SetColour(

if(mIsMouseOver || base.Focused

base.BackColor = mLightColor

els

base.BackColor = mBackColor




#endregio
 
D

Daniel Pratt

Hi Guy,

Guy said:
Guys,

Hope someone can help me! I'm having real problems getting properties I
type against a control I have written at design time.
I have written a control by inheriting from the Button control. I am
trying to add a feature whereby the control changes to a different color
when the mouse floats over it, or when the button has the focus. The color
changing works if the 'LightColor' is set at runtime in code. However, if I
draw the control on a form at design time, change the 'LightColor' property
in the Property viewing window, save the form, then close and reopen the
form, the property value (as seen in the window) goes back to the default
value for the control. It's driving me mad, since I can't see what I've done
wrong, although I suspect that the problem is to do with colors not being
serialized?
Here is the code for my control:
<snip>

A few things:

1.) You are "hiding" not "overriding" properties BackColor and Enabled.
This is almost always a bad idea. Override them instead.

2.) You should set the BackColor and LightColor properties to their
default values (as specified by the DefaultValue attribute) in the
constructor. If the value of a property is equal to its default value, the
designer is not going to generate code to set the property. This is most
likely your problem.

3.) This is probably not a problem, but I noticed in a similar scenario
I had used the unqualified name for the color in the DefaultValue attribute
(i.e. "Window" instead of "System.Drawing.SystemColors.Window").

Regards,
Dan
 
J

Jeffrey Tan[MSFT]

Hi,

I have reviewed your post, I will do some research on this issue.
I will reply to you ASAP

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi,

Thank you for posting in the community! My name is Jeffrey, and I will be
assisting you on this issue.

Based on my understanding, you write a custom control which inherited from
button control, but its 2 properties BackColor and LightColor both do not
persist value after you close your solution.

=========================================================
Based on my research, I found that the problem should be the
DesignerSerializationVisibilityAttribute.

Below is the document of DesignerSerializationVisibility Enumeration:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemcomponentmodeldesignerserializationvisibilityclasstopic.asp

From the document, you will see that "Content" member produces code for the
contents of the object, RATHER THAN for the object itself. So BackColor and
LightColor properties will not generate code in source file.

You should use the Visible Enumeration member. (Actually, .Net will default
use Visible Enumeration for you).

I have changed the DesignerSerializationVisibility attribute and it works
well after that.

For more information about code Generation in Visual Designers, please
refer to the article below:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/custcodegen.asp

=========================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Jeffrey

Thanks for the help! Yep, changing the attribute does work, although in future, since it's default, I'll omit it completely

Cheers

Guy.
 
J

Jeffrey Tan[MSFT]

Hi,

I am glad it works.

If you have any further concern, please feel free to tell me, I will work
with you. :)

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top