单击编程按钮会引发“System.StackOverflowException”例外

发布于 2024-08-21 21:22:32 字数 1122 浏览 13 评论 0原文

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

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

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

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

 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

我不确定是什么导致了此异常扔。

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

Form1 loads and shows Form2 as a dialogue box.

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();
                }
            }

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

壹場煙雨 2024-08-28 21:22:33

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

我的猜测是,调用 PerformClick() 会导致您发布的当前方法再次被调用,从而导致无限调用循环并导致 StackOverflowException

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

if (txtpass.Text == "1234")

计算结果为 false 并且 click 方法不会被一遍又一遍地调用。您可以通过在使其再次单击自身之前设置 txtpass.Text = "" 来实现此目的。

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.

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")

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.

怼怹恏 2024-08-28 21:22:33

通常您会手动调用您尝试运行的事件。

例如,如果您有一个方法

button1_Click(object sender, ButtonEventArgs e)
{
}

,那么您将在代码中调用以下内容:

button1_Click(this, new ButtonEventArgs());

我认为您可能需要解释代码中的一些逻辑,因为不清楚您要做什么。 StackOverflow 可能是因为您正在执行

PerformClick() -> PerformClick() -> PerformClick() 因为您的“1234”文本在调用之间永远不会改变。

Normally you would manually call the event that you are trying to run.

E.g. if you have a method

button1_Click(object sender, ButtonEventArgs e)
{
}

Then you would call the following in your code:

button1_Click(this, new ButtonEventArgs());

I think maybe you need to explain some logic in your code though, as it's not clear what you're trying to do. The StackOverflow probably because you're doing

PerformClick() -> PerformClick() -> PerformClick() because your "1234" text never changes between calls.

天涯离梦残月幽梦 2024-08-28 21:22:33

PerformClick() 是否位于按钮的单击事件内?如果是这样,那就是你出错的地方,因为你让你的应用程序陷入了无限循环。

用户单击按钮,
.NET 运行 Click() 处理程序,
按钮点击 PerformClick(),
.NET 运行 Click() 处理程序,
按钮点击 PerformClick(),
.NET 运行 Click() 处理程序,
按钮点击 PerformClick()

等。

form1 是否肯定在 form2 上调用 ShowDialog(),而不仅仅是 <代码>显示()?

尝试设置 this.DialogResult == DialogResult.OK,而不是 radButton1.DialogResult

按钮上的 DialogResult 属性告诉 .NET 单击 Button 时将哪个 DialogResult 分配给 Form

Is the PerformClick() inside the button's click event? If so, that's where you're going wrong because you're throwing your application into an infinite loop.

User clicks button,
.NET runs Click() handler,
Button clicks PerformClick(),
.NET runs Click() handler,
Button clicks PerformClick(),
.NET runs Click() handler,
Button clicks PerformClick(),

etc.

Is form1 definitely calling ShowDialog() on form2, and not just Show()?

Instead of radButton1.DialogResult, try setting this.DialogResult == DialogResult.OK.

The DialogResult property on a button tells .NET which DialogResult to assign to the Form when the Button is clicked.

孤凫 2024-08-28 21:22:33

要从内部再次调用事件处理程序,您可以使用以下代码:

if (txtpass.Text)
{
    case "1234":
        radButton1.DialogResult = DialogResult.OK;

        txtpass.Text = "12345";

        radButton1.PerformClick();

        break;

    default:
        case "12345":
        break;

}

To call the event handler again from inside you could use the following code:

if (txtpass.Text)
{
    case "1234":
        radButton1.DialogResult = DialogResult.OK;

        txtpass.Text = "12345";

        radButton1.PerformClick();

        break;

    default:
        case "12345":
        break;

}
笑脸一如从前 2024-08-28 21:22:33

堆栈溢出通常是因为方法无限期地调用自身而发生,因为每次调用方法时,都会将一个条目添加到堆栈中,直到没有更多堆栈剩余为止。

要停止递归,请删除行 radButton1.PerformClick();

A stack overflow happens usually because method is indefinitely calling itself, because each time a method is called, an entry is added to the stack to the point where there is no more stack left.

To stop the recursion, remove the line radButton1.PerformClick();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文