繁体   English   中英

.NET winforms bug(savefiledialog)?

[英].NET winforms bug (savefiledialog)?

以下示例仅在ToolStripButton单击事件上显示和SaveFileDialog。 如果我预先制作SaveFileDialog,然后双击ToolStripButton,则应用程序堆栈会溢出。 好像Winforms中的一个bug给了我。 从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;
   }
}

好的,所以当控件被Doubleclicked时会出现问题,因为它被设置为在单击时打开对话框,并且两个点击事件都试图同时打开对话框。 我的猜测是,当对话框加载时,应用程序在对话框打开之前短暂进入短暂的空闲状态,足够长以允许另一个事件被调用,当它调用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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM