当前位置: 首页 > 编程语言 > C#

PropertyGrid验证分享

时间:2023-04-11 02:55:10 C#

PropertyGrid验证我有一个PropertyGrid。当我输入一个格式错误的值(即一串整数项)时,我收到一条错误消息。如果我单击“确定”,错误的值将一直保留到我更改它为止。如果单击取消,则返回原始值。我想控制按钮,所以单击“确定”也将设置回原始值,而不是像取消按钮那样显示错误的值。我怎样才能做到这一点?我会加入@Crono,你为什么要你想要的?如果您问我如何删除该对话框,那么我可以回答使用您自己的TypeConverter:publicclassIntConverter:TypeConverter{publicoverrideboolCanConvertFrom(ITypeDescriptorContextcontext,TypesourceType){}publicoverrideboolCanConvertTo(ITypeDescriptorContextcontext,TypedestinationType){returntrue;}publicoverrideobjectConvertFrom(ITypeDescriptorContextcontext,System.Globalization.CultureInfoculture,objectvalue){if(valueisstring){//尝试解析为int,不要抛出异常}return0;//总是返回一些东西返回base.ConvertTo(上下文、文化、值、destinationType);//我把它留在这里但它不应该调用它}}如果你认为我想要我自己的对话框来编辑一些东西那么我会回答使用我自己的UITypeEditor:publicclassMyEditor:UITypeEditor{publicoverrideUITypeEditorEditStyleGetEditStyle(ITypeDescriptorContextcontext){returnUITypeEditorEditStyle.Modal;}publicoverrideobjectEditValue(ITypeDescriptorContextcontext,IServiceProviderprovider,objectvalue){Form1form1=newForm1();form1.ShowDialog();返回form1.SomeProperty;}}用法是[TypeConverter(typeof(IntConverter))][EditorAttribute(typeof(MyEditor),typeof(UITypeEditor))]publicintSomeProperty{...}但是你想要那个错误对话框(在设置/显示时有获取属性时出现异常)并且您希望“确定”按钮的工作方式与“取消”按钮的工作方式相同,为什么?简短的回答是:你不能。属性网格不支持修改它。但是,如果您感到沮丧,这里有一些示例代码展示了如何使用此对话框。当然,使用所有这些需要您自担风险。如何使用此实用程序类:propertyGrid1.Site=newMySite(propertyGrid1);propertyGrid1.SelectedObject=MyObj;实用类代码:以上就是C#学习教程:PropertyGridvalidation分享的全部内容,如果对大家有用还需要了解更多C#学习教程,希望大家多加关注——publicclassMySite:ISite,IUIService{publicMySite(PropertyGridpropertyGrid){PropertyGrid=propertyGrid;}publicobjectGetService(TypeserviceType){if(serviceType==typeof(IUIService))returnthis;返回空值;}//这是IUIService的一部分publicDialogResultShowDialog(Formform){//检查这里传递的表单是错误对话框。//它的类型名称应该是GridErrorDlg。//您还可以扫描所有控件,例如//删除或修改一些按钮...DialogResultresult=form.ShowDialog(PropertyGrid);if(form.GetType().Name=="GridErrorDlg"&&result==DialogResult.OK){PropertyGrid.Refresh();}返回结果;}publicPropertyGridPropertyGrid{得到;私有集;}publicboolDesignMode{get{returnfalse;}}普blicIContainer容器{get{返回null;}}publicboolCanShowComponentEditor(objectcomponent){returnfalse;}//我把剩下的留作未实现,但要确保整个事情在你的上下文中正常工作...publicIComponentComponent{get{thrownewNotImplementedException();}}publicstringName{get{thrownewNotImplementedException();}设置{抛出新的NotImplementedException();}}publicIWin32WindowGetDialogOwnerWindow(){thrownewNotImplementedException();}publicvoidSetUIDirty(){thrownewNotImplementedException();}publicboolShowComponentEditor(objectcomponent,IWin32Windowparent){thrownewNotImplementedException();}publicvoidShowError(Exceptionex,stringmessage){thrownewNotImplementedException();}publicvoidShowError(Exceptionex){thrownewNotImplementedException();}publicvoidShowError(stringmessage){thrownewNotImplementedException();}publicDialogResultShowMessage(字符串message,stringcaption,MessageBoxButtonsbuttons){thrownewNotImplementedException();}}publicvoidShowMessage(stringmessage,stringcaption){thrownewNotImplementedException();}publicvoidShowMessage(stringmessage){thrownewNotImplementedException();boolShowToolWindow(GuidtoolWindow){thrownewNotImplementedException();}publicSystem.Collections.IDictionaryStyles{get{thrownewNotImplementedException();}}}本文收集自网络,不代表立场。如涉及侵权请点击维权联系管理会员删除如需转载请注明出处: