c# KeyDown KeyPress 函数中event 的 Handled属性,使默认消息无效,不传递
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# KeyDown KeyPress 函数中event 的 Handled属性,使默认消息无效,不传递,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1454字,纯文字阅读大概需要3分钟。
内容图文
true
表示跳过控件的默认处理;false
表示还将该事件传递给默认控件处理程序。
下面的示例确定用户是否按了非数值键,如果是,则 KeyPress 使用属性取消事件 Handled
// Boolean flag used to determine when a character other than a number is entered. private bool nonNumberEntered = false; // Handle the KeyDown event to determine the type of character entered into the control. private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { // Initialize the flag to false. nonNumberEntered = false; // Determine whether the keystroke is a number from the top of the keyboard. if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) { // Determine whether the keystroke is a number from the keypad. if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) { // Determine whether the keystroke is a backspace. if(e.KeyCode != Keys.Back) { // A non-numerical keystroke was pressed. // Set the flag to true and evaluate in KeyPress event. nonNumberEntered = true; } } } //If shift key was pressed, it's not a number. if (Control.ModifierKeys == Keys.Shift) { nonNumberEntered = true; } } // This event occurs after the KeyDown event and can be used to prevent // characters from entering the control. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { // Check for the flag being set in the KeyDown event. if (nonNumberEntered == true) { // Stop the character from being entered into the control since it is non-numerical. e.Handled = true; } }
内容总结
以上是互联网集市为您收集整理的c# KeyDown KeyPress 函数中event 的 Handled属性,使默认消息无效,不传递全部内容,希望文章能够帮你解决c# KeyDown KeyPress 函数中event 的 Handled属性,使默认消息无效,不传递所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。