我正在使用 Visual Studio 2008、.net Framework 3.5 来开发我正在开发的 Windows 窗体客户端-服务器应用程序。当我运行程序并尝试打印时出现一个奇怪的错误。打印对话框打开,但我必须单击“确定”按钮两次才能正常工作。第二次点击后,一切正常,没有错误。当我设置断点时:if (result == DialogResult.OK) ,直到第二次单击时才会触发断点。这是代码:

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

这让我发疯,我看不到任何其他事情会干扰它。

有帮助吗?

其他提示

我在使用“第一个工具条单击无法识别”时遇到了这个问题 OpenFileDialog 在 C#/WinForms 中。经过一番咒骂和谷歌搜索后,我这样做了:

  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())并识别鼠标事件。

我使用定时器来实现它。

删除一个计时器到包含工具条的形式,并与比方说1毫秒的延迟把它变成单次定时器。注:定时器最初必须有“启用”设置为“假”

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

创建计时器滴答事件处理程序

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top