Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

Populating a PropertyGrid using Reflection.Emit

Rate me:
Please Sign up or sign in to vote.
4.62/5 (25 votes)
30 Jan 2004Public Domain3 min read 180.8K   1.2K   71  
How to create a class at runtime so you can use its properties in a PropertyGrid.
/* All code is written my me,Ben Ratzlaff and is available for any free use except where noted. 
 *I assume no responsibility for how anyone uses the information available*/

using System;
using System.Windows.Forms;
using System.Drawing;
using CustomPropGrid;

namespace TestCustPropGrid
{
	/// <summary>
	/// A form whose sole purpose is to hold a CustomPropertyGrid
	/// </summary>
	public class PropertyForm:System.Windows.Forms.Form
	{
		private CustomPropertyGrid propertyGrid;

		public PropertyForm(string typeName,Settings settings):this()
		{
			propertyGrid.TypeName=typeName;
			propertyGrid.Settings=settings;			
		}

		public PropertyForm()
		{
			InitializeComponent();
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.propertyGrid = new CustomPropGrid.CustomPropertyGrid();
			this.SuspendLayout();
			// 
			// propertyGrid
			// 
			this.propertyGrid.CommandsVisibleIfAvailable = true;
			this.propertyGrid.Dock = System.Windows.Forms.DockStyle.Fill;
			this.propertyGrid.LargeButtons = false;
			this.propertyGrid.LineColor = System.Drawing.SystemColors.ScrollBar;
			this.propertyGrid.Name = "propertyGrid";
			this.propertyGrid.Size = new System.Drawing.Size(232, 325);
			this.propertyGrid.TabIndex = 0;
			this.propertyGrid.Text = "PropertyGrid";
			this.propertyGrid.ViewBackColor = System.Drawing.SystemColors.Window;
			this.propertyGrid.ViewForeColor = System.Drawing.SystemColors.WindowText;
			// 
			// PropertyForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(232, 325);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.propertyGrid});
			this.Name = "PropertyForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "Custom PropertyGrid";
			this.ResumeLayout(false);

		}
		#endregion
	}
}

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 A Public Domain dedication


Written By
Software Developer (Senior)
United States United States
Graduate of the Computer Science department at the University of Arizona

Comments and Discussions