RichTextBox Control

Adding View Modes

The short code below allows you to easily add view modes to your RichTextBox code. This supports:
Default (ercDefault) '// WordWrap
NoWrap (ercWordWrap)
WYSIWYG (ercWYSIWYG) '// What you see is what you get.

To change the WYSIWYG settings (such as page orientation and paper size), simply change the printer properties, and call SetViewMode again. This will then allow for pages to be set to landscape, or an A5 page. Add this code to a module

You need Public Const WM_USER = &H400
Public Const EM_SETTARGETDEVICE = (WM_USER + 72)
Public Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

'// View Types
Public Enum ERECViewModes
   ercDefault = 0
   ercWordWrap = 1
   ercWYSIWYG = 2
End Enum

'// Sets the View Mode
Private Sub SetViewMode(ByVal eViewMode As ERECViewModes)
   Select Case eViewMode
   Case ercWYSIWYG
      On Error Resume Next
      SendMessageLong frmMain.rtfText.hWnd, EM_SETTARGETDEVICE, Printer.hDC, Printer.Width
   Case ercWordWrap
      SendMessageLong frmMain.rtfText.hWnd, EM_SETTARGETDEVICE, 0, 0
   Case ercDefault
      SendMessageLong frmMain.rtfText.hWnd, EM_SETTARGETDEVICE, 0, 1
   End Select
End Sub

This code has been adapted from VB Accelerator's RichEdit control.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler