gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:35:44 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com