C#實現輸入內容長度檢查( 二 )




5操縱回車事務來驗證錄入的內容長度是否合適自界說長度 。
       private void textBox2_KeyDown(object ser, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                    checklength();
            }
        }
    }

C#實現輸入內容長度檢查



6驗證成果合適要求 , 附完整源代碼:
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();
        }
        private void checklength()
        {
            int m = int.Parse(textBox1.Text);//界說長度
            int n = textBox2.Text.Length;//錄入內容長度
            if (m != n)
            {
                MessageBox.Show("輸入長度錯誤!");
                textBox2.BackColor = Color.Red;
                textBox2.SelectAll();
            }
            else
            {
                MessageBox.Show("OK!");
                textBox2.BackColor = Color.White;
                textBox2.SelectAll();
            }
        }
        private void textBox2_KeyDown(object ser, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                
                    checklength();
                
            }
        }
    }
}

C#實現輸入內容長度檢查



C#實現輸入內容長度檢查



C#實現輸入內容長度檢查




以上內容就是C#實現輸入內容長度檢查的內容啦 , 希望對你有所幫助哦!

猜你喜歡