单击编程按钮会引发“System.StackOverflowException”例外
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑不是猜测。告诉按钮从其内部单击自身肯定会导致无限循环。这会导致该方法被一遍又一遍地调用,填满堆栈并导致其溢出。
我的猜测是,调用
PerformClick()
会导致您发布的当前方法再次被调用,从而导致无限调用循环并导致StackOverflowException
。为了防止这种情况,您需要修复代码中某处的逻辑,以便:
计算结果为
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 aStackOverflowException
.To prevent this, you need to fix the logic somewhere in your code so that:
evaluates to
false
and the click method doesn't get called over and over. You can probably achieve this by settingtxtpass.Text = ""
right before you cause it to click itself again.通常您会手动调用您尝试运行的事件。
例如,如果您有一个方法
,那么您将在代码中调用以下内容:
我认为您可能需要解释代码中的一些逻辑,因为不清楚您要做什么。 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
Then you would call the following in your code:
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.
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 callingShowDialog()
onform2
, and not justShow()
?Instead of
radButton1.DialogResult
, try settingthis.DialogResult == DialogResult.OK
.The
DialogResult
property on a button tells .NET whichDialogResult
to assign to theForm
when theButton
is clicked.要从内部再次调用事件处理程序,您可以使用以下代码:
To call the event handler again from inside you could use the following code:
堆栈溢出通常是因为方法无限期地调用自身而发生,因为每次调用方法时,都会将一个条目添加到堆栈中,直到没有更多堆栈剩余为止。
要停止递归,请删除行
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();