ValidateTextBox(只能输入数字文本框)

这里我们创建2个TextBox 控件,应用WinForm的验证机制使得TextBox 只能输入数字否则提示错误并且不允许鼠标焦点切换 。

ValidateForm.Designer.cs代码如下:

namespace  validateTextForm
{
    
partial class ValidateForm
    
{
        
/// <summary>
        
/// Required designer variable.
        
/// </summary>

        private System.ComponentModel.IContainer components = null;

        
/// <summary>
        
/// Clean up any resources being used.
        
/// </summary>
        
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)
        
{
            
if (disposing && (components != null))
            
{
                components.Dispose();
            }

            
base.Dispose(disposing);
        }


        
Windows Form Designer generated code

        
private System.Windows.Forms.TextBox textBox1;
        
private System.Windows.Forms.GroupBox groupBox1;
        
private System.Windows.Forms.TextBox textBox2;
        
private System.Windows.Forms.ErrorProvider errorProvider1;
        
private System.Windows.Forms.ErrorProvider errorProvider2;
        
private System.Boolean Flag = false;
        
private System.Windows.Forms.Button button2;
        
private System.Windows.Forms.Button button1;
    }

}


ValidateForm.cs代码如下:

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;

namespace  validateTextForm
{
    
public partial class ValidateForm : Form
    
{
        
public ValidateForm()
        
{
            InitializeComponent();
        }


        
private void textBox1_Validating(object sender, CancelEventArgs e)
        
{
            Int32 intValue;
            
if (!Int32.TryParse(textBox1.Text, out intValue))
            
{
                errorProvider1.SetError(textBox1, 
"must be integer !");
                Flag 
= true;
                e.Cancel 
= true;
            }

            
else
            
{
                errorProvider1.SetError(textBox1,
null);   
            }

        }


        
private void textBox2_Validating(object sender, CancelEventArgs e)
        
{
            Int32 intValue;
            
if (!Int32.TryParse(textBox2.Text, out intValue))
            
{
                errorProvider2.SetError(textBox2, 
"must be integer !");
                Flag 
= true;
                e.Cancel 
= true;
            }

            
else
            
{
                errorProvider2.SetError(textBox2, 
null);
            }

        }


        
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        
{
              
if (Flag == true)
            
{
                e.Cancel 
= false;
            }

        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            MessageBox.Show(
"Validating Successful !");
        }


        
private void button2_Click(object sender, EventArgs e)
        
{
            textBox1.Text 
= textBox2.Text = String.Empty;
        }

    }

}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值