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

萝らか妹 提交于 2019-12-11 10:26:39

问题


Is there a simple way to find out if a property grid is currently being 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.


回答1:


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.



来源:https://stackoverflow.com/questions/7553423/c-sharp-propertygrid-check-if-a-value-is-currently-beeing-edited

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!