我发现了一个奇怪的ToolStripButton双击问题。这些步骤将重现问题:
Application.
ToolStrip.
ToolStrip。
ToolStripButton。
ToolStripButton的Click事件。<>H 217H 118将其添加到toolStripButton1_Click方法中:openFileDialog1.ShowDialog();
Start debug.
Quickly双击ToolStripButton.问题来了。首先,弹出一个打开的文件对话框,然后我关闭它,然后弹出另一个对话框。这不应该发生的。我再次关闭它,然后主表单可能有一些重绘问题。最后,我关闭主表单,但程序仍在运行。
请自己试一试,如果所有这些都发生了,请告诉我。
为什么会发生这种事?我该怎么办才能解决这个问题?
您可以使用它来重现问题:
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace WinForm
{
class MyForm : Form
{
private IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
openFileDialog1 = new OpenFileDialog();
toolStrip1 = new ToolStrip();
toolStripButton1 = new ToolStripButton();
toolStrip1.SuspendLayout();
this.SuspendLayout();
toolStrip1.Items.AddRange(new ToolStripItem[] { toolStripButton1 });
toolStripButton1.Text = "toolStripButton1";
toolStripButton1.Click += new EventHandler(toolStripButton1_Click);
this.Controls.Add(toolStrip1);
toolStrip1.ResumeLayout(false);
toolStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private OpenFileDialog openFileDialog1;
private ToolStrip toolStrip1;
private ToolStripButton toolStripButton1;
public MyForm()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyForm());
}
}
}发布于 2012-03-30 11:09:25
我决定使用这个(目前):
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.Enabled = false;
openFileDialog1.ShowDialog();
toolStripButton1.Enabled = true;
}发布于 2012-03-30 10:14:49
,为什么会发生这种事?
我真的不知道,这对我来说是个惊喜!
我该怎么做才能解决这个问题?
这是一个简单的解决办法:
private bool clicked = false;
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (clicked) return;
clicked = true;
openFileDialog1.ShowDialog();
clicked = false;
}编辑:
我认为这个问题不是双击本身,而是OpenFileDialog行为。
如果尝试此代码,即使(意外)双击,错误也会消失:
private void toolStripButton1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog()
{
Title = "Open file",
Filter = "PDF files|*.pdf|All files|*.*"
})
{
dlg.ShowDialog();
Debug.WriteLine(dlg.FileName);
}
}如果你使用tsb1.DoubleClickEnabled = true错误消失..。但我不确定这是个好办法
https://stackoverflow.com/questions/9940912
复制相似问题