C# Windows 窗体打印对话框单击“确定”两次有响应

发布于 2024-08-28 22:00:33 字数 623 浏览 3 评论 0原文

我正在使用 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();
        }
        ...

这让我发疯,我看不到任何其他会干扰它的东西。

I'm using Visual Studio 2008, .net Framework 3.5 for a Windows forms client-server app that I'm working on. There is a weird bug when I run the program and try to print. The print dialog box opens, but I have to click the OK button twice for it to work. After the second click it works fine, no errors. When I put a breakpoint on: if (result == DialogResult.OK) , the breakpoint doesn't trigger until the second click. Here is the code:

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

This is driving me crazy, and I can't see anything else that would interfere with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪眸﹌ 2024-09-04 22:00:33

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

  1. toolstrip1_Click中:

    private void toolStrip1_Click(object sender, EventArgs e)
    {
      this.Validate();
    }
    
  2. 在使用调用OpenFileDialog的函数中:

    private voidlocateMappingToolStripMenuItem_Click(对象发送者,EventArgs e)
    {
      OpenFileDialog dg = new System.Windows.Forms.OpenFileDialog();
      if (dg.ShowDialog() == DialogResult.OK)
      {
        文件位置 = Path.GetDirectoryName(dg.FileName);
        尝试
        {
          如果(加载数据())
          {
            //启用工具栏按钮
            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();
          }
        }
        捕获(异常 ex) 
        {
          MessageBox.Show(exx.Message + Environment.NewLine + exx.InnerException);
        }
      }
    }
    

两行似乎是关键:

  • OpenFileDialog 关闭时,焦点需要重置到主窗口 (EditorForm.ActiveForm.TopLevelControl.Focus();)
  • 单击工具条按钮时,工具条会验证自身 (this.Validate()) 并识别鼠标事件。

I came across this while having the "first toolstrip click unrecognized" using an OpenFileDialog in C#/WinForms. After much cursing and googling, I did this:

  1. In toolstrip1_Click:

    private void toolStrip1_Click(object sender, EventArgs e)
    {
      this.Validate();
    }
    
  2. In the function that uses calls 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);
        }
      }
    }
    

Two things lines seem to be key:

  • When the OpenFileDialog closes, focus needs to be reset to the main window (EditorForm.ActiveForm.TopLevelControl.Focus();)
  • When the toolstrip button is clicked, the toolstrip validates itself (this.Validate()) and recognizes the mouse event.
生死何惧 2024-09-04 22:00:33

我使用计时器实现了它。

将一个计时器放到包含工具条的表单上,并将其变成一个延迟为 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
    }
}

它可能很脏,但它让我摆脱了困境。华泰

I achieved it using a timer.

Drop a timer onto the form containing the toolstrip and turn it into a one shot timer with a delay of say 1mS. Note: Timer must initially have 'Enabled' set to 'False'

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

Create a timer tick event handler

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

It may be dirty but it got me out of a hole. HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文