c# - .NET winforms 错误(保存文件对话框)?

标签 c# winforms

以下示例简单地显示了 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;
   }
}

最佳答案

好的,所以当控件被双击时就会出现问题,因为它被设置为单击一次打开对话框,并且两个单击事件都试图同时打开对话框。我的猜测是,在加载对话框时,应用程序会在对话框打开之前进入短暂的空闲状态,这段时间足以让其他事件也被调用,从而在调用 ShowDialog() 时导致错误 两次。

为防止这种情况,您可以获得 System.Runtime.Remoting.Lifetime.Lease窗口并在显示之前仔细检查它是否处于事件状态。

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

关于c# - .NET winforms 错误(保存文件对话框)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24518472/

相关文章:

c# - WCF 服务主机何时销毁自定义 IDispatchMessageInspector?

c# - 正则表达式匹配两个或多个连续字符

c# - Visual Studio 2015 CTP 中没有 C# 6.0?

c# - 有没有办法通过委托(delegate)直接调用属性 setter ?

c# - 从我的 c# 应用程序中修剪 SQL 数据库值

c# - 如何处理来自嵌入式 Excel.OleObjects 或 Excel.Shapes 的事件

c# - HKEY 到 Microsoft.Win32.RegistryKey 的转换

winforms - 即使存在有效数据源,Datagridview 行计数也显示 0

.net - .NET 中的双向数据绑定(bind)不起作用,即使使用 INotifyPropertyChanged 对象也是如此

c#如何使用图形路径制作平滑的圆弧区域