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

Property Grid - Dynamic List ComboBox, Validation, and More

Rate me:
Please Sign up or sign in to vote.
4.57/5 (20 votes)
25 Sep 2009CPOL17 min read 167.4K   11.2K   90  
A PropertyGrid implementation showing how-to use, best practices, validation, and more.
using System.Windows.Forms;
using CarApplication.View;
using DataModel.CarModel.Cars;
using DataModel.CarModel.Wheels;
using DataModel.CarModel.Wheels.Falken;
using DataModel.PersonModel;

namespace CarApplication
{
	public partial class Form1 : Form
	{
		private readonly Car _car;
		private readonly Person _person;
		private readonly Wheel _wheel;

		private readonly ViewCar _viewCar;
		private readonly ViewPerson _viewPerson;
		private readonly ViewPerson2 _viewPerson2;
		private readonly ViewWheel _viewWheel;
		private ViewPersonCollection _viewAncestor;


		public Form1()
		{
			InitializeComponent();

			_car = new Car();
			_person = new Person();
			_wheel = new FalkenZE912();
			CreateAncestors();

			_person.Name = "Bob The Builder";

			_viewCar = new ViewCar( _car );
			_viewPerson = new ViewPerson( _person );
			_viewPerson2 = new ViewPerson2( _person );
			_viewWheel = new ViewWheel( _wheel );
			_viewWheel.Person = _viewPerson2;
		}

		private void CreateAncestors()
		{
			ViewPersonCollection view;

			_viewAncestor = AddChild( null, "Grandpa" );
			_viewAncestor.ChooseParent = _viewAncestor;

			view = AddChild( _viewAncestor, "Father" );
			AddChild( view, "Ann" );
			AddChild( view, "Bill" );
			AddChild( view, "Charley" );

			AddChild( _viewAncestor, "Uncle Albert" );
			AddChild( _viewAncestor, "Aunt Jemima" );
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="parent"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		private static ViewPersonCollection AddChild( ViewPersonCollection parent, string name )
		{
			Person person;
			ViewPersonCollection view;

			person = new Person();
			person.Name = name;

			view = new ViewPersonCollection( person );

			if ( parent != null )
				parent.Children.Add( view );

			return ( view );
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeView1_AfterSelect( object sender, TreeViewEventArgs e )
		{
			if ( e.Node.Name.Equals( "treeNode1" ) )
			{
				this.propertyGridControl1.SelectedObject = _viewCar;
			}
			else if ( e.Node.Name.Equals( "treeNode2" ) )
			{
				this.propertyGridControl1.SelectedObject = _viewWheel;
			}
			else if ( e.Node.Name.Equals( "treeNode3" ) )
			{
				this.propertyGridControl1.SelectedObject = _viewPerson;
			}
			else if ( e.Node.Name.Equals( "treeNode4" ) )
			{
				this.propertyGridControl1.SelectedObject = _viewAncestor;
			}
			
		}
	}
}

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
Software Developer (Senior) Webbert Solutions
United States United States
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.

Comments and Discussions