All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
C#中定义鼠标指针为指定的动画图标实例
XSL教程:了解XSL的选择模式_[XML教程]
asp.net2.0网站基础之创建Web窗体
PHPUnit袖珍指南之命令行测试工具_php资料_编程技术
SQL2005中的事务与锁定(七)_mssql学习_编程技术
PHP初学者:配置Apache服务器并且设置DNS_php资料_编程技术
ADO.NET和LINQ中古怪的事务特性_.net资料_编程技术
Ajax开发过程中提交获取数据的乱码问题_[AJAX教程]_0
VS2005
关于discuz论坛附件不能上传解决办法_php资料_编程技术
Reading number is top 10 pictures
身材野火台灣美女1
The goddess of the single reason2
中国文革时期的色情图片2
Summer is most suitable for young people to travel in China10
A man's favorite things4
教你22句话
这玉米,买还是不卖?
Summer is most suitable for young people to travel in China1
Beauty shocked Japan Tokyo motor show model
Exquisite decoration is not paying too much2
Download software ranking
Tram sex maniac 2 (H) rar bag5
C#程序员参考手册
Unix video tutorial5
Tram sex maniac 2 (H) rar bag18
Eclipse 4.2.1 For Win32
Desire a peach blossom
asp.netWeb服务器高级编程
Proficient in Eclipse
Tram sex maniac 2 (H) rar bag19
SP3 for SQL2000
qq published in(发表于) 2014/7/11 9:22:38 Edit(编辑)
C#教程:KeyPress事件使用实例

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

{

///



/// 必需的设计器变量。

///


private System.ComponentModel.IContainer components = null;

///

/// 清理所有正在使用的资源。

///


/// 如果应释放托管资源,为 true;否则为 false。

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

///


private void InitializeComponent()

{

this.textBox1 = new System.Windows.Forms.TextBox();

this.textBox2 = new System.Windows.Forms.TextBox();

this.label1 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(53, 80);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(151, 21);

this.textBox1.TabIndex = 0;

//

// textBox2

//

this.textBox2.Location = new System.Drawing.Point(144, 35);

this.textBox2.Name = "textBox2";

this.textBox2.Size = new System.Drawing.Size(100, 21);

this.textBox2.TabIndex = 0;

this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);

//

// label1

//

this.label1.AutoSize = true;

this.label1.Location = new System.Drawing.Point(46, 38);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(77, 12);

this.label1.TabIndex = 1;

this.label1.Text = "请输入数字:";

//

// frmKepress

//

this.ClientSize = new System.Drawing.Size(292, 105);

this.Controls.Add(this.label1);

this.Controls.Add(this.textBox2);

this.Name = "frmKepress";

this.Text = "请输入数字";

this.Load += new System.EventHandler(this.frmKepress_Load_1);

this.ResumeLayout(false);

this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.TextBox textBox2;

private System.Windows.Forms.Label label1;

}

}

★ ★★★★Program.cs主程序文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace _2_09

{

static class Program

{

///

/// 应用程序的主入口点。

///


[STAThread]


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.