1

我发现了一个奇怪的ToolStripButton双击问题。这些步骤将重现该问题:

  1. 创建一个 Windows 窗体应用程序。
  2. ToolStrip在主窗体上添加一个。
  3. 在. ToolStripButton_ToolStrip
  4. OpenFileDialog在主窗体上添加一个。
  5. 双击属性工具箱上的ToolStripButton'Click事件。
  6. 在方法中添加这个toolStripButton1_Click

    openFileDialog1.ShowDialog();
    
  7. 开始调试。
  8. 快速双击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());
        }
    }
}
4

2 回答 2

1

为什么会发生这些?

我真的不知道,这对我来说是一个惊喜!!

我应该怎么做才能解决它?

这是一个简单的解决方法:

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错误消失...但我不确定这是一个好的解决方案

于 2012-03-30T10:14:49.117 回答
1

我决定使用这个(现在):

private void toolStripButton1_Click(object sender, EventArgs e)
{
    toolStripButton1.Enabled = false;
    openFileDialog1.ShowDialog();
    toolStripButton1.Enabled = true;
}
于 2012-03-30T11:09:25.813 回答