Question

I found a strange ToolStripButton double click problem. These steps will reproduce the problem:

  1. Create a Windows Form Application.
  2. Add a ToolStrip on the main form.
  3. Add a ToolStripButton on the ToolStrip.
  4. Add an OpenFileDialog on the main form.
  5. Double click the ToolStripButton's Click event on the property toolbox.
  6. Add this in toolStripButton1_Click method:

    openFileDialog1.ShowDialog();
    
  7. Start debug.
  8. 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());
        }
    }
}
Was it helpful?

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top