-
Jul 1st, 2022, 04:21 AM
#1
Thread Starter
New Member
Print a RichTextBox
Hi,
i have a problem with a VB2013 Form APP on windows.
This is a old code that i try to modify.
In a "Public Class Form2" i try to print a content of RichTextBox.
but if i compile a project i receive the following error:
Error 7 Handles clause requires a WithEvents variable defined in the containing type or one of its base types. Form2.vb 2227 102 xxxxxxxxxxxxxxx
Error 8 'PrintDocument1' is not declared. It may be inaccessible due to its protection level. Form2.vb 2329 13 xxxxxxxxxxxxxxx
Error 9 'PrintDocument1' is not declared. It may be inaccessible due to its protection level. Form2.vb 2330 13 xxxxxxxxxxxxxxx
in the "Public Class Form2" i have the following code:
Error 7:
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(RichTextBox1.Text, RichTextBox1.Font, Brushes.Black, 6, 140)
End Sub
and:
Error 8/9:
PrintDocument1.PrinterSettings.Copies = 2
PrintDocument1.Print()
I suspect that i have missing something ........
Can help me?
-
Jul 1st, 2022, 04:51 AM
#2
Re: Print a RichTextBox
If you didn't have Button1 on your form, would you expect to be able to handle the Click event of of Button1? If you didn't have TextBox1 on your form, would you expect to be able to handle the TextChanged event of TextBox1. The answer to both questions should obviously be "no". Why, then, would you expect to be able to handle the PrintPage event of PrintDocument1 if you don't have PrintDocument1 on your form? The second and third error messages even tell you exactly what the problem is. The logic you already use doesn't stop working because the details of the situation change.
-
Jul 1st, 2022, 04:59 AM
#3
Thread Starter
New Member
Re: Print a RichTextBox
So i have to declare... PrintDocument1?
I'm not expert in this type of code, i try to merge piece of code found on web pages..... and this is the real problem
I don't know how to declare PrintDocument1
-
Jul 1st, 2022, 05:55 AM
#4
Re: Print a RichTextBox
There is a PrintDocument control in the toolbox. Drag it to your form and it will be called PrintDocument1 by default.
-
Jul 1st, 2022, 06:10 AM
#5
Thread Starter
New Member
-
Jul 1st, 2022, 06:12 AM
#6
Re: Print a RichTextBox
Originally Posted by gbendin
So i have to declare... PrintDocument1?
I'm not expert in this type of code, i try to merge piece of code found on web pages..... and this is the real problem
I don't know how to declare PrintDocument1
And I wonder what research you did to find out. You would generally add it to the form in the designer, just like you would a Button or a TextBox, just as my previous post implied. This is an example of why you should always do a bit of exploring when you're new to something to see what's available. When you started using the WinForms designer, you should have looked through the Toolbox to see what it contained. Then you'd have seen the PrintDocument listed and, when it came up later, known that you could go there and add one to your form.
-
Jul 1st, 2022, 06:15 AM
#7
Re: Print a RichTextBox
Originally Posted by paulg4ije
There is a PrintDocument control in the toolbox.
Just one small point: it's not a control. It's a component, which is what's required for designer support. A control is a type that inherits the Control class, which PrintDocument does not. A component is, strictly speaking, any type that implements IComponent, but generally means inheriting Component. Controls are specialised components. Other things that are not controls: BindingSource, Timer and ToolTip.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|