首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >奇怪的ToolStripButton单击以打开OpenFileDialog行为

奇怪的ToolStripButton单击以打开OpenFileDialog行为
EN

Stack Overflow用户
提问于 2012-03-30 10:06:34
回答 2查看 2.4K关注 0票数 1

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

Application.

  • Add ToolStrip.

  • Add

  • 在主窗体上创建一个ToolStrip

  • 在主窗体上添加一个ToolStripButton

  • 双击属性工具箱上的ToolStripButtonClick事件。<>H 217H 118将其添加到toolStripButton1_Click方法中:

openFileDialog1.ShowDialog();

  • Start debug.

  • Quickly双击ToolStripButton.

问题来了。首先,弹出一个打开的文件对话框,然后我关闭它,然后弹出另一个对话框。这不应该发生的。我再次关闭它,然后主表单可能有一些重绘问题。最后,我关闭主表单,但程序仍在运行。

请自己试一试,如果所有这些都发生了,请告诉我。

为什么会发生这种事?我该怎么办才能解决这个问题?

您可以使用它来重现问题:

代码语言:javascript
复制
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());
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-30 11:09:25

我决定使用这个(目前):

代码语言:javascript
复制
private void toolStripButton1_Click(object sender, EventArgs e)
{
    toolStripButton1.Enabled = false;
    openFileDialog1.ShowDialog();
    toolStripButton1.Enabled = true;
}
票数 1
EN

Stack Overflow用户

发布于 2012-03-30 10:14:49

,为什么会发生这种事?

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

我该怎么做才能解决这个问题?

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

代码语言:javascript
复制
private bool clicked = false;
private void toolStripButton1_Click(object sender, EventArgs e)
{
    if (clicked) return;
    clicked = true;
    openFileDialog1.ShowDialog();
    clicked = false;
}

编辑:

我认为这个问题不是双击本身,而是OpenFileDialog行为。

如果尝试此代码,即使(意外)双击,错误也会消失:

代码语言:javascript
复制
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错误消失..。但我不确定这是个好办法

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9940912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档