• C#二次控件


    实现效果:

    1,创建一个WinFrom程序

    2,添加一个类库

    3,类库中添加一个组件类

    4,从工具箱中向组件类中拖拽一个ErrorProvider组件

    代码部分:

     MyTextBox

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    //添加Forms引用
    using System.Windows.Forms;
    namespace ClassLibrary2
    {
        public partial class MyTextBox : TextBox
        {
            public MyTextBox()
            {
                InitializeComponent();
            }
    
            public MyTextBox(IContainer container)
            {
                container.Add(this);
    
                InitializeComponent();
            }
    
            public int IsNull(string info)
            {
                if (this.Text.Trim().Length == 0)
                {
                    this.Error.SetError(this, info);
                    return 0;
                }
                else 
                {
                    this.Error.SetError(this, string.Empty);
                    return 1;
                }
            }
    
        }
    }
    View Code

    Form1

    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 控件
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                int Result = this.myTextBox1.IsNull("不允许传送空值");
                if (Result == 1)
                {
                    this.myTextBox2.Text = this.myTextBox1.Text;
                }
                else
                { 
                //否则执行的代码
                }
                
            }
        }
    }
    View Code

    生成不了时,将dll文件拖拽过去即可。

  • 相关阅读:
    问题 A: C#抽象类Vehicles
    最短路练习
    BFS
    poj 1083 Moving Tables
    组合数
    hdu 1443 Joseph【约瑟夫环】
    poj 2449 Remmarguts' Date【第K短路】
    hdu 1695 GCD 【莫比乌斯函数】
    hdu 2178 猜数字
    bzoj 2440 完全平方数 【莫比乌斯函数】
  • 原文地址:https://www.cnblogs.com/Luck1996/p/12013687.html
Copyright © 2020-2023  润新知