【问题标题】:Object sender as parameter for validating current textbox对象发送者作为验证当前文本框的参数
【发布时间】:2013-11-18 15:15:09
【问题描述】:

我想完成 Winforms 验证模式,例如检查文本框中的空字符串。因此,如果我有名为 txtBox1 的文本框和事件处理程序 txtBox1_Validated。我想知道是否可以使用object sender 作为当前文本框属性的标识符?

例如,我有一个可行的解决方案,我将当前文本框的 Text 属性作为参数发送给 ValidateTextBox 这样的方法

private void txtBox1_Validated(object sender, EventArgs e)
{
    bool isEmpty = ValidateTextBox(txtBox1.Text);
    ...
}

我想知道是否可以使用上述方法中的对象发送者来替换txtBox1.Text参数?

谢谢

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    假设您已将txtBox1_Validated 附加到适当的控件,绝对:

    TextBox textBox = (TextBox) sender;
    bool isEmpty = ValidateTextBox(textBox.Text);
    

    这意味着您当然可以为多个控件共享相同的方法。

    编辑:由于其他两个答案(在撰写本文时)使用 as 而不是演员表,让我解释一下为什么我非常故意使用演员表。

    您将自己连接事件处理程序。您知道sender 必须是TextBox - 如果不是,则表明您的代码中存在错误。通过演员表,您会发现该错误。使用as,它将默默地被忽略 - 而且你很可能永远无法修复该错误。

    【讨论】:

    • 只是为了扩展这一点;它将允许您编写一个可以连接到所有文本框的 txtBox_Validated 方法,因此您不必为每个文本框编写一个。这可能是你帖子的重点,但我想确定一下。
    • @Jim:是的,就是这样。
    【解决方案2】:

    当然可以:

    private void txtBox1_Validated(object sender, EventArgs e)
    {
        TextBox txt = sender as TextBox;
        if(txt != null)
        {
           bool isEmpty = ValidateTextBox(txt.Text);
        }
    }
    

    编辑:

    其实if(txt != null)就是If Ok反模式

    这样会更好:

    private void txtBox1_Validated(object sender, EventArgs e)
    {
        TextBox txt = sender as TextBox;
        if(txt == null)
        {
            // Handler error
        }
    
        bool isEmpty = ValidateTextBox(txt.Text);
    }
    

    【讨论】:

    • 我绝对更喜欢在这里使用演员而不是as - 如果sender 引用文本框,那是由于错误,对吧?为什么要继续前进,忘记问题?
    • 是的,你是对的。那是错误。但是使用 as,您可以安全地处理它。
    • 但你没有。你完全忽略了它——你甚至没有提到在你的答案中发现问题的可能性。您可以记录它——但在我看来,面对已知的损坏代码,异常是更好的选择。它更难被忽视。
    • 请看我的编辑。当然,如果您只是忽略错误,最好使用您的版本。但我认为最好安全地处理错误并始终将其记录在某个地方。
    • 这是一个错误。这不是文件存在之类的错误,或者类似的东西。这与将 null 传递给需要非空参数的方法相同 - 这将在惯用的 C# 中给出 ArgumentNullException
    【解决方案3】:

    您可以将sender 参数强制转换为正确对象的实例。

    例如

    private void txtBox1_Validated(object sender, EventArgs e)
    {
        var myTextbox = sender as TextBox;
        if (myTextbox != null) 
        {
            bool isEmpty = ValidateTextBox(myTextbox.Text);
        }
    }
    

    【讨论】:

      【解决方案4】:

      是的,可以这样写

      private void txtBox1_Validated(object sender, EventArgs e)
      {
          bool isEmpty = ValidateTextBox(((TexBox)sender).Text);
      }
      

      但为什么不使用验证器控件呢?

      【讨论】:

      • ((TexBox)sender).Text - 否则,您将尝试将字符串 sender.Text 转换为 TextBox
      • @Chips_100 谢谢!这就是你在手机上打字的结果:) 更正了。
      【解决方案5】:

      Sender 变量是触发事件的对象。您需要强制转换对象以访问其属性:

      TextBox myObj = sender as TextBox;
      if(myObj != null) 
      {
       // TODO
      }
      

      【讨论】:

        【解决方案6】:
         private void button_Click(object sender, EventArgs e)
         {
               if ((sender == (object)button1))
         }
        

        【讨论】:

          猜你喜欢
          • 2016-07-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-04
          • 2020-08-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多