Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2015. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Class Name: ReplaceTextForm
- '
- ' Author: Joseph L. Bolen
- ' Date Created: Mar 2015
- '
- ' Description: Using a Singleton design pattern for a modeless form, this class searches
- ' through MainForm's MyTextBox with the help of the FindTextForm' instance.
- ' Then, using the String.Replace method, text is replace with the new
- ' replacement text.
- '
- ' Documentation is at:
- ' Form's screen image is at http://i.imgur.com/8R32OJQ.png
- ' App's Visual Basic .NET code is at http://pastebin.com/u/jaybeeoh
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Public Class ReplaceTextForm
- #Region " Class Level Variables Declared"
- Shared myInstance As ReplaceTextForm = Nothing
- Private Shared ReadOnly aLock As New Object
- #End Region
- #Region " Private Methods and Functions"
- ' Change constructor to a private method.
- Private Sub New()
- InitializeComponent()
- End Sub
- ' Retrieve last findString value.
- Private Sub ReplaceForm_Activated(sender As Object, e As EventArgs) _
- Handles Me.Activated
- If MainForm.findString <> String.Empty Then
- FindTextBox.Text = MainForm.findString
- End If
- End Sub
- ' On the FormClosing event hide the form, instead of closing it.
- Private Sub ReplaceForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
- Handles Me.FormClosing
- ' Cancel the Closing event from closing the form.
- e.Cancel = True
- Me.Hide()
- End Sub
- ' On the CancelButton Click event hide the form, instead of closing it
- Private Sub CancelButton_Click(sender As Object, e As EventArgs) _
- Handles CancelButton.Click
- Me.Hide()
- End Sub
- ' Find text.
- Private Sub FindNextButton_Click(sender As Object, e As EventArgs) _
- Handles FindNextButton.Click
- FindText()
- End Sub
- ' When Find Text changes, update MainForm's findStrng variable.
- Private Sub FindTextBox_TextChanged(sender As Object, e As EventArgs) _
- Handles FindTextBox.TextChanged
- If (FindTextBox.Text.Length > 0) Then
- MainForm.findString = FindTextBox.Text
- MainForm.MyTextBox.Select(MainForm.MyTextBox.SelectionStart, 0)
- FindNextButton.Enabled = True
- Else
- MainForm.findString = String.Empty
- FindNextButton.Enabled = False
- ReplaceButton.Enabled = False
- ReplaceAllButton.Enabled = False
- End If
- End Sub
- ' When Replace Text changes, update buttons' status.
- Private Sub ReplaceTextBox_TextChanged(sender As Object, e As EventArgs) _
- Handles ReplaceTextBox.TextChanged
- If (ReplaceTextBox.Text.Length > 0) Then
- ReplaceButton.Enabled = True
- ReplaceAllButton.Enabled = True
- Else
- ReplaceButton.Enabled = False
- ReplaceAllButton.Enabled = False
- End If
- End Sub
- ' Replace text.
- Private Sub ReplaceButton_Click(sender As Object, e As EventArgs) _
- Handles ReplaceButton.Click
- With MainForm.MyTextBox
- If .SelectedText <> String.Empty Then
- .SelectedText = ReplaceTextBox.Text
- .Modified = True
- .ScrollToCaret()
- .Focus()
- End If
- End With
- FindNextButton_Click(sender, e)
- End Sub
- ' Replace all text.
- Private Sub ReplaceAllButton_Click(sender As Object, e As EventArgs) _
- Handles ReplaceAllButton.Click
- Dim foundPosition As Integer
- If MatchCaseCheckBox.Checked Then ' case sensitive
- foundPosition = MainForm.MyTextBox.Text.IndexOf(FindTextBox.Text, 0, StringComparison.Ordinal)
- If foundPosition = -1 Then
- MessageBox.Show("Cannot find """ & FindTextBox.Text & """",
- MainForm.applicationTitle,
- MessageBoxButtons.OK,
- MessageBoxIcon.Information)
- FindTextBox.Focus()
- FindTextBox.SelectAll()
- Exit Sub
- Else
- MainForm.MyTextBox.Text = Replace(MainForm.MyTextBox.Text,
- FindTextBox.Text,
- ReplaceTextBox.Text, 1, -1,
- CompareMethod.Binary)
- MainForm.MyTextBox.Modified = True
- MainForm.MyTextBox.Select(0, 0)
- End If
- Else ' case insensitive
- foundPosition = MainForm.MyTextBox.Text.IndexOf(FindTextBox.Text, 0, StringComparison.OrdinalIgnoreCase)
- If foundPosition = -1 Then
- MessageBox.Show("Cannot find """ & FindTextBox.Text & """",
- MainForm.applicationTitle,
- MessageBoxButtons.OK,
- MessageBoxIcon.Information)
- FindTextBox.Focus()
- FindTextBox.SelectAll()
- Exit Sub
- Else
- MainForm.MyTextBox.Text = Replace(MainForm.MyTextBox.Text,
- FindTextBox.Text,
- ReplaceTextBox.Text, 1, -1,
- CompareMethod.Text)
- MainForm.MyTextBox.Modified = True
- MainForm.MyTextBox.Select(0, 0)
- End If
- End If
- End Sub
- ' Find text in MainForm's MyTextBox
- Private Sub FindText()
- Dim startPosition As Integer
- Dim foundPosition As Integer
- With MainForm.MyTextBox
- If startPosition <> .SelectionStart Then
- If DownRadioButton.Checked Then
- startPosition = .SelectionStart + 1
- If startPosition > .Text.Length Then
- startPosition = .Text.Length
- End If
- Else
- startPosition = .SelectionStart - 1
- If startPosition < 0 Then
- startPosition = 0
- End If
- End If
- End If
- If DownRadioButton.Checked Then
- If MatchCaseCheckBox.Checked Then
- foundPosition = .Text.IndexOf(MainForm.findString, startPosition, StringComparison.Ordinal)
- Else
- foundPosition = .Text.IndexOf(MainForm.findString, startPosition, StringComparison.OrdinalIgnoreCase)
- End If
- Else
- If MatchCaseCheckBox.Checked Then
- foundPosition = .Text.LastIndexOf(MainForm.findString, startPosition, StringComparison.Ordinal)
- Else
- foundPosition = .Text.LastIndexOf(MainForm.findString, startPosition, StringComparison.OrdinalIgnoreCase)
- End If
- End If
- If foundPosition = -1 Then
- If DialogResult.Yes = MessageBox.Show("Cannot find """ & MainForm.findString & """. Reverse search direction?",
- MainForm.applicationTitle,
- MessageBoxButtons.YesNo,
- MessageBoxIcon.Question) Then
- DownRadioButton.Checked = Not DownRadioButton.Checked
- UpRadioButton.Checked = Not DownRadioButton.Checked
- End If
- .ScrollToCaret()
- .Focus()
- Exit Sub
- End If
- .Select(foundPosition, MainForm.findString.Length)
- .ScrollToCaret()
- .Focus()
- End With
- End Sub
- #End Region
- #Region " Public Methods and Functions"
- ' Use factory method to instantiate class.
- Public Shared Function GetInstance() As ReplaceTextForm
- ' Thread safe.
- SyncLock aLock
- ' If constructor has not been evoked, call constructor and return instance.
- ' Otherwise, return the previously created instance.
- If myInstance Is Nothing Then
- myInstance = New ReplaceTextForm
- End If
- Return myInstance
- End SyncLock
- End Function
- #End Region
- End Class
- '=======================================================================================================================
- Changes to MainForm's code --
- Public Class MainForm
- #Region " Declare Class Level Variables"
- Public findString As String = String.Empty
- #End Region
- #Region " Find / Find Next / Replace"
- ' Edit Replace option - Show ReplaceTextForm.
- Private Sub ReplaceToolStripMenuItem_Click(sender As Object, e As EventArgs) _
- Handles ReplaceToolStripMenuItem.Click
- ' Instanciate and display ReplaceTextForm form.
- Dim aReplaceTextForm As ReplaceTextForm = ReplaceTextForm.GetInstance()
- aReplaceTextForm.Show()
- End Sub
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement