Click here to Skip to main content
14,088,376 members
Click here to Skip to main content
Articles » Languages » C# » Windows Forms » Downloads

Stats

210.5K views
6.7K downloads
148 bookmarked
Posted 7 Jan 2016
Licenced CPOL

PropertyGrid utilities

, 7 Jan 2016
An article on sorting and globalization of properties in a PropertyGrid.
bin
FormsUtilsTest
FormsUtilsTest.exe
FormsUtils.dll
nl
FormsUtilsTest.resources.dll
FormsUtils
FormsUtils.dll
nl
FormsUtils.resources.dll
src
FormsUtils
FormsUtils.cmbx
FormsUtils.prjx
FormsUtilsKey.snk
FormsUtilsTest
FormsUtilsTest.cmbx
FormsUtilsTest.prjx
SomeOtherResources.resources
FormsUtils.dll
FormsUtilsTest.exe
nl
FormsUtils.resources.dll
FormsUtilsTest.resources.dll
FormsUtilsTest
FormsUtilsTest.SomeClassWithProperties.nl.resources
FormsUtilsTest.cmbx
FormsUtilsTest.prjx
MainForm.resources
MyNameSpace.MainForm.resources
MyNameSpace.PropertyGridUtilTest.resources
MyNameSpace.SomeClassWithProperties.resources
PropertyGridUtilTest.resources
SomeClassWithProperties.resources
SomeOtherResources.resources
FormsUtils
CustomPropertyGrid.nl.resources
CustomPropertyGrid.resources
FormsUtils.CustomPropertyGrid.resources
FormsUtilsKey.snk
PropertyGridUtils
bin
nl
PropertyGridUtils.resources.dll
PropertyGridUtils.dll
PropertyGridUtils.cmbx
PropertyGridUtils.CustomPropertyGrid.nl.resources
PropertyGridUtils.CustomPropertyGrid.resources
PropertyGridUtils.prjx
PropertyGridUtilsKey.snk
PropertyGridUtilsTest
bin
nl
PropertyGridUtils.resources.dll
PropertyGridUtilsTest.resources.dll
PropertyGridUtils.dll
PropertyGridUtilsTest.exe
MyNameSpace.MainForm.resources
MyNameSpace.PropertyGridUtilTest.resources
MyNameSpace.SomeClassWithProperties.nl.resources
MyNameSpace.SomeClassWithProperties.resources
PropertyGridUtilsTest.cmbx
PropertyGridUtilsTest.prjx
SomeOtherResources.resources
//
// (C) Wout de Zeeuw 2004
//
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using FormsUtils;

namespace MyNameSpace
{
    [TypeConverter(typeof(PropertiesDeluxeTypeConverter))]
    class SomeClassWithProperties {
        string lastName = "Zeeuw";
        string occupation = "Software engineer";

        int dynamicProperty = 3;
        string dynamicPropertyDisplayName = "Dynamic property";
        bool dynamicPropertyIsVisible = true;
        bool dynamicPropertyIsReadOnly = false;

        string propertyWithResourcesElseWhere = "Dummy";
        SomeClassWithProperties child;
        
        // DisplayName and Description are gotten from resource file
        // MyNameSpace.SomeClassWithProperties.resources.
        [PropertyOrder(0)]
        [GlobalizedProperty(CategoryId = "GlobalCategory")]
        public string LastName {
            get {
                return lastName;
            }
        }
        
        // DisplayName and Description are gotten from resource file
        // MyNameSpace.SomeClassWithProperties.resources.
        [PropertyOrder(1)]
        [GlobalizedProperty(CategoryId = "GlobalCategory")]
        public string Occupation {
            get {
                return occupation;
            }
        }
        
        // Behaviour of this property is partially defined runtime.
        [PropertyOrder(2)]
        [Description("This property is partially dynamic.")]
        [PropertyAttributesProvider("DynamicPropertyAttributesProvider")]
        public int DynamicProperty {
            get {
                return dynamicProperty;
            }
            set {
                dynamicProperty = value;
            }
        }
        
        public void DynamicPropertyAttributesProvider(PropertyAttributes attributes) {
            attributes.DisplayName = dynamicPropertyDisplayName;
            attributes.IsReadOnly = dynamicPropertyIsReadOnly;
            attributes.IsBrowsable = dynamicPropertyIsVisible;
        }
        
        [PropertyOrder(3)]
        [DisplayName("Dynamic property display name")]
        public string DynamicPropertyDisplayName {
            get {
                return dynamicPropertyDisplayName;
            }
            set {
                dynamicPropertyDisplayName = value;
            }
        }
        
        [PropertyOrder(4)]
        [DisplayName("Dynamic property visible")]
        public bool DynamicPropertyIsVisible {
            get {
                return dynamicPropertyIsVisible;
            }
            set {
                dynamicPropertyIsVisible = value;
            }
        }
        
        [PropertyOrder(5)]
        [DisplayName("Dynamic property readonly")]
        public bool DynamicPropertyIsReadOnly {
            get {
                return dynamicPropertyIsReadOnly;
            }
            set {
                dynamicPropertyIsReadOnly = value;
            }
        }
        
        // DisplayName and Description are gotten from resource file
        // SomeOtherResources.resources
        [PropertyOrder(6)]
        [GlobalizedProperty(
                            BaseName = "SomeOtherResources",
                            DisplayNameId = "Dummy.DisplayName",
                            DescriptionId = "Dummy.Description" 
                            )]
        public string PropertyWithResourcesElseWhere {
            get {
                return propertyWithResourcesElseWhere;
            }
        }

        // Property with sub properties for testing tabbing accross expanded/non expanded items.
        [PropertyOrder(7)]
        public SomeClassWithProperties Child {
        	get {
        		if (child == null) {
        			child = new SomeClassWithProperties();
        		}
        		return child;
        	}
        }
    }
    
    
    public class PropertyGridUtilTest : System.Windows.Forms.Form
    {
		private FormsUtils.StatusBarEx statusBarEx;
		private FormsUtils.CustomPropertyGrid propertyGrid;
		private System.Windows.Forms.ListBox languageListBox;
        
        public PropertyGridUtilTest()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            
            languageListBox.Items.Add("en");
            languageListBox.Items.Add("nl");
            languageListBox.SelectedIndexChanged += 
                new EventHandler(OnListViewSelectionChanged);
            
            propertyGrid.SelectedObject = new SomeClassWithProperties();
            propertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(OnPropertyValueChanged);
            
            statusBarEx.AddItem(new Label(), 0, 1f);
        }
        
        private void OnListViewSelectionChanged(object sender, EventArgs e) {
            string language = languageListBox.SelectedItem.ToString();
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                new System.Globalization.CultureInfo(language);
            propertyGrid.Refresh();
        }
        
        private void OnPropertyValueChanged(object sender, PropertyValueChangedEventArgs e) {
            propertyGrid.Refresh();
        }
        
        [STAThread]
        public static void Main(string[] args)
        {
            Application.Run(new PropertyGridUtilTest());
        }
        
        #region Windows Forms Designer generated code
        /// <summary>
        /// This method is required for Windows Forms designer support.
        /// Do not change the method contents inside the source code editor. The Forms designer might
        /// not be able to load this method if it was changed manually.
        /// </summary>
        private void InitializeComponent() {
			this.languageListBox = new System.Windows.Forms.ListBox();
			this.propertyGrid = new FormsUtils.CustomPropertyGrid();
			this.statusBarEx = new FormsUtils.StatusBarEx();
			this.SuspendLayout();
			// 
			// languageListBox
			// 
			this.languageListBox.Location = new System.Drawing.Point(24, 16);
			this.languageListBox.Name = "languageListBox";
			this.languageListBox.Size = new System.Drawing.Size(120, 30);
			this.languageListBox.TabIndex = 1;
			// 
			// propertyGrid
			// 
			this.propertyGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
						| System.Windows.Forms.AnchorStyles.Left) 
						| System.Windows.Forms.AnchorStyles.Right)));
			this.propertyGrid.CommandsVisibleIfAvailable = true;
			this.propertyGrid.ExpandOnTab = false;
			this.propertyGrid.LargeButtons = false;
			this.propertyGrid.LineColor = System.Drawing.SystemColors.ScrollBar;
			this.propertyGrid.Location = new System.Drawing.Point(0, 48);
			this.propertyGrid.Name = "propertyGrid";
			this.propertyGrid.Size = new System.Drawing.Size(456, 272);
			this.propertyGrid.TabIndex = 3;
			this.propertyGrid.Text = "customPropertyGrid1";
			this.propertyGrid.ViewBackColor = System.Drawing.SystemColors.Window;
			this.propertyGrid.ViewForeColor = System.Drawing.SystemColors.WindowText;
			// 
			// statusBarEx
			// 
			this.statusBarEx.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.statusBarEx.Location = new System.Drawing.Point(0, 322);
			this.statusBarEx.Name = "statusBarEx";
			this.statusBarEx.Size = new System.Drawing.Size(456, 20);
			this.statusBarEx.TabIndex = 2;
			// 
			// PropertyGridUtilTest
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.ClientSize = new System.Drawing.Size(456, 342);
			this.Controls.Add(this.propertyGrid);
			this.Controls.Add(this.statusBarEx);
			this.Controls.Add(this.languageListBox);
			this.Name = "PropertyGridUtilTest";
			this.Text = "PropertyGridUtil test";
			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 The Code Project Open License (CPOL)

Share

About the Author

wout de zeeuw
Software Developer (Senior)
Netherlands Netherlands
Wout works as a freelancer based in the Netherlands. His interests are .NET, 3D, DWG, DXF, OpenGL, Computational Geometry and playing Tank Ball!

One of his latest private projects is a commercial DWG DXF .NET library and small DWG DXF viewing application.

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190518.1 | Last Updated 7 Jan 2016
Article Copyright 2016 by wout de zeeuw
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid