编程按钮单击将引发“ System.StackOverflowException”异常 [英] Programmatic button click throws 'System.StackOverflowException' exception

查看:127
本文介绍了编程按钮单击将引发“ System.StackOverflowException”异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在C#.Net中编写了一个WinForms程序,以编程方式在密码表单中单击一个按钮。

I have written a WinForms program in C#.Net to click a button programmatically within a password form.

Form1 加载并显示 Form2 作为对话框。

Form1 loads and shows Form2 as a dialogue box.

如果DialogResult不是DialogResult,则应用程序将关闭。 。

The application will close if DialogResult is anything other that DialogResult.OK.

到目前为止,我有一个按钮单击事件,其编码如下:

So far I have a button click event, which is coded as follows:

 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 (); ,但是运行程序会给我以下消息:

I use radButton1.PerformClick();, but running the program gives me the following message:

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

中发生'System.StackOverflowException'我不确定是什么导致此异常抛出。

I'm unsure what is causing this exception to throw.

推荐答案

编辑。告诉按钮从自身内部单击本身绝对会导致无限循环。这会导致该方法一遍又一遍地调用,填满堆栈并导致其溢出。

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.

为防止这种情况,您需要在代码中的某处修复逻辑,以便:

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

if (txtpass.Text == "1234")

评估为 false 并且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.

这篇关于编程按钮单击将引发“ System.StackOverflowException”异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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