Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#对象发送方-获取调用它的方法_C#_Asp.net - Fatal编程技术网

C#对象发送方-获取调用它的方法

C#对象发送方-获取调用它的方法,c#,asp.net,C#,Asp.net,我有以下方法: private void button_Click(object sender, EventArgs e) { //Changes the Text in the RichBox, EXAMPLE: richtTextBox.Text = "Now Changed and calling Method richTextBox_TextChanged"; } 以及 我如何处理这个问题,知道它是否是通过单击按钮调用的?

我有以下方法:

    private void button_Click(object sender, EventArgs e)
    {
          //Changes the Text in the RichBox, EXAMPLE:
          richtTextBox.Text = "Now Changed and calling Method richTextBox_TextChanged";
    }
以及

我如何处理这个问题,知道它是否是通过单击按钮调用的? 我必须使用对象发送器获取信息吗?但是怎么做呢

希望你们能帮助我

private void richTextBox_TextChanged(object sender, EventArgs e)
这里的
sender
是richTextBox,而不是更改文本的按钮

您可以进入堆栈跟踪以发现按钮单击是否在调用堆栈上,但这太过分了(就像使用核弹炸核桃)

在表单中添加一个标志(bool),在按钮单击中将其设置为true,并在TextChanged事件中检查它,然后在按钮单击结束时再次将其设置为false

如果您这样做,我建议将这个
信号
逻辑包装在一个实现IDispose的类中,并在
中使用
语句

也就是说,您确定需要此功能吗?

只需使用一个标志:

private bool _isInButtonClick;

private void button_Click(object sender, EventArgs e)
{
      try
      {
          _isInButtonClick = true;
          //Changes the Text in the RichBox, EXAMPLE:
          richtTextBox.Text = "Now Changed and calling Method richTextBox_TextChanged";
      }
      finally
      {
          _isInButtonClick = false;
      }
}


 private void richTextBox_TextChanged(object sender, EventArgs e)
 {
         if(_isInButtonClick)
         {
           //DO SOMETHING
         }
         else
         {
           //DO SOMETHING
         }
 }

首先想到的是,你可以有某种指示器或标志,一旦按钮点击被调用,你可以在richTextBox\u TextChanged和reset中检查该标志。但我相信这是一个直截了当的解决方案,对于这种情况,桌面应用程序开发人员肯定会知道有更好的解决方案。在finally block中重置“\u isInButtonClick”似乎会产生问题,因为它可以在“richTextBox\u TextChanged”中的“if”block之前执行,因此,我认为最好在“richTextBox\u TextChanged”中重置值。因此,这不起作用,他从不走进:如果(\u isInButtonClick){//DO SOMETHING}@eMi yes。。。最好在richTextBox中重置该标志,这样你就会得到你想要的结果;在richTextBox中\u text发生了变化?在“如果”块之前?听起来很复杂^^^我认为可以对发送者做些什么。。
private bool _isInButtonClick;

private void button_Click(object sender, EventArgs e)
{
      try
      {
          _isInButtonClick = true;
          //Changes the Text in the RichBox, EXAMPLE:
          richtTextBox.Text = "Now Changed and calling Method richTextBox_TextChanged";
      }
      finally
      {
          _isInButtonClick = false;
      }
}


 private void richTextBox_TextChanged(object sender, EventArgs e)
 {
         if(_isInButtonClick)
         {
           //DO SOMETHING
         }
         else
         {
           //DO SOMETHING
         }
 }