zoukankan      html  css  js  c++  java
  • Interface => IDataErrorInfo

    Introduction to common Interfaces

    IDataErrorInfo

    Provides the functionality to offer custom error information that a user interface can bind to. 

    .NET Framework Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

    .NET Framework Client Profile Supported in: 4, 3.5 SP1

    Example 1:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace IDataErrorProviderExample
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //var person = new Person();
                var person = new Person { Age = 10 };
                //var person = new Person { Age = 200 };
                //ageTextBox.DataBindings.Add("Text", person, "Age", true, DataSourceUpdateMode.OnPropertyChanged, null, "No");
                ageTextBox.DataBindings.Add("Text", person, "Age", true, DataSourceUpdateMode.OnPropertyChanged);
    
                //case 1
                errorProvider1.DataSource = person;
    
                //case 2
                //errorProvider1.SetError(ageTextBox, "==="); //temp
            }
        }
    
        public class Person : IDataErrorInfo
        {
            private int age;
    
            public int Age
            {
                get { return age; }
                set { age = value; }
            }
    
            public string Error
            {
                get
                {
                    //check validation on every property
                    return "";
                }
            }
    
            public string this[string name]
            {
                get
                {
                    string result = null;
    
                    switch (name)
                    {
                        case "Age":
                            if (this.age < 0 || this.age > 150)
                            {
                                result = "Age must not be less than 0 or greater than 150.";
                            }
                            break;
                        default:
                            break;
                    }
    
                    return result;
                }
            }
        }
    }
    View Code
  • 相关阅读:
    第二次冲刺spring会议(第一次会议)
    团队项目(4.15站立会议)
    团队项目(4.14站立会议)
    VB中的GDI编程-1 设备环境DC
    合并多个表格数据的代码
    随机跳转页面之使用VBA公共变量
    快速找到Office应用程序安装路径
    CSS3学习笔记(3)-CSS3边框
    CSS3学习笔记(2)-CSS盒子模型
    测试一下js是否可用
  • 原文地址:https://www.cnblogs.com/kevinygq/p/3960521.html
Copyright © 2011-2022 走看看