问题
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.
initialize a new window form
Form currentForm = new Form();
show the form
currentForm.Show();
Activate the form
currentForm.Activate();
Set its TopMost to true, so it will bring the form to the top
currentForm.TopMost = true;
Set it to be Focus
currentForm.Focus()
set the form.visible = false
currentForm.Visible = false;
start to show the print dialog
printDialog.ShowDialog(currentForm)
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