JayBeeOH

TxtEditor - Edit Find & Find Next Options.

Mar 15th, 2015
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.25 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:     FindTextForm
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   Mar 2015
  16. '
  17. ' Description:    Using a Singleton design pattern for a modeless form,
  18. '                 the class uses the String.IndexOf and String.LastIndexOf methods
  19. '                 to search forward and backward through MainForm's MyTextBox to
  20. '                 find matching text.
  21. '
  22. '                 Documentation is at:
  23. '                   Form's screen image is at http://i.imgur.com/4xVTebj.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 FindTextForm
  29.  
  30. #Region " Class Level Variables Declared"
  31.  
  32.     Private Shared myInstance As FindTextForm = 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 FindForm_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 FindForm_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.     ' On the CancelButton Click event hide the form, instead of closing it.
  64.     Private Sub CancelButton_Click(sender As Object, e As EventArgs) _
  65.         Handles CancelButton.Click
  66.  
  67.         Me.Hide()
  68.     End Sub
  69.  
  70.     ' Find text.
  71.     Private Sub FindNextButton_Click(sender As Object, e As EventArgs) _
  72.         Handles FindNextButton.Click
  73.  
  74.         FindText()
  75.     End Sub
  76.  
  77.     ' When Find Text changes, update MainForm's findStrng variable.
  78.     Private Sub FindTextBox_TextChanged(sender As Object, e As EventArgs) _
  79.         Handles FindTextBox.TextChanged
  80.  
  81.         If (FindTextBox.Text.Length > 0) Then
  82.             MainForm.findString = FindTextBox.Text
  83.             'Unselect last find, but keep cursor at current location.
  84.             MainForm.MyTextBox.Select(MainForm.MyTextBox.SelectionStart, 0)
  85.             FindNextButton.Enabled = True
  86.         Else
  87.             MainForm.findString = String.Empty
  88.             FindNextButton.Enabled = False
  89.         End If
  90.     End Sub
  91.  
  92. #End Region
  93.  
  94. #Region " Public Methods and Functions"
  95.  
  96.     ' Use factory method to instantiate class.
  97.     Public Shared Function GetInstance() As FindTextForm
  98.         ' Thread safe.
  99.         SyncLock aLock
  100.             ' If constructor has not been evoked, call constructor and return instance.
  101.             ' Otherwise, return the previously created instance.
  102.             If myInstance Is Nothing Then
  103.                 myInstance = New FindTextForm
  104.             End If
  105.             Return myInstance
  106.         End SyncLock
  107.     End Function
  108.  
  109.     ' Find text in MainForm's MyTextBox
  110.     Public Sub FindText()
  111.  
  112.         Dim startPosition As Integer
  113.         Dim foundPosition As Integer
  114.  
  115.         With MainForm.MyTextBox
  116.             If startPosition <> .SelectionStart Then
  117.                 If DownRadioButton.Checked Then
  118.                     startPosition = .SelectionStart + 1
  119.                     If startPosition > .Text.Length Then
  120.                         startPosition = .Text.Length
  121.                     End If
  122.                 Else
  123.                     startPosition = .SelectionStart - 1
  124.                     If startPosition < 0 Then
  125.                         startPosition = 0
  126.                     End If
  127.                 End If
  128.             End If
  129.  
  130.             If DownRadioButton.Checked Then
  131.                 If MatchCaseCheckBox.Checked Then
  132.                     foundPosition = .Text.IndexOf(MainForm.findString, startPosition, StringComparison.Ordinal)
  133.                 Else
  134.                     foundPosition = .Text.IndexOf(MainForm.findString, startPosition, StringComparison.OrdinalIgnoreCase)
  135.                 End If
  136.             Else
  137.                 If MatchCaseCheckBox.Checked Then
  138.                     foundPosition = .Text.LastIndexOf(MainForm.findString, startPosition, StringComparison.Ordinal)
  139.                 Else
  140.                     foundPosition = .Text.LastIndexOf(MainForm.findString, startPosition, StringComparison.OrdinalIgnoreCase)
  141.                 End If
  142.             End If
  143.  
  144.             If foundPosition = -1 Then
  145.                 If DialogResult.Yes = MessageBox.Show("Cannot find """ & MainForm.findString & """. Reverse search direction?",
  146.                                 MainForm.applicationTitle,
  147.                                 MessageBoxButtons.YesNo,
  148.                                 MessageBoxIcon.Question) Then
  149.                     DownRadioButton.Checked = Not DownRadioButton.Checked
  150.                     UpRadioButton.Checked = Not DownRadioButton.Checked
  151.                 End If
  152.                 .ScrollToCaret()
  153.                 .Focus()
  154.                 Exit Sub
  155.             End If
  156.  
  157.             .Select(foundPosition, MainForm.findString.Length)
  158.             .ScrollToCaret()
  159.             .Focus()
  160.         End With
  161.     End Sub
  162.  
  163. #End Region
  164.  
  165. End Class
  166.  
  167. '=======================================================================================================================
  168. Changes to MainForm's code --
  169.  
  170. Public Class MainForm
  171.  
  172. #Region "  Declare Class Level Variables"
  173.  
  174.     Public findString As String = String.Empty
  175.  
  176. #End Region
  177.  
  178. #Region " Find / Find Next / Replace"
  179.  
  180.     ' Edit Find option - Show FindTextForm.
  181.     Private Sub FindToolStripMenuItem_Click(sender As Object, e As EventArgs) _
  182.         Handles FindToolStripMenuItem.Click
  183.  
  184.         ' Instanciate and display FindTextForm form.
  185.         Dim aFindTextForm As FindTextForm = FindTextForm.GetInstance()
  186.         aFindTextForm.Show()
  187.     End Sub
  188.  
  189.     ' Edit Find Next option - Using FindTextForm's FindText method.
  190.     Private Sub FindNextToolStripMenuItem_Click(sender As Object, e As EventArgs) _
  191.         Handles FindNextToolStripMenuItem.Click
  192.  
  193.         ' Instanciate and display FindTextForm form, then call FindText subroutine.
  194.         Dim aFindTextForm As FindTextForm = FindTextForm.GetInstance()
  195.         aFindTextForm.FindText()
  196.     End Sub
  197.  
  198. #End Region
  199.  
  200. End Class
Advertisement
Add Comment
Please, Sign In to add comment