5 Replies - 2941 Views - Last Post: 07 September 2014 - 12:37 PM

#1 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,417
  • Joined: 12-December 12

RichTextBox Control Nuggets

Post icon  Posted 02 March 2014 - 01:07 PM

Add (append) text at the end
    Me.RichTextBox1.Selectionstart = Me.RichTextBox1.TextLength
    Me.RichTextBox1.AppendText("some text")


Get the clicked line number
    Private Sub RichTextBox1_Click(sender As Object, e As EventArgs) _
            Handles RichTextBox1.Click
        Dim clickedLine As Integer = 0
        clickedLine = sender.GetLineFromCharIndex(sender.Selectionstart)

        MessageBox.Show(String.Format("You clicked line {0:d}", clickedLine))
    End Sub


Colour specific word(s), or anything else matching Regex expression
Imports System.Text.RegularExpressions

    Dim reg As Regex = New Regex("\bhello\b", RegexOptions.Multiline Or RegexOptions.IgnoreCase)
    Dim rtb As RichTextBox = RichTextBox1
    Dim mtchs As MatchCollection = reg.Matches(rtb.Text)
    'Dim mat As Match = mtchs.Item(i)
    'rtb.Invalidate() - see comment below
    For Each m As Match In mtchs
        rtb.Selectionstart = m.Index
        rtb.SelectionLength = m.Length
        rtb.SelectionColor = Color.CadetBlue
    Next
    'rtb.Update()


Copy formatted text (Rtf) from one RTB to the end of a second RTB
    Clipboard.SetText(RichTextBox1.Rtf, TextDataFormat.Rtf)
    RichTextBox2.Select(RichTextBox2.TextLength - 1, 1)
    'RichTextBox2.ScrollToCaret()

    Dim myFormat As DataFormats.Format = DataFormats.GetFormat(DataFormats.Rtf)
    RichTextBox2.Paste(myFormat)


Combine the contents of two RTBs, using a third, and save the rich-text to a (Word) file:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim rich As RichTextBox = New RichTextBox()

        rich.Rtf = RichTextBox1.Rtf
        rich.Selectionstart = rich.TextLength
        rich.SelectionLength = 0
        rich.SelectedRtf = RichTextBox2.Rtf

        rich.SaveFile("C:\Users\Andrew\Documents\testrtf.docx", RichTextBoxStreamType.RichText)
    End Sub

This post has been edited by andrewsw: 07 September 2014 - 11:25 AM


Is This A Good Question/Topic? 2
  • +

Replies To: RichTextBox Control Nuggets

#2 msreed  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 22-March 09

Re: RichTextBox Control Nuggets

Posted 03 March 2014 - 12:42 PM

Thank you for these. Very useful.
Was This Post Helpful? 0
  • +
  • -

#3 satyamparmar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 01-April 14

Re: RichTextBox Control Nuggets

Posted 17 June 2014 - 12:48 AM

Sir how can autocomplete RichTextBox from customsource items?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,417
  • Joined: 12-December 12

Re: RichTextBox Control Nuggets

Posted 17 June 2014 - 12:54 AM

This Snippet has nothing to do with auto-complete. Create a new question in the appropriate forum.
Was This Post Helpful? 1
  • +
  • -

#5 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,417
  • Joined: 12-December 12

Re: RichTextBox Control Nuggets

Posted 07 September 2014 - 11:20 AM

For the colour-coding Invalidate() and Update() won't suspend re-painting as I originally thought, so remove those lines.

RichTextBoxes, unlike other controls, do not have BeginUpdate and EndUpdate methods to prevent continuous repainting. They can be implemented as follows (although this is in C#), which is worth considering if there is a lot of text, and keywords:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyRichTextBox : RichTextBox {
    public void BeginUpdate() {
        SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
    }
    public void EndUpdate() {
        SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero); 
        this.Invalidate();
    }
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    private const int WM_SETREDRAW = 0x0b;
}

From this SO topic.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,417
  • Joined: 12-December 12

Re: RichTextBox Control Nuggets

Posted 07 September 2014 - 12:37 PM

VB.NET translation of the previous code:
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Class MyRichTextBox
    Inherits RichTextBox

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, _
                                        ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function

    Private Const WM_SETREDRAW As Integer = &HB

    Public Sub BeginUpdate()
        SendMessage(Me.Handle, WM_SETREDRAW, CType(0, IntPtr), IntPtr.Zero)
    End Sub

    Public Sub EndUpdate()
        SendMessage(Me.Handle, WM_SETREDRAW, CType(1, IntPtr), IntPtr.Zero)
        Me.Invalidate()
    End Sub
End Class

This post has been edited by andrewsw: 07 September 2014 - 12:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1