How to bring Print Dialog box to front? - c#

I have a Print Button to print my Crystal report directly to the printer. My code is as below:
CrystalDecisions.CrystalReports.Engine.ReportClass clsReport = new CrystalDecisions.CrystalReports.Engine.ReportClass();
protected void Button3_Click(object sender, EventArgs e)
{
System.Windows.Forms.PrintDialog dialog1 = new System.Windows.Forms.PrintDialog();
dialog1.AllowSomePages = true;
dialog1.AllowPrintToFile = false;
if (dialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
int copies = dialog1.PrinterSettings.Copies;
int fromPage = dialog1.PrinterSettings.FromPage;
int toPage = dialog1.PrinterSettings.ToPage;
bool collate = dialog1.PrinterSettings.Collate;
clsReport.PrintOptions.PrinterName = dialog1.PrinterSettings.PrinterName;
clsReport.PrintToPrinter(copies, collate, fromPage, toPage);
}
}
This working fine but my problem is, whenever I click on Print Button the Print Dialoq appears at the back of all pages so that sometimes I cannot see even that it came or not. So, I want to bring the Print Dialog box in front so on top of all window.
Thank you.

Related

C# WinForms contextmenu focus issue when textbox is added

In a C# WinForms application I need to create a ContextMenuStrip with dropdown and textbox:
private System.Windows.Forms.ContextMenuStrip ct1;
private void button_Click(object sender, EventArgs e)
{
var header = new ToolStripMenuItem("Header");
header.Enabled = false;
var options = new ToolStripMenuItem("Options");
for (int i = 0; i < 5; i++)
{
var checkoption = new ToolStripMenuItem("Check Me " + i + "!");
checkoption.CheckOnClick = true;
options.DropDownItems.Add(checkoption);
}
var txt = new ToolStripTextBox();
txt.Text = "changeme";
options.DropDownItems.Add(txt);
options.DropDown.Closing += DropDown_Closing;
ct1.Items.Clear();
ct1.Items.Add(header);
ct1.Items.Add(options);
ct1.Show(this, button.Left, button.Top);
}
private void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked);
}
Now, e.Cancel will prevent closing the dropdown if the reason is ItemClicked, so I can select more items without having to open the menu again:
Please note that "changeme" is a ToolStripTextBox!
Once I focus it (click on it), I can edit the text inside:
After finish editing the textbox, I still can change the checkbox items, but there is no focus indicator:
How can I get back the focus indicator just as shown on the first picure?
Note: if I move the mouse onto "Header", the dropdown will close, and then moving it back to "Options", will reopen the dropdown and then the focus indicator is good again:
How can I do this without closing and reopening the dropdown?
I have tried Select() for the options item, but it did not help, neither Invalidate() on ct1.
Just have found it:
First needs to add a click handler on the dropdown:
options.DropDown.Click += DropDown_Click;
Then in the click handler it needs to be focused:
private void DropDown_Click(object sender, EventArgs e)
{
var dropdown = (ToolStripDropDown)sender;
dropdown.Focus();
}

How to present print settings from print preview?

In my C# Project I have 2 print functions. One that prints the document directly and another that presents a preview to the user and prints if the user chooses to do so.
While both methods are working, the direkt print version presents the print settings window before it prints the document.
private void printButton_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printIssues;
printDialog.UseEXDialog = true;
if (DialogResult.OK == printDialog.ShowDialog())
{
printIssues.DocumentName = "Some Name";
printIssues.DefaultPageSettings.Landscape = true;
printIssues.Print();
}
}
private void previewButton_Click(object sender, EventArgs e)
{
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Icon = Properties.Resources.favicon;
printPreview.Document = printIssues;
printIssues.DefaultPageSettings.Landscape = true;
((Form)printPreview).WindowState = FormWindowState.Maximized;
printPreview.ShowDialog();
}
The second Version where I present the preview first, once I click the print button the document gets printed on the Default Printer without presenting the Settings window. I've tried some things and searched quiet some time but couldn't find anything that helped me.
I appreciate your help.
I'm afraid this is a known limitation of the PrintPreviewDialog.
It needs to know the printer to draw the layout so it uses the default printer.
I've had the same problem in the past, and I believe it can be solved by showing a PrintDialog before showing the PrintPreviewDialog.
private void previewButton_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
if (DialogResult.OK == printDialog.ShowDialog())
{
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Document = printIssues;
// this is were you take the printersettings from the printDialog
printPreview.Document.PrinterSettings = printDialog.PrinterSettings;
printIssues.DefaultPageSettings.Landscape = true;
printPreview.ShowDialog();
}
}
Another workaround would be to make your own PrintPreviewDialog. But it requires more coding.
can you tell me if the above code works for you?

printing content of a picturebox

HI all,
I have a picture box in my C# WinForms application which is sized 800x800. I want to print the content of this picture box using the following code but it does not do anything at all (just shows the print dialog and when I click on PRINT in the dialog it doe nothing too. What's wrong?
private void menuFilePrint_Click(object sender, EventArgs e)
{
printDocument.OriginAtMargins = true;
printDocument.DocumentName = "TEST IMAGE PRINTING";
printDialog.Document = printDocument;
printDialog.ShowDialog();
}
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(curveBox.Image, 0, 0);
}
You didn't instruct the printDocument to print:
if(printDialog.ShowDialog() == DialogResult.OK)
printDocument.Print();
the printDialog is used to set printing settings.
Without this command "printDocument1.Print();" Nothing will work.

Why does showing a OpenFileDialog in WPF block my WPF Popup?

I have a WPF application where i open a popup mennu (popup control) using the escape key. In that popup menu I open a file dialog when pushing a button, and the when pushing the button closes the popup. When i next time pushes the esc button it doesnt pop up, not until i have focused another program, eg. reset focus. Does anyone know what could cause this?
Edit
//called when pushing esc
private void ShowSettingsMenu()
{
SettingsMenu.IsOpen = true;
}
//clicking my button, subsequent presses on my esc, doesnt pop it up (the code is run)
private void ImportLicenseButton_Click(object sender, RoutedEventArgs e)
{
SettingsMenu.IsOpen = false; //<- hiding it again
OpenFileDialog filedialog = new OpenFileDialog();
filedialog.Filter = "Xml Files|*.xml";
if ((bool)filedialog.ShowDialog())
{
string fileName = "license.xml";
string destinationFolder = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
if (!string.IsNullOrEmpty(filedialog.FileName))
{
File.Copy(filedialog.FileName, System.IO.Path.Combine(destinationFolder, fileName), true);
}
else
{
MessageBox.Show("Please select a file name");
}
}
this.Cursor = Cursors.None;
}
Fixed by redisplaying the popup.

PrinterSetting are ignored while printing an Image

I'm trying to print an Image using PrintDocumentin C# but somehow the setting (like Number of Pages and Image Quality ) are ignored while printing and preview.
Is there anything wrong in following code, Am I missing something?
private void button1_Click(object sender, EventArgs e)
{
using (var printDialog = new PrintDialog())
{
if (printDialog.ShowDialog() == DialogResult.OK)
{
_printDocument.PrinterSettings = printDialog.PrinterSettings;
}
}
}
void _printDocument_Print(object sender, PrintPageEventArgs e)
{
using (Image image = new Bitmap("image0002.tif"))
{
e.Graphics.DrawImage(image, e.MarginBounds.X, e.MarginBounds.Y);
}
}
Have you tried setting the PrintDialog's Document property to the document you want to print? The dialog should automatically manage the settings for the current PrintDocument if I remember correctly, so there should be no need to manually assign the PrinterSettings.
In addition, I think that a DialogResult.OK from PrintDialog.ShowDialog() means that you should print the document (the user clicked the 'Print' button).
For example:
using (var printDialog = new PrintDialog { Document = _printDocument })
{
if (printDialog.ShowDialog() == DialogResult.OK)
{
_printDocument.Print();
}
}
Does that help?
EDIT: If you don't want to print right away, you could try:
using (var printDialog = new PrintDialog { Document = _printDocument })
{
printDialog.ShowDialog();
}
but users might find it a little strange if they click 'Print' and the document doesn't print.

Categories