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.
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 24, 2024 (simplify).