2010-04-15 61 views
4

我正在使用Visual Studio 2008,.net Framework 3.5進行Windows窗體客戶端服務器應用程序的開發。當我運行程序並嘗試打印時會出現一個奇怪的錯誤。打印對話框打開,但我必須單擊確定按鈕兩次才能正常工作。第二次點擊後,它工作正常,沒有錯誤。當我把斷點放在:if(result == DialogResult.OK)時,直到第二次點擊纔會觸發斷點。這裏是代碼:C#Windows窗體打印對話框單擊確定兩次以迴應

private void tbPrint_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     printDialog1.Document = pDoc; 

     DialogResult result = printDialog1.ShowDialog(); 

     if (result == DialogResult.OK) 
     { 
      pDoc.PrinterSettings.PrinterName = printDialog1.PrinterSettings.PrinterName; 
      pDoc.Print(); 
     } 
     ... 

這讓我瘋狂,我看不到任何會干擾它的東西。

+0

當您應該將結果作爲DialogResult.OK發送出去時,您能向我們展示printDialog中的代碼嗎? – 2010-04-15 21:11:37

+0

這是密封的printDialog。 – sbartlett 2010-04-16 17:18:26

回答

0
+0

這似乎是它。當我使用常規按鈕而不是工具條按鈕時,只需點擊一下就可以工作! – sbartlett 2010-04-16 17:16:37

+0

以下是解決tooltrip和printdialog問題的方法:http://www.codeguru.com/forum/showthread.php?p=1746116。 – sbartlett 2010-04-16 17:22:51

+0

它從理論上解決了這個問題,但是無法從printdialog中取回DialogResult,所以它不是一個真正的實用修補程序。 – sbartlett 2010-04-19 02:22:12

1

我碰到這個就同時具有了「第一工具條點擊無法識別」,在C#/的WinForms使用OpenFileDialog。很多咒罵,google搜索後,我這樣做:

  1. toolstrip1_Click

    private void toolStrip1_Click(object sender, EventArgs e) 
    { 
        this.Validate(); 
    } 
    
  2. 在使用電話OpenFileDialog功能:

    private void locateMappingToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
        OpenFileDialog dg = new System.Windows.Forms.OpenFileDialog(); 
        if (dg.ShowDialog() == DialogResult.OK) 
        { 
        fileLocation = Path.GetDirectoryName(dg.FileName); 
        try 
        { 
         if (LoadData()) 
         { 
         //Enable toolbar buttons 
         toolStripButton3.Enabled = true; 
         toolStripButton5.Enabled = true; 
         toolStripButton1.Enabled = true; 
         toolStripButton2.Enabled = true; 
         searchParm.Enabled = true; 
         toolStripButton4.Enabled = true; 
         toolStripButton6.Enabled = true; 
         exitToolStripMenuItem.Enabled = true; 
         EditorForm.ActiveForm.TopLevelControl.Focus(); 
         } 
        } 
        catch (Exception exx) 
        { 
         MessageBox.Show(exx.Message + Environment.NewLine + exx.InnerException); 
        } 
        } 
    } 
    

兩件事情線條顯得待定鑰匙:

  • OpenFileDialog關閉,焦點需要復位到主窗口(EditorForm.ActiveForm.TopLevelControl.Focus();
  • 當點擊了工具條按鈕時,工具條驗證本身(this.Validate())並識別鼠標事件。
+0

看來我無法獲得stackoverflow格式化。一天... – JonS 2012-05-08 05:44:33

1

我用計時器實現了它。

將一個定時器拖放到包含工具條的表單上,並將其轉換爲延遲爲1mS的單次定時器。注:計時器最初必須有「已啓用」設置爲「假」

private void toolStripBtnPrint_Click(object sender, EventArgs e) 
{ 
    timer1.Interval = 1; // 1ms 
    timer1.Enabled = true; 
} 

創建一個定時器Tick事件處理程序

private void timer1_Tick(object sender, EventArgs e) 
{ 
    timer1.Enabled = false; 
    PrintDialog printDialogue=new PrintDocument();   
    //Do your initialising here 
    if(DialogResult.OK == printDialogue.ShowDialog()) 
    { 
     //Do your stuff here 
    } 
} 

這可能是骯髒的,但它讓我擺脫困境。 HTH