Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 138,235 times

Contents

Related Categories

RichTextBox Control - Adding View Modes

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.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: Read Only Property

    Posted by mmarksbury on 24 Apr 2008

    This is long past the topic post date but...

     I used the selection method (where you select text and then update it), for formatting the output of an application's process in a RTF. ...

  • Re: Read Only Property

    Posted by magiel on 06 Jan 2007

    I am had the same proplems;


    RichTextBox1.SelectionFont.Bold
    RichTextBox1.SelectionFont.Underline etc are readonly


    Solved them the following way


    ...

  • Re: Pre-formatted text/Cursor position

    Posted by Verdi on 21 Sep 2006

    Kylua, your efforts were not wasted...


    You have probably saved me a week's worth of head-pounding. Why it is so difficult to programmatically modify RTF using this control is beyond me. All...

  • Re: Pre-formatted text/Cursor position

    Posted by Kylua on 31 Jul 2006

    I spent AGES working thru this one and couldn't find it anywhere!


    The basic problem is that you have to update the richtextbox.rtf, not .text.


    And it is very fussy about goes in the...

  • can't get EM_SETTEXTMODE to respond

    Posted by vb_lover on 06 Dec 2005

    James Crowley:

    I am using vb6sp6(EM_SETTEXTMODE didn't work with vb6sp4 either) RTB control, I have already implemented URL detection similar to yours. However now i am trying to get MULTI-LEVEL U...