【发布时间】:2014-07-01 20:07:34
【问题描述】:
以下示例仅显示 ToolStripButton 单击事件上的 SaveFileDialog。如果我预先制作 SaveFileDialog,然后双击 ToolStripButton,则应用程序堆栈溢出。对我来说,这似乎是 Winforms 中的一个错误。对获得修复甚至是 MS 的回复并不乐观(甚至几年前,当我报告错误时,他们只是回复“不再为 winforms 修复错误”),所以我想就这是否是错误提出一些意见或者我做错了什么。
using System;
using System.Windows.Forms;
namespace ToolStripDoubleClickSaveDialog
{
public partial class Form1 : Form
{
SaveFileDialog sfd = new SaveFileDialog();
public Form1()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
sfd.ShowDialog(this);
}
private void InitializeComponent()
{
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
this.toolStrip1.SuspendLayout();
this.SuspendLayout();
//
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(284, 25);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripButton1
//
this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(23, 22);
this.toolStripButton1.Text = "double click me";
this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.toolStrip1);
this.Name = "Form1";
this.Text = "Form1";
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton toolStripButton1;
}
}
【问题讨论】:
-
你可能对this answer感兴趣。
-
嗯...我知道 stackoverflow 异常是什么...这不是问题。
-
可以重现问题的代码是什么?您发布的代码看起来不错。
-
这通常发生在我的事件处理程序意外调用事件本身时。它最终在一个循环中循环触发事件。有时我会有一个按钮,我首先实现逻辑,然后是调用该按钮的工具条项目,但我会无意中调用工具条事件......因此发生了循环。
-
@gunr2171 OP 的代码为我重现了这个问题 - 无论如何,当我双击按钮时,它会导致 LINQPad 崩溃。你试过了吗?