2012-03-30 7 views
1

이상한 ToolStripButton 두 번 클릭 문제를 발견했습니다. 이러한 단계를 통해 문제가 재현됩니다.Strange ToolStripButton OpenFileDialog 비헤이비어를 클릭하여 엽니 다.

  1. Windows Form 응용 프로그램을 만듭니다.
  2. 기본 양식에 ToolStrip을 추가하십시오.
  3. ToolStripToolStripButton을 추가하십시오.
  4. 주 양식에 OpenFileDialog을 추가하십시오.
  5. 속성 도구 상자에서 ToolStripButtonClick 이벤트를 두 번 클릭하십시오.
  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()); 
     } 
    } 
} 
+0

거기에 왜 당신은 * 더블 클릭 *을에 * 한 번의 클릭 *를 응답합니다? – Coder

+0

우연히 두 번 클릭하면이 문제가 발견되었습니다.내 말은, 누군가가 버튼을 두 번 클릭하면, 문제가 발생해서는 안된다는 거지, 그렇지? – EFanZh

+0

예,하지만 OP가 닫히면 앱이 여전히 실행되는 이유는 설명하지 않습니다. (발생했지만 시도했습니다) – Marco

답변

1
모르겠어요

나는 (지금은)이 사용하기로 결정 : 당신은 toolstripbutton의`Click` 이벤트를 사용하는

private void toolStripButton1_Click(object sender, EventArgs e) 
{ 
    toolStripButton1.Enabled = false; 
    openFileDialog1.ShowDialog(); 
    toolStripButton1.Enabled = true; 
} 
1

:

당신은 문제를 재현하기 위해 사용할 수 있습니까?

정말 잘 모르겠다. 놀랍다 !!

해결하려면 어떻게해야합니까? 편집을 할

private bool clicked = false; 
private void toolStripButton1_Click(object sender, EventArgs e) 
{ 
    if (clicked) return; 
    clicked = true; 
    openFileDialog1.ShowDialog(); 
    clicked = false; 
} 

:

이 간단한 해결 방법입니다
나는 그 문제가 아닌 가정 자체를 더블 클릭 만 OpenFileDialog 행동.
당신은 오류가도 사라이 코드를하려고하면 (실수로) 두 번 클릭하면 오류가 사라 tsb1.DoubleClickEnabled = true를 사용하는 경우

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); 
    } 
} 

을 ...하지만 난이 좋은 해결책

+0

@EFanZh : 내 코드를 사용해 보셨습니까? 그것은 내 PC에 문제를 해결 ... 지금은 왜 응용 프로그램이 제대로 닫히지 않는지 검색 해요 – Marco

+0

예, 그것은 귀하의 코드가 작동하지만, 모든 ToolStripButton에 대한 것? 할일이 많습니다. – EFanZh

+0

@EFanZh : 문제는 'OpenFileDialog' 내에서 버튼 자체가 아닌 것 같아요 ... 일부 재검색 작업을 진행하고 있습니다 ... – Marco

관련 문제