Link to home
Start Free TrialLog in
Avatar of aomega
aomega

asked on

Common Dialog ShowPrinter Cancel

I'm having trouble canceling from the show printer screen.

Here's what I'm using

CommonDialog1.ShowPrinter
RichTextBox1.SelPrint CommonDialog1.hDC
Avatar of Erick37
Erick37
Flag of United States of America image

Use:

CommonDialog1.CancelError = True
On Error GoTo ErrHandler
CommonDialog1.ShowPrinter
'...
Exit Sub

ErrHandler:
'User pressed Cancel
End Sub
Avatar of paulstamp
paulstamp

What exactly is the problem ?

To detect cancel you can set the CancelError property to true and then put an error trap around your code, ie

On Error Goto Cancel

CommonDialog1.ShowPrinter

RichTextBox1.SelPrint CommonDialog1.hDC

Exit Sub

Cancel:

'Dont print anything

End Sub
On Error Resume Next
Form1.ShowPrinter
If (Err.Number <> cdlCancel) Then
    '...do something...
End IF
On Error Goto 0
GivenRandy, if you are going to lock a question with an answer, please be sure it at least works.  Paul and I have both given examples of how to use the CnacelError property in conjunction with On Error to achieve this.

Form1.ShowPrinter?
You're right.  I usually do test code segments before posting, but not this time -- haste makes waste.

Erick's answer is very similar to mine, although slightly different (mine avoids clumsy indirect "goto" statements).  I'll withdraw the lock and let the poster decide.
I would caution against using "On Error Resume Next" unless you are absolutely sure other runtime errors can safely be ignored in the code.

Error handlers are typically / usually implemented as "indirect" GoTo's.
We are both concerned with the same problem with VB (other languages handle it differently -- some better, some worse).  Like you said, if you don't have your functions adequately protected, it might be better to trap by doing the "indirect" method.

What happens is that an error condition that occurs will trigger this and go to whereever it is indicated.  In Erick's recommendation, it would bail to his GoTo point.  This is a good approach if you're not sure about the other code or how your own code functions at a deeper level.  However, if your code is more robust, I prefer my approach -- keep it close to the chest.
ASKER CERTIFIED SOLUTION
Avatar of paulstamp
paulstamp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial