C#Windows窗体打印对话框单击确定两次以回应

问题描述:

我正在使用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

这似乎是它。当我使用常规按钮而不是工具条按钮时,只需点击一下就可以工作! – 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

我碰到这个就同时具有了“第一工具条点击无法识别”,在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

看来我无法获得*格式化。一天... – JonS 2012-05-08 05:44:33

我用计时器实现了它。

将一个定时器拖放到包含工具条的表单上,并将其转换为延迟为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