Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Windows Forms » Downloads
 
Add your own
alternative version

Unleash PropertyGrid with Dynamic Properties and Globalization

, 29 Jan 2004
The article presents a way to enhance the use of the PropertyGid control with dynamic properties and globalization
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DynamicProperties
{
	/// <summary>
	/// Summary description for TestForm.
	/// </summary>
	public class TestForm : System.Windows.Forms.Form
	{
		private Company company;
		private Person person;
		private FlatPropertyGrid pg;
		private CustomControls.Win32Controls.DropDownListBox lb_Language;
		private CustomControls.Win32Controls.ToggleButton btn_Address;
		private CustomControls.Win32Controls.ToggleButton btn_CompanyName;
		private CustomControls.Win32Controls.PushButton btn_Employees;
		private System.Windows.Forms.GroupBox gp_PropertyControl;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public TestForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.lb_Language.List.Items.Add("English");
			this.lb_Language.List.Items.Add("Romanian");
			this.lb_Language.List.Items.Add("Portugues");

			this.lb_Language.List.SelectedIndex=0;
			this.lb_Language.List.SelectedIndexChanged+= new EventHandler(lb_SelectedIndex_Changed);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		public static void Main()
		{
			Application.Run(new TestForm());
		}

		#region Windows Form 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()
		{
			DynamicProperties.Employee employee1 = new DynamicProperties.Employee();
			DynamicProperties.Employee employee2 = new DynamicProperties.Employee();
			this.person = new DynamicProperties.Person();
			this.company = new DynamicProperties.Company();
			this.pg = new DynamicProperties.FlatPropertyGrid();
			this.lb_Language = new CustomControls.Win32Controls.DropDownListBox();
			this.btn_Address = new CustomControls.Win32Controls.ToggleButton();
			this.btn_CompanyName = new CustomControls.Win32Controls.ToggleButton();
			this.btn_Employees = new CustomControls.Win32Controls.PushButton();
			this.gp_PropertyControl = new System.Windows.Forms.GroupBox();
			((System.ComponentModel.ISupportInitialize)(this.person)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.company)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.lb_Language)).BeginInit();
			this.gp_PropertyControl.SuspendLayout();
			this.SuspendLayout();
			// 
			// person
			// 
			this.person.Age = -1;
			this.person.CategoryCommands.AddRange(new CustomControls.HelperClasses.CategoryCommand[] {
																										 new CustomControls.HelperClasses.CategoryCommand("Misc", true)});
			this.person.FullName = "";
			this.person.PropertyCommands.AddRange(new CustomControls.HelperClasses.PropertyCommand[] {
																										 new CustomControls.HelperClasses.PropertyCommand("Age", true, false),
																										 new CustomControls.HelperClasses.PropertyCommand("FullName", true, false)});
			// 
			// company
			// 
			this.company.Address = "Calea Bucuresti no 27";
			this.company.CategoryCommands.AddRange(new CustomControls.HelperClasses.CategoryCommand[] {
																										  new CustomControls.HelperClasses.CategoryCommand("CompanyInfo", true),
																										  new CustomControls.HelperClasses.CategoryCommand("Employees", true)});
			this.company.CompanyName = "Master";
			employee1.Age = 18;
			employee1.CategoryCommands.AddRange(new CustomControls.HelperClasses.CategoryCommand[] {
																									   new CustomControls.HelperClasses.CategoryCommand("EmployeeInfo", true)});
			employee1.FullName = "Tom Sawyer ";
			employee1.PropertyCommands.AddRange(new CustomControls.HelperClasses.PropertyCommand[] {
																									   new CustomControls.HelperClasses.PropertyCommand("Age", true, false),
																									   new CustomControls.HelperClasses.PropertyCommand("FullName", true, false)});
			employee2.Age = 16;
			employee2.CategoryCommands.AddRange(new CustomControls.HelperClasses.CategoryCommand[] {
																									   new CustomControls.HelperClasses.CategoryCommand("EmployeeInfo", true)});
			employee2.FullName = "Huckleberry Finn ";
			employee2.PropertyCommands.AddRange(new CustomControls.HelperClasses.PropertyCommand[] {
																									   new CustomControls.HelperClasses.PropertyCommand("Age", true, true),
																									   new CustomControls.HelperClasses.PropertyCommand("FullName", true, false)});
			this.company.Employees.AddRange(new DynamicProperties.Employee[] {
																				 employee1,
																				 employee2});
			this.company.PropertyCommands.AddRange(new CustomControls.HelperClasses.PropertyCommand[] {
																										  new CustomControls.HelperClasses.PropertyCommand("Address", true, false),
																										  new CustomControls.HelperClasses.PropertyCommand("CompanyName", true, false),
																										  new CustomControls.HelperClasses.PropertyCommand("Employees", true, true)});
			// 
			// pg
			// 
			this.pg.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.pg.CommandsVisibleIfAvailable = true;
			this.pg.LargeButtons = false;
			this.pg.LineColor = System.Drawing.SystemColors.ScrollBar;
			this.pg.Location = new System.Drawing.Point(224, 8);
			this.pg.Name = "pg";
			this.pg.SelectedObject = this.company;
			this.pg.Size = new System.Drawing.Size(240, 288);
			this.pg.TabIndex = 5;
			this.pg.Text = "propertyGrid1";
			this.pg.ViewBackColor = System.Drawing.SystemColors.Window;
			this.pg.ViewForeColor = System.Drawing.SystemColors.WindowText;
			// 
			// lb_Language
			// 
			this.lb_Language.Location = new System.Drawing.Point(16, 32);
			this.lb_Language.Name = "lb_Language";
			this.lb_Language.Size = new System.Drawing.Size(112, 20);
			this.lb_Language.TabIndex = 1;
			// 
			// btn_Address
			// 
			this.btn_Address.Location = new System.Drawing.Point(16, 64);
			this.btn_Address.Name = "btn_Address";
			this.btn_Address.Size = new System.Drawing.Size(168, 24);
			this.btn_Address.TabIndex = 2;
			this.btn_Address.Text = "Toggle \"Address.ReadOnly\"";
			this.btn_Address.Click += new System.EventHandler(this.btn_Address_Click);
			// 
			// btn_CompanyName
			// 
			this.btn_CompanyName.Location = new System.Drawing.Point(16, 96);
			this.btn_CompanyName.Name = "btn_CompanyName";
			this.btn_CompanyName.Size = new System.Drawing.Size(168, 24);
			this.btn_CompanyName.TabIndex = 3;
			this.btn_CompanyName.Text = "Toggle \"CompanyName.Visible\"";
			this.btn_CompanyName.Click += new System.EventHandler(this.btn_CompanyName_Click);
			// 
			// btn_Employees
			// 
			this.btn_Employees.Location = new System.Drawing.Point(16, 128);
			this.btn_Employees.Name = "btn_Employees";
			this.btn_Employees.Size = new System.Drawing.Size(168, 24);
			this.btn_Employees.TabIndex = 4;
			this.btn_Employees.Text = "Toggle Employees[0]";
			this.btn_Employees.Click += new System.EventHandler(this.btn_Employees_Click);
			// 
			// gp_PropertyControl
			// 
			this.gp_PropertyControl.Controls.Add(this.btn_CompanyName);
			this.gp_PropertyControl.Controls.Add(this.btn_Employees);
			this.gp_PropertyControl.Controls.Add(this.lb_Language);
			this.gp_PropertyControl.Controls.Add(this.btn_Address);
			this.gp_PropertyControl.Location = new System.Drawing.Point(8, 32);
			this.gp_PropertyControl.Name = "gp_PropertyControl";
			this.gp_PropertyControl.Size = new System.Drawing.Size(200, 168);
			this.gp_PropertyControl.TabIndex = 0;
			this.gp_PropertyControl.TabStop = false;
			this.gp_PropertyControl.Text = "Dynamic Property";
			// 
			// TestForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(472, 301);
			this.Controls.Add(this.gp_PropertyControl);
			this.Controls.Add(this.pg);
			this.Name = "TestForm";
			this.Text = "TestForm";
			((System.ComponentModel.ISupportInitialize)(this.person)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.company)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.lb_Language)).EndInit();
			this.gp_PropertyControl.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void lb_SelectedIndex_Changed(object sender, System.EventArgs e)
		{

			if(lb_Language.List.SelectedItem!=null)
			{
				string lang="en-US";

				if(lb_Language.List.SelectedItem.ToString()=="English")
				{
					lang="en-US";
				}
				else if (lb_Language.List.SelectedItem.ToString()=="Romanian")
				{
					lang="ro-RO";
				}
				else if (lb_Language.List.SelectedItem.ToString()=="Portugues")
				{
					lang="pt-PT";			
				}
			

				System.Threading.Thread.CurrentThread.CurrentCulture= new System.Globalization.CultureInfo(lang);

				pg.Refresh();
			}

		}

		

		private void btn_Address_Click(object sender, System.EventArgs e)
		{
			if(company.PropertyCommands.Contains("Address"))
			{
				company.PropertyCommands["Address"].ReadOnly=!company.PropertyCommands["Address"].ReadOnly;
			}
			else
			{
				company.PropertyCommands.Add(new CustomControls.HelperClasses.PropertyCommand("Address", true, true));
			}
			pg.Refresh();
		}

		private void btn_CompanyName_Click(object sender, System.EventArgs e)
		{
			if(company.PropertyCommands.Contains("CompanyName"))
			{
				company.PropertyCommands["CompanyName"].Visible=!company.PropertyCommands["CompanyName"].Visible;
			}
			else
			{
				company.PropertyCommands.Add(new CustomControls.HelperClasses.PropertyCommand("CompanyName", false, false));
			}
			pg.Refresh();
		}

		private void btn_Employees_Click(object sender, System.EventArgs e)
		{
			Employee emp=null;

			if( company.Employees.Count>0)
			{
				emp=company.Employees[0];

				if(emp.PropertyCommands.Contains("Age"))
				{
					emp.PropertyCommands["Age"].ReadOnly=!emp.PropertyCommands["Age"].ReadOnly;
				}
				else
				{
					emp.PropertyCommands.Add(new CustomControls.HelperClasses.PropertyCommand("Age", true, true));
				}

				if(emp.PropertyCommands.Contains("FullName"))
				{
					emp.PropertyCommands["FullName"].Visible=!emp.PropertyCommands["FullName"].Visible;
				}
				else
				{
					emp.PropertyCommands.Add(new CustomControls.HelperClasses.PropertyCommand("FullName", false, false));
				}

				pg.Refresh();
			}
		}

		
	}
}

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)

About the Author

Daniel Zaharia
Web Developer
Romania Romania
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 30 Jan 2004
Article Copyright 2004 by Daniel Zaharia
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid