C# .NET winforms错误(保存文件对话框)?
下面的示例仅显示ToolStripButton单击事件上的“保存文件”对话框。如果我预先创建SaveFileDialog,然后双击ToolStripButton,则应用程序堆栈溢出。在我看来好像Winforms中的一个bug。对于微软的修复甚至回复都不乐观(即使在几年前,当我报告一个bug时,他们只是回复“winforms不再修复bug”),所以我想听听关于这是一个bug还是我做错了什么的一些意见C# .NET winforms错误(保存文件对话框)?,c#,winforms,C#,Winforms,下面的示例仅显示ToolStripButton单击事件上的“保存文件”对话框。如果我预先创建SaveFileDialog,然后双击ToolStripButton,则应用程序堆栈溢出。在我看来好像Winforms中的一个bug。对于微软的修复甚至回复都不乐观(即使在几年前,当我报告一个bug时,他们只是回复“winforms不再修复bug”),所以我想听听关于这是一个bug还是我做错了什么的一些意见 using System; using System.Windows.Forms; namesp
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;
}
}
好的,当双击控件时会出现问题,因为它被设置为在一次单击时打开对话框,并且两个单击事件都试图同时打开对话框。我的猜测是,在加载对话框时,应用程序会在对话框打开之前短暂进入空闲状态,足够长的时间让其他事件也被调用,从而在调用
ShowDialog()
两次时导致错误
为了防止出现这种情况,您可以获取窗口的名称,并在显示它之前再次检查它是否处于非活动状态
using System.Runtime.Remoting.Lifetime;
//.....
private SaveFileDialog sfd;
private ILease sfdLease;
public Form1()
{
InitializeComponent();
sfd = new SaveFileDialog();
sfdLease= (ILease)sfd.InitializeLifetimeService();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if(sfdLease.CurrentState != LeaseState.Active)
sfd.ShowDialog(this);
}
你可能会感兴趣。嗯……我知道stackoverflow异常是什么……这不是问题。什么代码可以重现这个问题?您发布的代码看起来不错。当我的事件处理程序意外调用事件本身时,通常会发生这种情况。它最终在一个循环中触发事件。有时我会有一个按钮,我首先实现逻辑,然后是一个工具条项目调用该按钮,但我会无意中调用工具条事件。。。这样就出现了一个循环。@gunr2171 OP的代码为我重新编写了这个问题-无论如何,当我双击按钮时,它会使LINQPad崩溃。你试过了吗?