Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to: Refresh PropertyGrid when user is not editing

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.

like image 856
seekingalpha Avatar asked Nov 23 '25 06:11

seekingalpha


1 Answers

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.

like image 170
KreepN Avatar answered Nov 24 '25 20:11

KreepN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!