(NET) NET (2019)

<< Multi Languages Spell Checker for webmaster. Part 6. TextEditor with highlighting, searching, line numbering and FTP-uploading. <<


Sorry, no time for more details description, only code.



   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:          Me.BringToFront()
  24:          SearchToolStripTextBox.Text = Txt
  25:          Dim M As MatchCollection = Regex.Matches(HtmlTxt1, Txt)
  26:          SearchCountToolStripLabel.Text = M.Count
  27:          Dim Start1 As Integer = 0
  28:          SelectedLines = New List(Of Integer)
  29:          For I As Integer = 0 To M.Count - 1
  30:              Dim One As Match = M(I)
  31:              Start1 = RichTextBox1.Find(Txt, Start1, RichTextBoxFinds.None)
  32:              If Start1 < 0 Then
  33:                  AutoClosingMessageBox.Show(Txt & vbCrLf & "not found")
  34:                  Exit Sub
  35:              Else
  36:                  RichTextBox1.Select(Start1, Txt.Length)
  37:                  RichTextBox1.SelectionColor = Color.Red
  38:                  SelectedLines.Add(RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart))
  39:                  RichTextBox1.DeselectAll()
  40:                  Start1 += Txt.Length + 1
  41:              End If
  42:          Next
  43:      End Sub
  44:   
  45:      Private Sub EditForm_Load(sender As Object, e As EventArgs) Handles Me.Load
  46:          SearchToolStripTextBox.Text = ""
  47:          SearchCountToolStripLabel.Text = ""
  48:          For Each One As ToolStripItem In ToolStrip1.Items
  49:              One.Enabled = False
  50:          Next
  51:      End Sub
  52:   
  53:      Private Sub SaveToolStripButton_Click(sender As Object, e As EventArgs) Handles SaveToolStripButton.Click
  54:          Try
  55:              My.Computer.FileSystem.WriteAllText(WorkingFileName, RichTextBox1.Text.Replace(vbLf, vbCrLf), False, System.Text.Encoding.UTF8)
  56:              My.Computer.FileSystem.WriteAllText(IO.Path.Combine(GetTempFolderName, "log.txt"), Now & " S " & WorkingFileName & vbCrLf, True)
  57:              RefreshToolStripButton_Click(Nothing, Nothing)
  58:              Text = "Edit"
  59:          Catch ex As Exception
  60:              MsgBox(ex.Message)
  61:          End Try
  62:      End Sub
  63:   
  64:      Private Sub BGW1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW1.DoWork
  65:          HtmlTxt1 = My.Computer.FileSystem.ReadAllText(e.Argument.FileName)
  66:      End Sub
  67:   
  68:      Private Sub BGW1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW1.RunWorkerCompleted
  69:          RichTextBox1.Text = HtmlTxt1
  70:          RichTextBox1.Select()
  71:          AddLineNumbers()
  72:          For Each One As ToolStripItem In ToolStrip1.Items
  73:              One.Enabled = True
  74:          Next
  75:      End Sub
  76:   
  77:  #Region "LineNumber"
  78:   
  79:      'Dim WithEvents BGW2 As New BackgroundWorker
  80:      'Dim BGW2_Prm As New Object
  81:   
  82:      'Private Sub BGW2_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BGW2.ProgressChanged
  83:      '    Enumerate()
  84:      'End Sub
  85:   
  86:      Public Sub AddLineNumbers()
  87:          Enumerate()
  88:          'If Not BGW2.IsBusy Then
  89:          '    BGW2.RunWorkerAsync()
  90:          'End If
  91:      End Sub
  92:   
  93:   
  94:   
  95:      Sub Enumerate()
  96:          Dim pt As Point = New Point(0, 0)
  97:          Dim First_Index As Integer = RichTextBox1.GetCharIndexFromPosition(pt)
  98:          Dim First_Line As Integer = RichTextBox1.GetLineFromCharIndex(First_Index)
  99:          pt.X = ClientRectangle.Width
 100:          pt.Y = ClientRectangle.Height
 101:          Dim Last_Index As Integer = RichTextBox1.GetCharIndexFromPosition(pt)
 102:          Dim Last_Line As Integer = RichTextBox1.GetLineFromCharIndex(Last_Index)
 103:          If First_Line = 0 And Last_Line = 0 Then Exit Sub
 104:          For ShowLines As Integer = First_Line To Last_Line + 2
 105:              Dim IsPresent As Integer = 0
 106:              If SelectedLines Is Nothing Then
 107:                  'first load, still no searching
 108:                  LineNumberTextBox.AppendColorText(ShowLines + 1 & vbCrLf, Color.Gray)
 109:              Else
 110:                  IsPresent = SelectedLines.FirstOrDefault(Function(Z) Z = ShowLines)
 111:                  If IsPresent > 0 Then
 112:                      LineNumberTextBox.AppendColorText(ShowLines + 1 & vbCrLf, Color.Red)
 113:                  Else
 114:                      LineNumberTextBox.AppendColorText(ShowLines + 1 & vbCrLf, Color.Gray)
 115:                  End If
 116:              End If
 117:          Next
 118:          RichTextBox1.Select()
 119:          LineNumberTextBox.DeselectAll()
 120:      End Sub
 121:   
 122:      Private Sub RichTextBox1_VScroll(sender As Object, e As EventArgs) Handles RichTextBox1.VScroll
 123:          LineNumberTextBox.Text = ""
 124:          AddLineNumbers()
 125:          LineNumberTextBox.Invalidate()
 126:      End Sub
 127:   
 128:      Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
 129:          If RichTextBox1.Text = "" Then
 130:              AddLineNumbers()
 131:          End If
 132:          Text &= "*"
 133:      End Sub
 134:   
 135:      Private Sub RichTextBox1_FontChanged(sender As Object, e As EventArgs) Handles RichTextBox1.FontChanged
 136:          RichTextBox1.Select()
 137:          AddLineNumbers()
 138:      End Sub
 139:   
 140:      Private Sub RichTextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseDown
 141:          RichTextBox1.Select()
 142:          LineNumberTextBox.DeselectAll()
 143:      End Sub
 144:   
 145:      Private Sub RichTextBox1_Resize(sender As Object, e As EventArgs) Handles RichTextBox1.Resize
 146:          AddLineNumbers()
 147:      End Sub
 148:   
 149:      Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged
 150:          Dim pt As Point = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)
 151:          If pt.X = 1 Then
 152:              AddLineNumbers()
 153:          End If
 154:      End Sub
 155:   
 156:      Private Sub RefreshToolStripButton_Click(sender As Object, e As EventArgs) Handles RefreshToolStripButton.Click
 157:          SearchToolStripTextBox.Text = ""
 158:          SearchCountToolStripLabel.Text = ""
 159:          RichTextBox1.SelectAll()
 160:          RichTextBox1.SelectionColor = Color.Black
 161:          RichTextBox1.DeselectAll()
 162:          If SelectedLines IsNot Nothing Then SelectedLines.Clear()
 163:          EditNewFile(WorkingFileName)
 164:      End Sub
 165:   
 166:      Private Sub FTPToolStripButton_Click(sender As Object, e As EventArgs) Handles FTPToolStripButton.Click
 167:          Try
 168:              Dim RG As New Registry("SpellChecker")
 169:              Dim FTPHost As String = RG.GetValue(Of String)("FtpHost")
 170:              Dim FtpName As String = RG.GetValue(Of String)("FtpName")
 171:              Dim FtpPass As String = RG.GetValue(Of String)("FtpPass")
 172:              Dim X = New WebClient()
 173:              X.Credentials = New NetworkCredential(FtpName, FtpPass)
 174:              Dim ServerPath = "ftp://" & FTPHost & WorkingFileName.Replace("E:\vb-net", "").Replace("\", "/")
 175:              X.UploadFile(ServerPath, WebRequestMethods.Ftp.UploadFile, WorkingFileName)
 176:              My.Computer.FileSystem.WriteAllText(IO.Path.Combine(GetTempFolderName, "log.txt"), Now & " U " & WorkingFileName & vbCrLf, True)
 177:              AutoClosingMessageBox.Show(WorkingFileName & vbCrLf & "Uploaded")
 178:          Catch ex As Exception
 179:              MsgBox(ex.Message)
 180:          End Try
 181:      End Sub
 182:   
 183:      Private Sub SearchToolStripButton_Click(sender As Object, e As EventArgs) Handles SearchToolStripButton.Click
 184:          Search(SearchToolStripTextBox.Text)
 185:      End Sub
 186:   
 187:   
 188:  #End Region
 189:   
 190:  End Class


Comments ( )
Link to this page: //www.vb-net.com/SpellChecker/TextEditor.htm
< THANKS ME>