Cannot show print dialog on the top of asp.net web control

旧城冷巷雨未停 提交于 2019-12-12 04:44:08

问题


I am currently using System.Windows.Forms.PrintDialog on a custom control in ASP.net because I want to show the print dialog and assign its PrinterSettings to ReportPrintDocument.PrinterSettings after I click Ok button on the dialog. Here is my code:

using (PrintDialog printDialog = new PrintDialog())
                {
                   if (printDialog.ShowDialog() == DialogResult.OK)
                    {
                        ReportPrintDocument rp = new ReportPrintDocument(rvPermit.ServerReport);
                        rp.PrinterSettings = printDialog.PrinterSettings;
                        rp.Print();
                    }
                }

My problem is that the print dialog always show behind the web browser and I couldn't know that it is showing or not until I minimize the web browser.

Do you know how to show the print dialog on the top of a web form? Please help.


回答1:


Here is my solution for now. (Not recommended) If you can find another one, please share it to me and I'm very appreciate your help on that.

  1. initialize a new window form Form currentForm = new Form();

  2. show the form currentForm.Show();

  3. Activate the form currentForm.Activate();

  4. Set its TopMost to true, so it will bring the form to the top currentForm.TopMost = true;

  5. Set it to be Focus currentForm.Focus()

  6. set the form.visible = false currentForm.Visible = false;

  7. start to show the print dialog printDialog.ShowDialog(currentForm)

  8. close the new form currentForm.Close();

       try
            {
    
              using (PrintDialog printDialog = new PrintDialog())
                {
                    ReportPrintDocument rp = new ReportPrintDocument(rvPermit.ServerReport);
    
                    Form currentForm = new Form();
                    currentForm.Show();
                    currentForm.Activate();
                    currentForm.TopMost = true;
                    currentForm.Focus();
                    currentForm.Visible = false;
    
                if (printDialog.ShowDialog(currentForm) == DialogResult.OK)
                {
                    if (PrintReport != null)
                        PrintReport(this, e);
    
                    rp.PrinterSettings = printDialog.PrinterSettings;
                    rp.Print();
                }
    
                currentForm.Close();
            }
        }
        catch (Exception)
        {
            // Prevent any error while calling the printer dialog
        }
    


来源:https://stackoverflow.com/questions/22202680/cannot-show-print-dialog-on-the-top-of-asp-net-web-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!