C# PropertyGrid: Windows FormsUse the PropertyGrid control from Windows Forms to display properties from an object.
PropertyGrid. The PropertyGrid control in Windows Forms uses reflection to display all of the properties on any object. You can change the properties.
Property changes are reflected immediately in the user interface. The PropertyGrid looks the same as the property dialogs in Visual Studio.
In this example, we create a Dictionary and then assign it as the SelectedObject. We can then visually browse the Count, all the Keys, and all the Values of the Dictionary instance.
using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApplication15 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Create an example collection. var dictionary = new Dictionary<string, string>(); dictionary.Add("cat", "blue"); dictionary.Add("dog", "green"); // Assign SelectedObject. propertyGrid1.SelectedObject = dictionary; } } }
To get started, open your project and add a PropertyGrid. You can change the object whose properties are displayed in the PropertyGrid by assigning to the SelectedObject property.
Note Through reflection, the grid will enumerate all the property members on the object instance and display them.
And In the screenshot, I created a Button and assigned it to the SelectedObject property.
Then I used Visual Studio to change the color of the Button in the grid and also the Text.
SelectedObject. With PropertyGrid, we need to set the SelectedObject property. You can do this in the Visual Studio designer and in an event handler such as Form1_Load.
Discussion. Is the PropertyGrid useful in Windows Forms programming? The PropertyGrid will always be technically-focused. It will display what it finds in the object through reflection.
Thus PropertyGrid is unsuitable for commonly-used interfaces or user-facing interfaces.
But It could be useful for developing a settings window where advanced users can adjust certain settings in the program.
Summary. The PropertyGrid control in Windows Forms provides a quick way to mutate the object model in your application. It is not as easy to use as a custom solution.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on 9/26/2022 (edit).
© 2007-2022 sam allen.
see site info on the changelog.