4

I have a Windows form (.NET 3.5) that contains a propertygrid control. The propertygrid control gets refreshed periodically do display any changes that may have occurred in the class which it represents. I want the refresh to only occur if the user is not currently editing a property in the grid. Is there a way to detect if the user is currently editing a control?

PICyourBrain
  • 9,976
  • 26
  • 91
  • 136
  • 3
    There comes a point where PropertyGrid stops being a suitable replacement for a form with its own dedicated controls. You're getting very close. – Hans Passant Sep 22 '10 at 15:11

4 Answers4

4

Yes - it's a little hacky but you can find out which subcontrol of the property grid is active, and make an educated guess based on what it is. The following seems to work:

bool isEditing = (propertyGrid.ActiveControl.GetType().Name != "PropertyGridView");
Gwyn
  • 41
  • 2
1

There probably is, but might I recommend having your type implement INotifyPropertyChanged instead of refreshing the grid on a timer? This way you would never have to call Refresh yourself; the display would automatically update the value displayed for each property whenever that property changed.

Of course, if your type has tons of properties, or if you're using your grid to dynamically display objects of many different types, this suggestion may not be practical. It's just a thought.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • Per MSDN `INotifyPropertyChanged` doesn't function with the `PropertyGrid`: The information displayed in the grid is a snapshot of the properties at the time the object is assigned. If a property value of the object specified by the SelectedObject is changed in code at run time, the new value is not displayed until an action is taken in the grid that causes the grid to refresh. – CoryG Feb 18 '17 at 04:23
0

This is a fairly complex problem. I'd suggest a two fold approach:

If the control hasn't been modified within a certain threshold and has focus, or if the control doesn't have focus, I'd consider that to be sufficient to determine that it is not currently being edited.

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
0

You could hook up the OnLostFocus event. This way, the control would only get updated once it no longer had focus.

protected virtual void OnLostFocus( EventArgs e)