3

This is my first question on stackoverflow, so please bear with me if I made any mistakes.

Background: I have a Winform PropertyGrid that displays status such as gain and exposure from a Camera object. User can control/edit camera properties easily through PropertyGrid. Meanwhile, camera object periodically queries latest status from a physical camera and cause a refresh in PropertyGrid through INotifyProperfyChanged.

Problem: If a refresh took place while user was entering/editing data on propertygrid (such as entering a new shutter time value), focus was suddenly lost and entered value would be replaced by new value from camera. This resulted in a unfriendly UI experience.

Question: How to prevent PropertyGrid from performing refresh when user was doing editing?

Thanks in advance.

KreepN
  • 8,528
  • 1
  • 40
  • 58

1 Answers1

2

Edit (1:54 PM CST): The code you want is this, as you would wrap your update code / method call to do the update within it:

if(FindFocusedControl(this).GetType().ToString() != "GridViewEdit")
{

}

This will check if the active control,in this case a cell within the propertygrid, is active and skip over the nested code if true.


Utilizing the following method from this SO question and answer you should be able to wrap whatever code does the updating in an If conditional to check to see what control is being focused/used. If the control in use is the propertygrid, don't do anything.

    public static Control FindFocusedControl(Control control)
    {
        var container = control as ContainerControl;
        while (container != null)
        {
            control = container.ActiveControl;
            container = control as ContainerControl;
        }
        return control;

    }

If you wish to try it, pass it your form and check out the results:

var a = FindFocusedControl(this);

As you are a new user, if this works out for you, you may accept an answer by clicking on the check mark to the left of a provided answer. This lets other users know what worked for you when they come upon your question later.

Community
  • 1
  • 1
KreepN
  • 8,528
  • 1
  • 40
  • 58
  • Thanks! This worked. PropertyGrid is now only refreshed when not focused. Although this could delay live data update when a user always focused on PropertyGrid, such behaviour is way better than before. – seekingalpha Jul 10 '12 at 19:11
  • That could easily be resolved via some sort of inactivity timer in which the focus is set to another control, perhaps a label or the form itself, after a set amount of time. Glad to have helped, and welcome to SO! – KreepN Jul 10 '12 at 19:15
  • Timer is a great idea! Thanks again! – seekingalpha Jul 10 '12 at 20:42