1

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());
        }
    }
}
EFanZh
  • 2,357
  • 3
  • 30
  • 63
  • You are using toolstripbutton's `Click` event that responds to *single click* then why are you *double-clicking* on it ? – Software Engineer Mar 30 '12 at 10:20
  • I double clicked it by accident, then I found this problem. I mean, if someone double click the button, It shouldn't cause a problem, right? – EFanZh Mar 30 '12 at 10:22
  • Yes, but this does not explain why app still runs when OP closes it (it happens, I tried) – Marco Mar 30 '12 at 10:23
  • Write `toolStripButton1.DoubleClickEnabled = true;` inside your form's constructor. Read this : [http://stackoverflow.com/questions/4576775/why-is-a-nullreferenceexception-thrown-when-a-toolstrip-button-is-clicked-twice]. – Software Engineer Mar 30 '12 at 10:35

2 Answers2

1

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

Marco
  • 56,740
  • 14
  • 129
  • 152
  • @EFanZh: did you try my code? It solved the problem on my pc... now I'm searching why app does not close properly – Marco Mar 30 '12 at 10:24
  • Yes, it seems your code works, but this for every ToolStripButton? There is a lot of job to do. – EFanZh Mar 30 '12 at 10:27
  • @EFanZh: I think problem is within `OpenFileDialog`, not in button itself... I'm going on with some reasearch... – Marco Mar 30 '12 at 10:30
  • But I tried the `Click` event of `Label`, `Button` and `Form`, all work perfectly. – EFanZh Mar 30 '12 at 10:35
1

I decided to use this (for now):

private void toolStripButton1_Click(object sender, EventArgs e)
{
    toolStripButton1.Enabled = false;
    openFileDialog1.ShowDialog();
    toolStripButton1.Enabled = true;
}
EFanZh
  • 2,357
  • 3
  • 30
  • 63