views:

617

answers:

3

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.

A: 

Maybe it is an issue similar to this one: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/681a50b4-4ae3-407a-a747-87fb3eb427fd

joshperry
This seems to be it. When I use a regular button instead of a toolstrip button it works with one click!
Here is the fix for the problem with the toolstrip and printdialog: http://www.codeguru.com/forum/showthread.php?p=1746116.
It fixes the problem in theory, but you cannot get back the DialogResult from the printdialog, so it's not really a practical fix.
You can wrap whatever needs the result into a method called by the delegate. Just had the same problem, in my case it was easy: I have a ToolStripMenuItem for this functionality, so I just triggered ToolStripMenuItem_Clicked via delegate from ToolStripButton_Click :)
peter p
A: 

I just tried your code on Win7, VS2008 and could not replicate your problem. May I ask what happens if you put it all on one line, like this?

PrintDialog printDialog1 = new PrintDialog();
PrintDocument pDoc = new PrintDocument();

printDialog1.Document = pDoc;

if (printDialog1.ShowDialog() == DialogResult.OK)
{
    pDoc.PrinterSettings.PrinterName = printDialog1.PrinterSettings.PrinterName;
    pDoc.Print();
}
Brandi
The same so long as I am using a toolstrip button. With a regular button it works fine.
A: 

Okay, so using delegates is the way to make the print dialog work, but how do you get the dialog result value back?