In a property grid is there a way to unselect all grid elements programatically?

1k views Asked by At

I am working on a project in which I am using a property grid to display the properties of the selected control.

enter image description here

The Property Grid is fixed to the left edge of the container and in the rest of the space I have the form I am designing.

On clicking a control on the form, the specific control’s property is getting selected.

enter image description here

In the above figure, I have selected the textbox and the textbox’s properties get shown on the propertygrid.

Here if you observe, by default, the Name property is highlighted as well. Is there some way to unselect this property programmatically?

I have tried some suggestions online but none have helped. I am not able to find find a way to remove all selections from the PropertyGrid, but its behaviour seem to be different form a DataGrid...

Here is why I need this... On selecting a control, if a property in the property grid is selected, then the property is getting modified.

For example, If i cut the control using Ctrl + X, the selected value in property grid is getting cut which in some cases is forcing user to set the property before modifying anything on the form.

I have tried selecting multiple controls, but in that case alse the selected property seems to be persistent

2

There are 2 answers

0
Phil1970 On

Well, what you are trying are hacks. It is never a good idea to do such hacks particularly if you are not the only person that use the software.

In your case, the focus should be on the designer while you interact with it. So if the user press Ctrl+X, the designer should respond to the keyboard and it should not have any effect on the property grid (as only one control can have the focus at the same time).

Thus it is up to you to make sure that your designer is focusable, that it has the focus when initially displayed, that it get the focus when you press the TAB key. Pressing the TAB key again should put the focus on the property grid so that user can interact with the grid without using the keyboard.

If you have more than these 2 controls, then obviously TAB should also stop at any appropriate controls. Also, it can be a good idea to have some direct shortcuts like F4 to (show and) activate the properties pane.

If you are not able to make it works, then the best compromise would be to use another windows for the properties grid. By using a distinct Tool windows for the properties, it should not respond to the keyboard when the main windows has the focus.

Here are some links that might help you:

Panel not getting focus

Control.Focus Method() — See Remarks section.

In any case, you should not prevent Ctrl+X to works as expected when the property grid has the focus and a property is selected. Users don't like software that do not follows UI conventions.

As a software developer, you should as much as possible ensure that your application follows standard behaviors. I recommend you that you take one or 2 extra days developing your software properly instead of doing hacks.

Often, compromise to gain a few days will never be fix and will be a pain for many years. Better to do it right from the start. Unselecting an item in the property grid is not an acceptable workaround. Your manager should not allows you to do that.

7
Reza Aghaei On

Since PropertyGrid uses DefaultProperty to select a property in its grid, as an option you can set DefaultProperty attribute at run-time for your object to a non-browsable property, for example:

this.propertyGrid1.SelectedObject = null;
TypeDescriptor.AddAttributes(someControl,
    new Attribute[] { new DefaultPropertyAttribute("Site") });
this.propertyGrid1.SelectedObject = someControl;