* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 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从而不处理按键事件,因此当你输入非法,就不能读入;

本文介绍了一个简单的Windows Forms应用程序,该程序使用C#实现,能够限制文本框中的输入仅限于数字和字母。通过KeyPress事件处理函数,实现了对非字母数字字符的过滤。
577

被折叠的 条评论
为什么被折叠?



