首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PropertyGrid验证

PropertyGrid验证
EN

Stack Overflow用户
提问于 2014-04-22 20:09:44
回答 1查看 3.4K关注 0票数 2

我有一台PropertyGrid。当我输入一个格式错误的值(例如,一个字符串进入一个整数项)时,我得到一条错误信息。如果我点击"OK",坏值就会一直保留到我改变它为止。如果我点击“取消”,原始值又回来了。

我想要控制按钮,这样点击“确定”也会设置回原始值,而不是像取消按钮那样显示坏值。

我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2014-04-22 20:47:19

简短的回答是:你不能。属性网格不支持修改它。

但是,如果您觉得心情不好,这里有一些示例代码演示了如何使用此对话框。当然,使用所有这些都要自负风险。

如何使用此实用程序类:

代码语言:javascript
复制
propertyGrid1.Site = new MySite(propertyGrid1);
propertyGrid1.SelectedObject = MyObj;

实用程序类代码:

代码语言:javascript
复制
public class MySite : ISite, IUIService
{
    public MySite(PropertyGrid propertyGrid)
    {
        PropertyGrid = propertyGrid;
    }

    public object GetService(Type serviceType)
    {
        if (serviceType == typeof(IUIService))
            return this;

        return null;
    }

    // this is part of IUIService
    public DialogResult ShowDialog(Form form)
    {
        // Check the form passed here is the error dialog box.
        // It's type name should be GridErrorDlg.
        // You can also scan all controls and for example
        // remove or modify some buttons...
        DialogResult result = form.ShowDialog(PropertyGrid);
        if (form.GetType().Name == "GridErrorDlg" && result == DialogResult.OK)
        {
            PropertyGrid.Refresh();
        }
        return result;
    }

    public PropertyGrid PropertyGrid { get; private set; }
    public bool DesignMode { get { return false; } }
    public IContainer Container { get { return null; } }
    public bool CanShowComponentEditor(object component) { return false; }

    // I've left the rest as not implemented, but make sure the whole thing works in your context...
    public IComponent Component
    {
        get { throw new NotImplementedException(); }
    }

    public string Name
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public IWin32Window GetDialogOwnerWindow()
    {
        throw new NotImplementedException();
    }

    public void SetUIDirty()
    {
        throw new NotImplementedException();
    }

    public bool ShowComponentEditor(object component, IWin32Window parent)
    {
        throw new NotImplementedException();
    }

    public void ShowError(Exception ex, string message)
    {
        throw new NotImplementedException();
    }

    public void ShowError(Exception ex)
    {
        throw new NotImplementedException();
    }

    public void ShowError(string message)
    {
        throw new NotImplementedException();
    }

    public DialogResult ShowMessage(string message, string caption, MessageBoxButtons buttons)
    {
        throw new NotImplementedException();
    }

    public void ShowMessage(string message, string caption)
    {
        throw new NotImplementedException();
    }

    public void ShowMessage(string message)
    {
        throw new NotImplementedException();
    }

    public bool ShowToolWindow(Guid toolWindow)
    {
        throw new NotImplementedException();
    }

    public System.Collections.IDictionary Styles
    {
        get { throw new NotImplementedException(); }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23219139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档