模拟按钮单击C# [英] Simulate button click c#

查看:181
本文介绍了模拟按钮单击C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个点击按钮的模拟输入密码的形式,基本上Form1的加载和显示窗体2作为一个对话,如果它不给回DialogResult.OK,然后关闭应用程序。

Hey, I am trying to make a simulation of a button click for a password form, basically the form1 loads and shows form2 as a dialogue, and if it does not give back DialogResult.OK, then it closes the application.

到目前为止,我有在按钮:

So far I have in the button:

 if (txtpass.Text == "")
            {
                MessageBox.Show("You need to enter a password", "Password", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                txtpass.Focus();
            }
            else
            {
                if (txtpass.Text == "1234")
                {
                    radButton1.DialogResult = DialogResult.OK;
                    radButton1.PerformClick();
                }
                else
                {
                    MessageBox.Show("Password Incorrect", "Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtpass.Text = "";
                    txtpass.Focus();
                }
            }

到目前为止,你可以看到我试图使用 radButton1.PerformClick(); 但给我的错误信息:

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

任何想法?

感谢。

推荐答案

修改不是猜测。告诉右键单击本身从内部本身是最肯定造成一个无限循环。这将导致被调用遍地的方法,填补了堆栈并使其溢出。

Edit Not a guess. Telling the button to click itself from within itself is most definitely causing an infinite loop. This causes the method to get called over and over, filling up the stack and causing it to overflow.

我的猜测是,调用 PerformClick()是造成张贴到再次调用当前方法,从而导致一个无限通话循环,并导致了 StackOverflowException

My guess is that calling PerformClick() is causing the current method you posted to get called again, thus causing an infinite call loop and resulting in a StackOverflowException.

要prevent这一点,你需要的地方修复逻辑在code,因此:

To prevent this, you need to fix the logic somewhere in your code so that:

if (txtpass.Text == "1234")

计算结果为和Click方法不会被调用了个遍。您可以通过设置可能达到这个 txtpass.Text =你会使其再次点击自己的权利之前。

evaluates to false and the click method doesn't get called over and over. You can probably achieve this by setting txtpass.Text = "" right before you cause it to click itself again.

这篇关于模拟按钮单击C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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