.NET winforms (savefiledialog) error?

The following example simply shows SaveFileDialog on the ToolStripButton click event. If I pre-do SaveFileDialog, and then double-click ToolStripButton, the application stackoverflow streams. Sounds like a bug in Winforms for me. Not optimistic about getting a fix or even a response from MS (even a couple of years ago they just answered “no longer fixes errors for winforms” when I reported an error), so I just would like some opinions on whether this is a mistake or am I doing something wrong.

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;
   }
}
+4
source share
1 answer

, , 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);
}
+3

Source: https://habr.com/ru/post/1546742/


All Articles