(NET) NET (2012)

RichTextBox Editor for various purposes with row numbering and searching.


In this page I'll describe a small form for text edition using RichTextBox. This form usually I call in MDI-application as part of any another my program, therefor this form has external control (Sub EditNewFile) for loading text and (Sub Search) for searching text. You can use this form as independently, but it need to add two button in toolbar calling EditNewFile and Search.

This is name of controls in this form:



And this is full code of form.


   1:  Imports System.ComponentModel
   2:  Imports System.Net
   3:  Imports System.Text.RegularExpressions
   4:  Imports RichTextBoxExt
   5:   
   6:  Public Class EditForm
   7:   
   8:      Dim HtmlTxt1 As String
   9:      Dim WorkingFileName As String
  10:      Dim WithEvents BGW1 As BackgroundWorker
  11:      Dim BGW1_Prm As New Object
  12:   
  13:   
  14:      Public Sub EditNewFile(FileName As String)
  15:          WorkingFileName = FileName
  16:          BGW1 = New BackgroundWorker
  17:          BGW1_Prm = New With {FileName}
  18:          BGW1.RunWorkerAsync(BGW1_Prm)
  19:      End Sub
  20:   
  21:      Dim SelectedLines As List(Of Integer)
  22:      Public Sub Search(Txt As String)
  23:          SearchToolStripLabel.Text = Txt
  24:          Dim M As MatchCollection = Regex.Matches(HtmlTxt1, Txt)
  25:          SearchCountToolStripLabel.Text = M.Count
  26:          Dim Start1 As Integer = 0
  27:          SelectedLines = New List(Of Integer)
  28:          For I As Integer = 0 To M.Count - 1
  29:              Dim One As Match = M(I)
  30:              Start1 = RichTextBox1.Find(Txt, Start1, RichTextBoxFinds.None)
  31:              RichTextBox1.Select(Start1, Txt.Length)
  32:              RichTextBox1.SelectionColor = Color.Red
  33:              SelectedLines.Add(RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart))
  34:              RichTextBox1.DeselectAll()
  35:              Start1 += Txt.Length + 1
  36:          Next
  37:      End Sub
  38:   
  39:      Private Sub EditForm_Load(sender As Object, e As EventArgs) Handles Me.Load
  40:          SearchToolStripLabel.Text = ""
  41:          SearchCountToolStripLabel.Text = ""
  42:      End Sub
  43:   
  44:      Private Sub SaveToolStripButton_Click(sender As Object, e As EventArgs) Handles SaveToolStripButton.Click
  45:          My.Computer.FileSystem.WriteAllText(WorkingFileName, RichTextBox1.Text.Replace(vbLf, vbCrLf), False, System.Text.Encoding.UTF8)
  46:          RefreshToolStripButton_Click(Nothing, Nothing)
  47:          Text = "EditForm"
  48:      End Sub
  49:   
  50:      Private Sub BGW1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW1.DoWork
  51:          HtmlTxt1 = My.Computer.FileSystem.ReadAllText(e.Argument.FileName)
  52:      End Sub
  53:   
  54:      Private Sub BGW1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW1.RunWorkerCompleted
  55:          RichTextBox1.Text = HtmlTxt1
  56:          RichTextBox1.Select()
  57:          AddLineNumbers()
  58:      End Sub
  59:   
  60:  #Region "LineNumber"
  61:   
  62:      Public Sub AddLineNumbers()
  63:          Enumerate()
  64:      End Sub
  65:   
  66:   
  67:   
  68:      Sub Enumerate()
  69:          Dim pt As Point = New Point(0, 0)
  70:          Dim First_Index As Integer = RichTextBox1.GetCharIndexFromPosition(pt)
  71:          Dim First_Line As Integer = RichTextBox1.GetLineFromCharIndex(First_Index)
  72:          pt.X = ClientRectangle.Width
  73:          pt.Y = ClientRectangle.Height
  74:          Dim Last_Index As Integer = RichTextBox1.GetCharIndexFromPosition(pt)
  75:          Dim Last_Line As Integer = RichTextBox1.GetLineFromCharIndex(Last_Index)
  76:          If First_Line = 0 And Last_Line = 0 Then Exit Sub
  77:          For ShowLines As Integer = First_Line To Last_Line + 2
  78:              Dim IsPresent As Integer = 0
  79:              If SelectedLines Is Nothing Then
  80:                  'first load, still no searching
  81:                  LineNumberTextBox.AppendColorText(ShowLines + 1 & vbCrLf, Color.Gray)
  82:              Else
  83:                  IsPresent = SelectedLines.FirstOrDefault(Function(Z) Z = ShowLines)
  84:                  If IsPresent > 0 Then
  85:                      LineNumberTextBox.AppendColorText(ShowLines + 1 & vbCrLf, Color.Red)
  86:                  Else
  87:                      LineNumberTextBox.AppendColorText(ShowLines + 1 & vbCrLf, Color.Gray)
  88:                  End If
  89:              End If
  90:          Next
  91:          RichTextBox1.Select()
  92:          LineNumberTextBox.DeselectAll()
  93:      End Sub
  94:   
  95:      Private Sub RichTextBox1_VScroll(sender As Object, e As EventArgs) Handles RichTextBox1.VScroll
  96:          LineNumberTextBox.Text = ""
  97:          AddLineNumbers()
  98:          LineNumberTextBox.Invalidate()
  99:      End Sub
 100:   
 101:      Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
 102:          If RichTextBox1.Text = "" Then
 103:              AddLineNumbers()
 104:          End If
 105:          Text &= "*"
 106:      End Sub
 107:   
 108:      Private Sub RichTextBox1_FontChanged(sender As Object, e As EventArgs) Handles RichTextBox1.FontChanged
 109:          RichTextBox1.Select()
 110:          AddLineNumbers()
 111:      End Sub
 112:   
 113:      Private Sub RichTextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseDown
 114:          RichTextBox1.Select()
 115:          LineNumberTextBox.DeselectAll()
 116:      End Sub
 117:   
 118:      Private Sub RichTextBox1_Resize(sender As Object, e As EventArgs) Handles RichTextBox1.Resize
 119:          AddLineNumbers()
 120:      End Sub
 121:   
 122:      Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged
 123:          Dim pt As Point = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
 124:          If pt.X = 1 Then
 125:              AddLineNumbers()
 126:          End If
 127:      End Sub
 128:   
 129:      Private Sub RefreshToolStripButton_Click(sender As Object, e As EventArgs) Handles RefreshToolStripButton.Click
 130:          SearchToolStripLabel.Text = ""
 131:          SearchCountToolStripLabel.Text = ""
 132:          RichTextBox1.SelectAll()
 133:          RichTextBox1.SelectionColor = Color.Black
 134:          RichTextBox1.DeselectAll()
 135:          If SelectedLines IsNot Nothing Then SelectedLines.Clear()
 136:      End Sub
 137:   
 138:      Private Sub FTPToolStripButton_Click(sender As Object, e As EventArgs) Handles FTPToolStripButton.Click
 139:          Dim X = New WebClient()
 140:          X.Credentials = New NetworkCredential(My.MySettings.Default.FtpName, My.MySettings.Default.FtpPass)
 141:          Dim ServerPath = "ftp://" & My.MySettings.Default.FtpHost & WorkingFileName.Replace("E:\vb-net", "").Replace("\", "/")
 142:          X.UploadFile(ServerPath, WebRequestMethods.Ftp.UploadFile, WorkingFileName)
 143:      End Sub
 144:   
 145:  #End Region
 146:   
 147:  End Class

This is small additional extension function I used in this form.


   1:  Imports System.Runtime.CompilerServices
   2:   
   3:  Module RichTextBoxExt
   4:   
   5:      <Extension>
   6:      Public Sub AppendColorText(R As RichTextBox, Txt As String, C As Color)
   7:          Dim Start1 As Integer = R.TextLength
   8:          R.AppendText(Txt)
   9:          Dim End1 As Integer = R.TextLength
  10:          R.Select(Start1, End1 - Start1)
  11:          R.SelectionColor = C
  12:      End Sub
  13:   
  14:  End Module


Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19
Link to this page: http://www.vb-net.com/RichTextBox/Index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <MAIL ME>  <ABOUT ME>  < THANKS ME>