0

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • The title of question and the part which you described '*Here is why I need this*' are two different things. What's the question exactly? – Reza Aghaei Dec 21 '16 at 11:43
  • hi @RezaAghaei. I want something like propertyGrid1.UnSelectProperties() function that I can call from my program to remove all active selections on the PropertyGrid – Ganesh Kamath - 'Code Frenzy' Dec 21 '16 at 11:57
  • In the figure 2, Name property is selected when i click a control. This will remain selected even when i select multiple controls. I want a way to unselect the Name property – Ganesh Kamath - 'Code Frenzy' Dec 21 '16 at 11:59
  • lets say I had manually selected Default value for some reason and clicked enter to save a default value. After this, If i select 2 controls, then even though the highlight is now on my 2 controls, their default value remains to be highlighted in the PropertyGrid. I want some mechanism to UnSelect in that scenario – Ganesh Kamath - 'Code Frenzy' Dec 21 '16 at 12:35
  • 1
    Why don't you simply remove focus away from property grid to prevent Ctrl+X side effect? – Dusan Dec 21 '16 at 12:44
  • I tried **propertyGrid1.ContainsFocus = false;** it does not allow setting. Also there is no **propertyGrid1.RemoveFocus()** function. – Ganesh Kamath - 'Code Frenzy' Dec 21 '16 at 13:38
  • Not a good idea. How will the property grid works with keyboard if you do that. **You should never do that.** Users expect that when they use the Tab key to put focus on the property grid that one property will be selected and that he can selected another property using the arrow keys. – Phil1970 Dec 21 '16 at 14:34
  • If you press **Ctrl+X** and the item in the grid get cut, then your problem is that your control don't accept the focus. **So fixes your control instead of breaking Microsoft one**. – Phil1970 Dec 21 '16 at 14:40
  • @Phil1970, I am fixing my code but I work on a really huge project and since this is a software issue I need to fix problems. While developing with our tool our customers are facing problems with properties getting updated leading to controls being jumped around while developing their software with our IDE. Really need a fix for this issue... – Ganesh Kamath - 'Code Frenzy' Dec 22 '16 at 04:51
  • One easy solution if you are not able to make it works would be to put the property grid in a floating tools windows. That way, the property grid will only respond to the keyboard when it is active. It would be a good idea to have a shortcut (maybe **F4**) to activate the properties grid windows. There is not reason for your application not to works correctly. Visual Studio and other applications uses the property grid without any problem. If you have problem with the focus, then your application is bugged and if your application is bugged, then you should fix it. Make your designer focusable. – Phil1970 Dec 22 '16 at 13:50
  • Okay thanks @Phil1970, I will try this solution as well – Ganesh Kamath - 'Code Frenzy' Dec 22 '16 at 15:10

2 Answers2

1

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;
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

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.

Community
  • 1
  • 1
Phil1970
  • 2,605
  • 2
  • 14
  • 15