* Copyright (c) 2012, 煙臺大學計算機學院學生
* All rights reserved.
* 作 者: 劉鎮
* 完成日期: 2012 年 11 月 10 日
* 版 本 號: 3.016
* 對任務及求解方法的描述部分
* 問題描述:一個文本,該文本框中只能輸入0至9十種數字、a至z或A至Z五十二種字符
*代碼部分:
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 win4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z'))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = null;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
具體測試結果:
心得體會:
就是處理輸入內容所對應的unicode編碼;從而使非字母或數字的不能輸入;通過Handled賦值爲true從而不處理按鍵事件,因此當你輸入非法,就不能讀入;