PropertyGrid in C#


Let us first look at the form:

 01.png

As you can see in the above image, you can directly access the properties of the controls.

Depending on the selected Radio Box it will load aal the properties in the Property Grid Control 7 you can directly use the properties from it.

For Textbox :

myPropertyGrid.SelectedObject = myTextBox;

When the radiobutton is checked it will load all the properties of the Textbox into the PropertyGrid Control.

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton1.Checked == true)
    {
        myPropertyGrid.SelectedObject = myTextBox;
    }
}

See the following image:

02.png

In the above image at runtime I have assigned the foreground color of the textbox. This way you can use other properties of Textbox.

For Button :

myPropertyGrid.SelectedObject = myButton;

When the radiobutton for the TextBox is checked it will load all the properties of the Button into the PropertyGrid Control.

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton2.Checked == true)
    {
        myPropertyGrid.SelectedObject = myButton;
    }
}

See the following images:

03.png

In the above image at runtime I assign the foreground color & background color of the Button. In this way you can use other properties of Button.

For ListBox :

myPropertyGrid.SelectedObject = myListBox;

When the radiobutton for the LisBox is checked then it will load all the properties of the ListBox into the PropertyGrid Control.

private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton3.Checked == true)
    {
        myPropertyGrid.SelectedObject = myListBox;
    }
}

See the following images:

04.png

In the above image I have added items into the Listbox at run time using PropertyGrid Control.

05.png

I have added a total of five items. When I click on the Ok button, it will add those five items into the Listbox.

06.png

After adding the five items, I asign the red foreground color to the listbox.

Hope this will clear for you all...


Similar Articles