Results 1 to 2 of 2

Thread: Problems persisting property values of custom controls at design time!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Nottingham, England
    Posts
    197

    Angry Problems persisting property values of custom controls at design time!

    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.

    Here is the code for my control:

    Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;
    
    namespace HCS.Components.Controls
    {
    	public class LightButton : Button
    	{
    		#region Fields
    
    		private System.ComponentModel.Container components = null;
    		private Color mBackColor;
    		private Color mLightColor;
    		private bool mIsMouseOver;
    
    		#endregion
    
    		#region Constructor
    
    		public LightButton()
    		{
    			// This call is required by the Windows.Forms Form Designer.
    			InitializeComponent();
    
    			mBackColor = base.BackColor;
    			mLightColor = base.BackColor;
    		}
    
    	
    		#endregion
    
    		#region Clean Up Code
    
    		/// <summary> 
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if(components != null)
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#endregion
    
    		#region Component Designer generated code
    		/// <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();
    		}
    		#endregion
    
    		#region Properties
    
    		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    		[DefaultValue(typeof(System.Drawing.Color), "System.Drawing.SystemColors.Window")]
    		public new Color BackColor
    		{
    			get
    			{
    				return mBackColor;
    			}
    			set
    			{
    				mBackColor = value;
    				SetColour();				
    			}
    
    		}
    
    	
    		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    		[DefaultValue(typeof(bool), "true")]
    		public new bool Enabled
    		{
    			get
    			{
    				return base.Enabled;
    			}
    			set
    			{
    				base.Enabled = value;
    				SetColour();
    			}
    		}
    
    		
    		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    		[DefaultValue(typeof(System.Drawing.Color), "System.Drawing.SystemColors.Window")]
    		public Color LightColor
    		{
    			get
    			{
    				return mLightColor;
    			}
    			set
    			{
    				mLightColor = value;
    				SetColour();
    			}
    		}
    		
    		
    		#endregion
    
    		#region Protected Override Methods
    
    		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();
    			
    		}
    
    	
    		#endregion
    
    		#region Private Methods
    
    		private void SetColour()
    		{
    			if(mIsMouseOver || base.Focused)
    			{
    				base.BackColor = mLightColor;
    			}
    			else
    			{
    				base.BackColor = mBackColor;
    			}
    		}
    
    	
    		#endregion
    	}
    }
    Please help - I'm going insane!

    Thanks in advance,

    Steve.

    P.S. Really glad to see this site is still going!
    Sent by: Steve Barker
    E-mail: [email protected]

    P.S. I KNOW 1 is not a prime!
    See this thread: http://forums.vb-world.net/showthread.php?threadid=26485

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Nottingham, England
    Posts
    197
    I think I've cracked it! If anyone is interested, it's because colors cannot be serialized. Simple as that!

    To get round the problem, it's been suggested that the serialization is done manually using the BinaryFormatter class. This can be used to break the color down into a byte array, which can be persisted to a suitable file.

    The DesignMode property will need to be used to make sure that values are only written at design time.

    Hope this is of interest!

    Steve.
    Sent by: Steve Barker
    E-mail: [email protected]

    P.S. I KNOW 1 is not a prime!
    See this thread: http://forums.vb-world.net/showthread.php?threadid=26485

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width