Solved

Common Dialog ShowPrinter Cancel

Posted on 2000-04-27
8
1,263 Views
Last Modified: 2012-05-04
I'm having trouble canceling from the show printer screen.

Here's what I'm using

CommonDialog1.ShowPrinter
RichTextBox1.SelPrint CommonDialog1.hDC
0
Comment
Question by:aomega
  • 3
  • 3
  • 2
8 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 2755382
Use:

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

ErrHandler:
'User pressed Cancel
End Sub
0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2755393
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
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2756055
On Error Resume Next
Form1.ShowPrinter
If (Err.Number <> cdlCancel) Then
    '...do something...
End IF
On Error Goto 0
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2756754
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?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 9

Expert Comment

by:GivenRandy
ID: 2756848
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.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2756919
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.
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2757769
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.
0
 
LVL 3

Accepted Solution

by:
paulstamp earned 25 total points
ID: 2889954
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


0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now