另一个事件禁用事件

编程入门 行业动态 更新时间:2024-10-27 10:25:17
本文介绍了另一个事件禁用事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设表单上有2个TextBox。 textbox2可以通过用户或后面的代码获取值。如果用户填写textbox1,textbox2将自动从代码后面获取值。 现在如果用户手动更改textbox2,它的值应该手动获取值。 我在Textbox1_Leave上写了一个代码自动启动textbox2,我想在用户手动更改Texbox2文本时在Texbox2_KeyPress中禁用此事件。谢谢所有。

解决方案

简单方法: 使用boolean类型的(class-)全局状态变量。 当你不想要触发其他事件时,将此变量设置为true。 在Textbox1_Leave中检查此变量。如果是,请返回。 这看起来像这样

private bool _noEvent = false ; protected Texbox2_KeyPress( object sender) { _noEvent = true ; // ...您的其他代码 } 受保护 void Textbox1_Leave( object 发​​件人) { 如果(_ noEvent) return ; // ...您的其他代码 }

hi,assume there are 2 TextBox on the form. textbox2 can get value by user or on code behind. if user fill textbox1,textbox2 will got value automatically from code behind. now if user change textbox2 manually,it's value should get manually value . I wrote a code on Textbox1_Leave that initiate textbox2 automatically,I want disable this event in Texbox2_KeyPress when user changed Texbox2 Text manually.thanks all.

解决方案

The easy way: use a (class-) global status variable of type boolean. Set this variable to true when you do not want the other event fired. In Textbox1_Leave check this variable. If true, just return. This would look like this

private bool _noEvent = false; protected Texbox2_KeyPress(object sender) { _noEvent = true; // ... your other code } protected void Textbox1_Leave(object sender) { if(_noEvent) return; // ... your other code }

更多推荐

另一个事件禁用事件

本文发布于:2023-11-09 15:03:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!