Results 1 to 7 of 7

Thread: Print a RichTextBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2016
    Posts
    11

    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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2016
    Posts
    11

    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

  4. #4
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2016
    Posts
    11

    Re: Print a RichTextBox

    Thank's. Now is ok!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Print a RichTextBox

    Quote Originally Posted by gbendin View Post
    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Print a RichTextBox

    Quote Originally Posted by paulg4ije View Post
    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
  •  



Click Here to Expand Forum to Full Width