Meccanica Rossi
Meccanica Rossi

Reputation: 51

Show property editor by click on property text box in PropertyGrid

I am creating a touch application on a no-keyboard pc, where I use a PropertyGrid to manage classes to store / save the app configuration. I need to edit the propertyline's rows with a custom keyboard that I created (not the system's) setting the class as UITypeEditor Now the custom keyboard is showed when right button is clicked.

Is it possible to show when on the row start edit (like textbox Enter event), or when the row is selected ?

Upvotes: 1

Views: 1117

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125292

The editor control which you see in PropertyGrid is a GridViewEdit control which is a child of PropertyGridView which is a child of the PropertyGrid.

You can find the edit control and assign an event handler to its Enter event. In this event, you can find the SelectedGridItem and then call its EditPropertyValue method which is responsible to show the UITypeEditor.

private void propertyGrid1_SelectedObjectsChanged(object sender, EventArgs e)
{
    var grid = propertyGrid1.Controls.Cast<Control>()
        .Where(x => x.GetType().Name == "PropertyGridView").FirstOrDefault();

    var edit = grid.Controls.Cast<Control>()
        .Where(x => x.GetType().Name == "GridViewEdit").FirstOrDefault();

    edit.Enter -= edit_Enter;
    edit.Enter += edit_Enter;
}

private void edit_Enter(object sender, EventArgs e)
{
    var item = this.propertyGrid1.SelectedGridItem;
    if (item.GetType().Name == "PropertyDescriptorGridEntry")
    {
        var method = item.GetType().GetMethod("EditPropertyValue",
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Instance);

        var grid = propertyGrid1.Controls.Cast<Control>()
            .Where(x => x.GetType().Name == "PropertyGridView").FirstOrDefault();

        method.Invoke(item, new object[] { grid });
    }
}

Note: For modal editors the Enter event is annoying and repeats over and over again. To avoid this you can use Click event of the control.

Also as another option you can rely on SelectedGridItemChanged event of PropertyGrid and check if e.NewSelection.GetType().Name == "PropertyDescriptorGridEntry" then call EditPropertyValue of the selected grid item using reflection.

Upvotes: 1

Related Questions