473,378 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Persisting Color Properties in Custom Controls at Design Time.

Guy
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(DesignerSerializat ionVisibility.Content)
[DefaultValue(typeof(System.Drawing.Color), "System.Drawing.SystemColors.Window")
public new Color BackColo

ge

return mBackColor

se

mBackColor = value
SetColour();

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

ge

return base.Enabled

se

base.Enabled = value
SetColour()


[DesignerSerializationVisibility(DesignerSerializat ionVisibility.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
-------------------------------------------------------------------------------

Thanks in advance!
Nov 15 '05 #1
5 2732
Hi Guy,

"Guy" <gu*@nospam.hcs.com> wrote in message
news:63**********************************@microsof t.com...
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
Nov 15 '05 #2

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.

Nov 15 '05 #3

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/de...us/cpref/html/
frlrfsystemcomponentmodeldesignerserializationvisi bilityclasstopic.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/de...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.

Nov 15 '05 #4
Guy
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.
Nov 15 '05 #5

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.

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
0
by: Wayne | last post by:
I have added some custom properties to a custom control. The properties are called row and col. At design time, when I change row or col, I would like to see my custom control change visually. ...
1
by: Christophe Peillet | last post by:
I have a CompositeControl with two types of properties: 1.) Mapped Properties that map directly to a child control's properties (ex.: this.TextboxText = m_txt.Text). These properties are handled...
5
by: ganeshokade | last post by:
Dear Experts, I have to write a C# program with the following requirements. I have to make two components (call C1 and C2) both of which can be included by an end user into his project. I have not...
1
by: David Veeneman | last post by:
I'm backporting a component to .NET 1.x, and it required me to use a custom collection, StringList, instead of List<string>. StringList is derived from CollectionBase and is marked serializable. ...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
10
by: Allan Ebdrup | last post by:
I have a custom server web control that inserts another custom web server control in a templated child control (a wisard that inserts a header in its wizardstep) Now after switching to design view...
5
by: gerry | last post by:
I am trying to create a custom container control that will only ever contain a specific type of control. At design time, when a control of a different type is added to the container I would like...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.