c# 限制textbox的输入范围和长度(长度不用maxlength方法)

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static int i = 0;
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
            }
            else {
                i++;
                if (i > 8)
                {
                    e.Handled = true;//不处理
                }
            }
        }
    }
}


 

你可能感兴趣的:(c#)