【问题标题】:.NET winforms bug (savefiledialog)?.NET winforms 错误(保存文件对话框)?
【发布时间】: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 崩溃。你试过了吗?

标签: c# winforms


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多