简体   繁体   English

将文本框传递给方法。 使用textchanged事件时。 C#Visual Studio

[英]Passing a textbox to a method. When using a textchanged event. C# visual studio

I have a text box on my form which I am trying to add some validation to. 我在表单上有一个文本框,试图向其中添加一些验证。 I've attached the following method to the textchanged event. 我已将以下方法附加到textchanged事件。 However I'm getting a 'no overload for method matches system.EventHandler' error. 但是我得到一个'没有重载的方法匹配system.EventHandler'错误。 I don't really understand why I'm getting it. 我真的不明白为什么要得到它。 Am I not passing the arguments to the method when I call it? 调用该方法时,是否没有将参数传递给该方法?

        private void rangeValidate(TextBox textbox)
        {

            int mark = Convert.ToInt32(textbox.Text);
            if (mark >= 0 & mark >= 100)
            {
                MessageBox.Show("Please enter a number betwen 0 and 100");
            }

        }

If you attached this method: 如果您附加了此方法:

private void rangeValidate(TextBox textbox)

to TextChanged , you need to conform to the delegates signature. TextChanged ,您需要遵守委托人签名。 So, change it like this: 因此,像这样更改它:

private void rangeValidate(object sender, EventArgs e)
{
    TextBox textbox = sender as TextBox;
    int mark = Convert.ToInt32(textbox.Text);
    if (mark >= 0 & mark >= 100)
    {
        MessageBox.Show("Please enter a number betwen 0 and 100");
    }
}

You can also declare this function like: 您也可以像下面这样声明此函数:

protected void rangeValidate(object sender, EventArgs e)

and add event in the pageload for the textbox, say TextBox1 as 并在该文本框的页面加载中添加事件,例如说TextBox1为

TextBox1.TextChanged += rangeValidate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM