Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published 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

{

///



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

///


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]


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759