Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
All articles(文章集合)All Picture(图片集合)All Video(视频集合)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:22:38 C#教程:KeyPress事件使用实例
C#教程:KeyPress事件使用实例|方法
KeyPress事件KeyPress事件在用户完成按键后发生。示例KeyPress事件的使用本示例中,当程序运行时,只允许在文本框中输入数字,如果文本框输入的内容不是数字,文本框中的内容会显示在窗体的标题栏上,文本框中不会显示内容。示例运行结果如图1所示。图1 KeyPress事件的使用private void textBox2_KeyPress(object sender, KeyPressEventArgs e){if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar)){this.Text = e.KeyChar.ToString();e.Handled = True;}}完整程序代码如下:★ ★★★★frmKepress.cs窗体代码文件完整程序代码★★★★★using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _2_09{public partial class frmKepress : Form{public frmKepress(){InitializeComponent();}private void frmKepress_Load(object sender, EventArgs e){}private void textBox1_KeyPress(object sender, KeyPressEventArgs e){this.Text = e.KeyChar.ToString();e.Handled = true;}private void frmKepress_Load_1(object sender, EventArgs e){}private void textBox2_KeyPress(object sender, KeyPressEventArgs e){if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar)){this.Text = e.KeyChar.ToString();e.Handled = true;}}}}★ ★★★★frmKepress.desiger.cs窗体设计文件完整程序代码★★★★★namespace _2_09{partial class frmKepress{///