开发者

C# PropertyGrid - Check if a value is currently beeing edited

Is there a simple way to find out if a property grid is currently bei开发者_运维百科ng edited by the user?

My usecase is the following: I update the grid data every second. If the user is editing a value, all inputs get lost when my update is called. So what I want to do is only update if the user is not editing something.


I don't think there is any official way. However the following piece of code can detect when a grid entry is opened using the builtin text box editor, or the dropdown editor. It does not detect when an entry is opened using the small '...' edit button.

public static bool IsInEditMode(PropertyGrid grid)
{
    if (grid == null)
        throw new ArgumentNullException("grid");

    Control gridView = (Control)grid.GetType().GetField("gridView", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(grid);
    Control edit = (Control)gridView.GetType().GetField("edit", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(gridView);
    Control dropDownHolder = (Control)gridView.GetType().GetField("dropDownHolder", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(gridView);

    return ((edit != null) && (edit.Visible & edit.Focused)) || ((dropDownHolder != null) && (dropDownHolder.Visible));
}

Of course, since it's based on the grid internal structure, it may change in the future, so, use at your own risk.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜