Advertisement
JayBeeOH

TxtEditor - Edit Replace Option.

Mar 19th, 2015
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.97 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2015. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Class Name:     ReplaceTextForm
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   Mar 2015
  16. '
  17. ' Description:    Using a Singleton design pattern for a modeless form, this class searches
  18. '                 through MainForm's MyTextBox with the help of the FindTextForm' instance.
  19. '                 Then, using the String.Replace method, text is replace with the new
  20. '                 replacement text.
  21. '
  22. '                 Documentation is at:
  23. '                   Form's screen image is at http://i.imgur.com/8R32OJQ.png
  24. '                   App's Visual Basic .NET code is at http://pastebin.com/u/jaybeeoh
  25. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  26. '-------------------------------------------------------------------------------------------
  27.  
  28. Public Class ReplaceTextForm
  29.  
  30. #Region " Class Level Variables Declared"
  31.  
  32.     Shared myInstance As ReplaceTextForm = Nothing
  33.     Private Shared ReadOnly aLock As New Object
  34.  
  35. #End Region
  36.  
  37. #Region " Private Methods and Functions"
  38.  
  39.     ' Change constructor to a private method.
  40.     Private Sub New()
  41.  
  42.         InitializeComponent()
  43.     End Sub
  44.  
  45.     ' Retrieve last findString value.
  46.     Private Sub ReplaceForm_Activated(sender As Object, e As EventArgs) _
  47.         Handles Me.Activated
  48.  
  49.         If MainForm.findString <> String.Empty Then
  50.             FindTextBox.Text = MainForm.findString
  51.         End If
  52.     End Sub
  53.  
  54.     ' On the FormClosing event hide the form, instead of closing it.
  55.     Private Sub ReplaceForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
  56.         Handles Me.FormClosing
  57.  
  58.         ' Cancel the Closing event from closing the form.
  59.         e.Cancel = True
  60.         Me.Hide()
  61.     End Sub
  62.  
  63.  
  64.     ' On the CancelButton Click event hide the form, instead of closing it
  65.     Private Sub CancelButton_Click(sender As Object, e As EventArgs) _
  66.         Handles CancelButton.Click
  67.  
  68.         Me.Hide()
  69.     End Sub
  70.  
  71.     ' Find text.
  72.     Private Sub FindNextButton_Click(sender As Object, e As EventArgs) _
  73.         Handles FindNextButton.Click
  74.  
  75.         FindText()
  76.     End Sub
  77.  
  78.     ' When Find Text changes, update MainForm's findStrng variable.
  79.     Private Sub FindTextBox_TextChanged(sender As Object, e As EventArgs) _
  80.         Handles FindTextBox.TextChanged
  81.  
  82.         If (FindTextBox.Text.Length > 0) Then
  83.             MainForm.findString = FindTextBox.Text
  84.             MainForm.MyTextBox.Select(MainForm.MyTextBox.SelectionStart, 0)
  85.             FindNextButton.Enabled = True
  86.         Else
  87.             MainForm.findString = String.Empty
  88.             FindNextButton.Enabled = False
  89.             ReplaceButton.Enabled = False
  90.             ReplaceAllButton.Enabled = False
  91.         End If
  92.     End Sub
  93.  
  94.     ' When Replace Text changes, update buttons' status.
  95.     Private Sub ReplaceTextBox_TextChanged(sender As Object, e As EventArgs) _
  96.         Handles ReplaceTextBox.TextChanged
  97.  
  98.         If (ReplaceTextBox.Text.Length > 0) Then
  99.             ReplaceButton.Enabled = True
  100.             ReplaceAllButton.Enabled = True
  101.         Else
  102.             ReplaceButton.Enabled = False
  103.             ReplaceAllButton.Enabled = False
  104.         End If
  105.     End Sub
  106.  
  107.     ' Replace text.
  108.     Private Sub ReplaceButton_Click(sender As Object, e As EventArgs) _
  109.         Handles ReplaceButton.Click
  110.  
  111.         With MainForm.MyTextBox
  112.             If .SelectedText <> String.Empty Then
  113.                 .SelectedText = ReplaceTextBox.Text
  114.                 .Modified = True
  115.                 .ScrollToCaret()
  116.                 .Focus()
  117.             End If
  118.         End With
  119.         FindNextButton_Click(sender, e)
  120.     End Sub
  121.  
  122.     ' Replace all text.
  123.     Private Sub ReplaceAllButton_Click(sender As Object, e As EventArgs) _
  124.         Handles ReplaceAllButton.Click
  125.  
  126.         Dim foundPosition As Integer
  127.         If MatchCaseCheckBox.Checked Then   ' case sensitive
  128.             foundPosition = MainForm.MyTextBox.Text.IndexOf(FindTextBox.Text, 0, StringComparison.Ordinal)
  129.             If foundPosition = -1 Then
  130.                 MessageBox.Show("Cannot find """ & FindTextBox.Text & """",
  131.                 MainForm.applicationTitle,
  132.                 MessageBoxButtons.OK,
  133.                 MessageBoxIcon.Information)
  134.                 FindTextBox.Focus()
  135.                 FindTextBox.SelectAll()
  136.                 Exit Sub
  137.             Else
  138.                 MainForm.MyTextBox.Text = Replace(MainForm.MyTextBox.Text,
  139.                                                   FindTextBox.Text,
  140.                                                   ReplaceTextBox.Text, 1, -1,
  141.                                                   CompareMethod.Binary)
  142.                 MainForm.MyTextBox.Modified = True
  143.                 MainForm.MyTextBox.Select(0, 0)
  144.             End If
  145.         Else                                ' case insensitive
  146.             foundPosition = MainForm.MyTextBox.Text.IndexOf(FindTextBox.Text, 0, StringComparison.OrdinalIgnoreCase)
  147.             If foundPosition = -1 Then
  148.                 MessageBox.Show("Cannot find """ & FindTextBox.Text & """",
  149.                                 MainForm.applicationTitle,
  150.                                 MessageBoxButtons.OK,
  151.                                 MessageBoxIcon.Information)
  152.                 FindTextBox.Focus()
  153.                 FindTextBox.SelectAll()
  154.                 Exit Sub
  155.             Else
  156.                 MainForm.MyTextBox.Text = Replace(MainForm.MyTextBox.Text,
  157.                                                   FindTextBox.Text,
  158.                                                   ReplaceTextBox.Text, 1, -1,
  159.                                                   CompareMethod.Text)
  160.                 MainForm.MyTextBox.Modified = True
  161.                 MainForm.MyTextBox.Select(0, 0)
  162.             End If
  163.         End If
  164.  
  165.     End Sub
  166.  
  167.     ' Find text in MainForm's MyTextBox
  168.     Private Sub FindText()
  169.  
  170.         Dim startPosition As Integer
  171.         Dim foundPosition As Integer
  172.  
  173.         With MainForm.MyTextBox
  174.             If startPosition <> .SelectionStart Then
  175.                 If DownRadioButton.Checked Then
  176.                     startPosition = .SelectionStart + 1
  177.                     If startPosition > .Text.Length Then
  178.                         startPosition = .Text.Length
  179.                     End If
  180.                 Else
  181.                     startPosition = .SelectionStart - 1
  182.                     If startPosition < 0 Then
  183.                         startPosition = 0
  184.                     End If
  185.                 End If
  186.             End If
  187.  
  188.             If DownRadioButton.Checked Then
  189.                 If MatchCaseCheckBox.Checked Then
  190.                     foundPosition = .Text.IndexOf(MainForm.findString, startPosition, StringComparison.Ordinal)
  191.                 Else
  192.                     foundPosition = .Text.IndexOf(MainForm.findString, startPosition, StringComparison.OrdinalIgnoreCase)
  193.                 End If
  194.             Else
  195.                 If MatchCaseCheckBox.Checked Then
  196.                     foundPosition = .Text.LastIndexOf(MainForm.findString, startPosition, StringComparison.Ordinal)
  197.                 Else
  198.                     foundPosition = .Text.LastIndexOf(MainForm.findString, startPosition, StringComparison.OrdinalIgnoreCase)
  199.                 End If
  200.             End If
  201.  
  202.             If foundPosition = -1 Then
  203.                 If DialogResult.Yes = MessageBox.Show("Cannot find """ & MainForm.findString & """. Reverse search direction?",
  204.                                 MainForm.applicationTitle,
  205.                                 MessageBoxButtons.YesNo,
  206.                                 MessageBoxIcon.Question) Then
  207.                     DownRadioButton.Checked = Not DownRadioButton.Checked
  208.                     UpRadioButton.Checked = Not DownRadioButton.Checked
  209.                 End If
  210.                 .ScrollToCaret()
  211.                 .Focus()
  212.                 Exit Sub
  213.             End If
  214.  
  215.             .Select(foundPosition, MainForm.findString.Length)
  216.             .ScrollToCaret()
  217.             .Focus()
  218.         End With
  219.     End Sub
  220.  
  221. #End Region
  222.  
  223. #Region " Public Methods and Functions"
  224.  
  225.     ' Use factory method to instantiate class.
  226.     Public Shared Function GetInstance() As ReplaceTextForm
  227.         ' Thread safe.
  228.         SyncLock aLock
  229.             ' If constructor has not been evoked, call constructor and return instance.
  230.             ' Otherwise, return the previously created instance.
  231.             If myInstance Is Nothing Then
  232.                 myInstance = New ReplaceTextForm
  233.             End If
  234.             Return myInstance
  235.         End SyncLock
  236.     End Function
  237.  
  238. #End Region
  239.  
  240. End Class
  241.  
  242. '=======================================================================================================================
  243. Changes to MainForm's code --
  244.  
  245. Public Class MainForm
  246.  
  247. #Region "  Declare Class Level Variables"
  248.  
  249.     Public findString As String = String.Empty
  250.  
  251. #End Region
  252.  
  253. #Region " Find / Find Next / Replace"
  254.  
  255.     ' Edit Replace option - Show ReplaceTextForm.
  256.     Private Sub ReplaceToolStripMenuItem_Click(sender As Object, e As EventArgs) _
  257.         Handles ReplaceToolStripMenuItem.Click
  258.  
  259.         ' Instanciate and display ReplaceTextForm form.
  260.         Dim aReplaceTextForm As ReplaceTextForm = ReplaceTextForm.GetInstance()
  261.         aReplaceTextForm.Show()
  262.     End Sub
  263.  
  264. #End Region
  265.  
  266. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement