【问题标题】:Validation within the Winforms PropertygridWinforms Propertygrid 中的验证
【发布时间】:2013-10-24 07:08:12
【问题描述】:

是否可以在 Winforms Propertgrid 控件中实现输入验证?例如,我如何验证/定义“必填字段”?我可以使用错误提供程序吗? 是否可以使用RequiredAttribute(System.ComponentModel.DataAnnotations)?

【问题讨论】:

    标签: c# .net winforms validation


    【解决方案1】:

    注册 OnValueChanged 事件并在那里完成工作!

    propertyGrid.PropertyValueChanged+=  new PropertyValueChangedEventHandler(propertyGrid_PropertyValueChanged ); 
    
    
    private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) 
        { 
            if (e.ChangedItem.Label == "???" && !IsValid((int)e.ChangedItem.Value) ) 
            { 
                // the entered value is wrong - show error message 
                e.ChangedItem.PropertyDescriptor.SetValue( propertyGrid.SelectedObject, e.OldValue); 
                MessageBox.Show("Wrong Data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
            } 
        }
    
    
        private static bool IsValid( int inputData) 
        { 
          // logic here
        } 
    

    希望这能帮助你解决问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多