另一个事件禁用事件 [英] disable event by another event

查看:80
本文介绍了另一个事件禁用事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设表单上有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
}


这篇关于另一个事件禁用事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆