Print Windows C # Forms dialog box, double-click OK to answer

I am using Visual Studio 2008, .net Framework 3.5 for the Windows Form Server Windows application that I am working on. When I run the program and try to print, a strange error occurs. A print dialog will open, but I have to double click the OK button for it to work. After the second click, it works fine, no errors. When I set a breakpoint: if (result == DialogResult.OK), the breakpoint does not start 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(); } ... 

It drives me crazy, and I see nothing else that could stop him.

+4
source share
3 answers

I stumbled upon this with the โ€œfirst laidback toolโ€ using OpenFileDialog in C # / WinForms. After much cursing and googling, I did this:

  • In toolstrip1_Click :

     private void toolStrip1_Click(object sender, EventArgs e) { this.Validate(); } 
  • In a function using OpenFileDialog calls:

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

There seem to be two main lines:

  • When closing OpenFileDialog focus should be reset in the main window ( EditorForm.ActiveForm.TopLevelControl.Focus(); )
  • When the toolbar button is pressed, the toolbar checks itself ( this.Validate() ) and recognizes the mouse event.
+1
source

I achieved this with a timer.

Drop the timer on the form containing the dashboard and turn it into a one-shot timer with a delay of 1 ms. Note: for the timer, you must first set Enabled to False

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

Create a timer 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 he pulled me out of the pit. NTN

+1
source

Source: https://habr.com/ru/post/1307081/


All Articles