Strange ToolStripButton click to open an OpenFileDialog behavior
-
28-05-2021 - |
Question
I found a strange ToolStripButton
double click problem. These steps will reproduce the problem:
- Create a Windows Form Application.
- Add a
ToolStrip
on the main form. - Add a
ToolStripButton
on theToolStrip
. - Add an
OpenFileDialog
on the main form. - Double click the
ToolStripButton
'sClick
event on the property toolbox. Add this in
toolStripButton1_Click
method:openFileDialog1.ShowDialog();
- Start debug.
- Quickly double click the
ToolStripButton
.
Here comes the problem. First, an open file dialog pops up, and I close it, then another dialog pops up. This shouldn't happen. I close it again, then the main form may have some redraw problem. Finally, I close the main form, but the program is still running.
Please try it yourself and let me know if all those happens.
Why those happens? What should I do to solve it?
You can use this to reproduce the problem:
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());
}
}
}
Solution 2
I decided to use this (for now):
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.Enabled = false;
openFileDialog1.ShowDialog();
toolStripButton1.Enabled = true;
}
OTHER TIPS
Why those happens?
I really don't know, it's a surprise for me!!
What should I do to solve it?
This is a simple workaround:
private bool clicked = false;
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (clicked) return;
clicked = true;
openFileDialog1.ShowDialog();
clicked = false;
}
EDITED:
I suppose that problem is not double-click itself, but OpenFileDialog
behaviour.
If you try this code the error disappears even for (accidental) double-click:
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);
}
}
If you use tsb1.DoubleClickEnabled = true
the error disappear... but I'm not sure this is a good solution