Click here to Skip to main content
15,885,141 members
Articles / Programming Languages / C#

Unleash PropertyGrid with Dynamic Properties and Globalization

Rate me:
Please Sign up or sign in to vote.
4.97/5 (24 votes)
29 Jan 2004CPOL9 min read 126.1K   4.8K   88  
The article presents a way to enhance the use of the PropertyGid control with dynamic properties and globalization
using System;
using CustomControls.BaseClasses;
using CustomControls.HelperClasses;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace CustomControls.Win32Controls
{
	
	public class DropDownColorPicker:DropDownListBox
	{

		private Color _Value=Color.White;
		public event EventHandler ValueChanged;
		private ColorListBox CList= new ColorListBox();


		public override ListBox List
		{
			get{return CList;}
		}

		[DefaultValue(typeof(Color),"White")]
		public Color Value
		{
			get{return _Value;}
			set
			{
				if(value !=_Value)
				{
					_Value= value;
					Text=value.Name;
					OnValueChanged(new EventArgs());
					Invalidate(); 
				}
			}
		}


		public DropDownColorPicker()
		{
			this.DropDownStyle=ComboBoxStyle.DropDownList;
			this.List.SelectedIndexChanged+=new EventHandler(List_SelectedIndexChanged);
		}


		protected virtual void OnValueChanged(System.EventArgs e)
		{			
			if(ValueChanged!= null)	{ValueChanged(this,e);}
		}

		protected override void DrawText(System.Drawing.Graphics g, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.StringFormat strformat)
		{
			using ( SolidBrush colBrush= new SolidBrush(Value))
			{			
				
				Rectangle colRect=GetColorRect(bounds);
				
				g.FillRectangle(colBrush,colRect);
				g.DrawRectangle( Enabled? Pens.Black:Pens.DarkGray,colRect);
				
				base.DrawText(g,text,font,GetTextRect(colRect, bounds),strformat);
				
			}
		}
		
		private Rectangle GetColorRect(Rectangle bounds)
		{
			return new Rectangle(bounds.X + 2, Math.Max(0,Math.Min(3,bounds.Height-3)),Math.Min(bounds.Width-5,22) ,Math.Max(0,bounds.Height-5));
		}
		
		private Rectangle GetTextRect(Rectangle ColRect,Rectangle bounds)
		{
			return new Rectangle(ColRect.Width+3,ColRect.Top,bounds.Width-ColRect.Width-3, ColRect.Height);
		}
		

		private void List_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.Value=Color.FromName(List.SelectedItem.ToString());
		}
		

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions