VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "Mscomctl.ocx"
Begin VB.Form frmFindReplace 
   BorderStyle     =   4  'Fixed ToolWindow
   Caption         =   "Find and Replace"
   ClientHeight    =   2325
   ClientLeft      =   4350
   ClientTop       =   2310
   ClientWidth     =   4935
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   2325
   ScaleWidth      =   4935
   Begin MSComctlLib.ProgressBar ProgressBar1 
      Height          =   255
      Left            =   120
      TabIndex        =   9
      Top             =   1920
      Width           =   4695
      _ExtentX        =   8281
      _ExtentY        =   450
      _Version        =   393216
      Appearance      =   1
      Scrolling       =   1
   End
   Begin VB.CommandButton cmdCancel 
      Caption         =   "&Cancel"
      Height          =   355
      Left            =   3720
      TabIndex        =   6
      Top             =   1440
      Width           =   1095
   End
   Begin VB.CommandButton cmdFindNext 
      Caption         =   "&Find Next"
      Default         =   -1  'True
      Enabled         =   0   'False
      Height          =   355
      Left            =   120
      TabIndex        =   3
      ToolTipText     =   "Find next instance of search item"
      Top             =   1440
      Width           =   1095
   End
   Begin VB.CommandButton cmdReplace 
      Caption         =   "&Replace"
      Enabled         =   0   'False
      Height          =   355
      Left            =   1320
      TabIndex        =   4
      ToolTipText     =   "Replace current instance of search item with new item"
      Top             =   1440
      Width           =   1095
   End
   Begin VB.CheckBox chkCaseSense 
      Caption         =   "Case sensitive"
      Height          =   255
      Left            =   1440
      TabIndex        =   2
      Top             =   1000
      Width           =   1455
   End
   Begin VB.CommandButton cmdReplaceAll 
      Caption         =   "Replace &All"
      Enabled         =   0   'False
      Height          =   355
      Left            =   2520
      TabIndex        =   5
      ToolTipText     =   "Replace all search item instances with new item"
      Top             =   1440
      Width           =   1095
   End
   Begin VB.TextBox txtSearchTerm 
      Height          =   285
      Left            =   1440
      TabIndex        =   0
      Top             =   180
      Width           =   3375
   End
   Begin VB.TextBox txtReplaceWithString 
      Height          =   285
      Left            =   1440
      TabIndex        =   1
      Top             =   600
      Width           =   3375
   End
   Begin VB.Label Label1 
      Caption         =   "Find what:"
      Height          =   255
      Left            =   120
      TabIndex        =   8
      Top             =   195
      Width           =   1095
   End
   Begin VB.Label Label2 
      Caption         =   "Replace with:"
      Height          =   255
      Left            =   120
      TabIndex        =   7
      Top             =   600
      Width           =   1215
   End
End
Attribute VB_Name = "frmFindReplace"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'================================================================
'               Program Author: Krishna Kumar Khatri
'            © 2005, http://khatri-krishna.tripod.com
'                    zipcn046@nda.vsnl.net.in
'================================================================
'Modified by FollowTheWatch55 for Virtual Hypnotist

Private CancelSearch As Boolean

' This method is the public interface to SnR functionality.
Public Sub FindnReplace(ByRef Tb As TextBox)
    Set ScriptMaker.txtClient = Tb
    ScriptMaker.mnuEditFindNext.Enabled = True
    Me.Show , ScriptMaker.txtClient.parent
End Sub

Private Sub chkCaseSense_Click()
ScriptMaker.chkCaseSense = chkCaseSense.Value
End Sub

Private Sub cmdCancel_Click()
CancelSearch = True
Unload Me
End Sub

Private Sub FindReplace(Replace As Boolean, ReplaceAll As Boolean)
    Dim CaseSense As Integer
    Dim SourceText As String
    Dim SourceTextCopy As String

    ' Check for the case sensitivity options
    If (chkCaseSense.Value = vbChecked) Then
        CaseSense = 0
    Else
        CaseSense = 1
    End If

    ' One contains the original text and another contains replaced (updated) one
    ' Used to check whether a replacement was done or not
    SourceText = ScriptMaker.txtClient.Text
    SourceTextCopy = SourceText

    If Len(SourceText) = 0 Then Exit Sub

If ExceptionHandlers = True Then On Error GoTo ErrHandler
    Dim SearchTermLen As Integer
    Dim FndPos As Integer
    
    SearchTermLen = Len(txtSearchTerm.Text)

    If Replace = False Then ScriptMaker.Cnt = ScriptMaker.txtClient.SelStart + ScriptMaker.txtClient.SelLength + 1
    
' This is endless loop (terminated on a condition checked inside the loop body).
While (1)
    FndPos = InStr(ScriptMaker.Cnt, SourceText, txtSearchTerm.Text, CaseSense)
    DoEvents
    
    'Update progress bar
    ProgressBar1.Value = Int((ScriptMaker.Cnt / Len(SourceText)) * 100)
    
    If CancelSearch = True Then Exit Sub
    
    ' When a match is found replace it appropriately
    If (FndPos > 0) Then
        If Replace = True And (ReplaceAll = True Or ScriptMaker.txtClient.SelText = txtSearchTerm.Text) Then
            SourceText = ReplaceFun(SourceText, FndPos, Len(txtSearchTerm.Text), txtReplaceWithString.Text)
            ScriptMaker.txtClient.Text = SourceText
            ScriptMaker.txtClient.SelStart = FndPos
            ScriptMaker.txtClient.SelLength = 0
            If ReplaceAll = False Then
                Replace = False
                FndPos = 0
                ScriptMaker.Cnt = ScriptMaker.Cnt + 1
            End If
        Else
            ScriptMaker.txtClient.SelStart = FndPos - 1
            ScriptMaker.txtClient.SelLength = SearchTermLen
            ScriptMaker.SetFocus
            Exit Sub
        End If
    Else
        ScriptMaker.Cnt = ScriptMaker.Cnt + 1
    End If
    
    ' Whether a replacement was done at all or not
    If (ScriptMaker.Cnt >= Len(SourceText)) Then
        ScriptMaker.txtClient.Text = SourceText
        If ScriptMaker.ShowWindow = True Then Call SetWindowPos(Me.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, flags)
        If (SourceTextCopy <> SourceText) Then
            MsgBox "Finished replacing all occurrences.", vbInformation + vbOKOnly, "Replaced All"
        Else
            MsgBox "No matching strings found.", vbInformation + vbOKOnly, "No Replacement"
        End If
        If ScriptMaker.ShowWindow = True Then Call SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, flags)
        Exit Sub
    End If
    ' Else Restart from henceforth
Wend
Exit Sub

ErrHandler:
    Response = MsgBox("An error occurred while searching." + vbCrLf + vbCrLf + "Error source: " + err.Source + vbCrLf + "Error number: " + LTrim$(Str$(err.Number)) + vbCrLf + "Description: " + err.Description, vbExclamation, "Error Searching")
End Sub

Public Sub cmdFindNext_Click()
Call FindReplace(False, False)
End Sub

Private Sub cmdReplace_Click()
Call FindReplace(True, False)
End Sub

Private Sub cmdReplaceAll_Click()
Call FindReplace(True, True)
End Sub

Private Sub Form_Activate()
txtReplaceWithString.Text = ScriptMaker.txtReplaceWithString
txtSearchTerm.Text = ScriptMaker.txtSearchTerm
chkCaseSense.Value = ScriptMaker.chkCaseSense
End Sub

Private Sub Form_Load()
    ' Default SearchTerm must be the one selected by the user in MainForm
    If Len(ScriptMaker.txtClient.SelText) <> 0 Then
        txtSearchTerm.Text = ScriptMaker.txtClient.SelText
    End If
    If ScriptMaker.ShowWindow = True Then Call SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, flags)
    Me.Left = (ScreenX / 2) - (Me.Width / 2)
    Me.Top = (ScreenY / 2) - (Me.Height / 2)
End Sub

Function ReplaceFun(Source As String, FromPos As Integer, Length As Integer, StringTBReplaced As String) _
         As String
    ' Replaces a source string with new one appropriately
    Dim ResultStr As String
    
    ResultStr = Left(Source, FromPos - 1)
    ResultStr = ResultStr & StringTBReplaced
    ResultStr = ResultStr & Right(Source, Len(Source) - FromPos - Length + 1)
    
    ReplaceFun = ResultStr
End Function

Private Sub txtReplaceWithString_Change()
    ScriptMaker.txtReplaceWithString = txtReplaceWithString.Text
    Call EnableDisableReplaceButton
End Sub

Private Sub txtReplaceWithString_GotFocus()
    ' Select the contents of textbox
    If Len(txtReplaceWithString.Text) <> 0 Then
        txtReplaceWithString.SelStart = 0
        txtReplaceWithString.SelLength = Len(txtReplaceWithString.Text)
    End If
End Sub

Private Sub txtSearchTerm_Change()
    ScriptMaker.txtSearchTerm = txtSearchTerm.Text
    Call EnableDisableReplaceButton
End Sub

Private Sub EnableDisableReplaceButton()
    If Len(txtSearchTerm.Text) <> 0 Then
        cmdReplace.Enabled = True
        cmdFindNext.Enabled = True
        cmdReplaceAll.Enabled = True
    Else
        cmdReplace.Enabled = False
        cmdFindNext.Enabled = False
        cmdReplaceAll.Enabled = False
    End If
End Sub

Private Sub txtSearchTerm_GotFocus()
    ' Select the contents of textbox
    If Len(txtSearchTerm.Text) <> 0 Then
        txtSearchTerm.SelStart = 0
        txtSearchTerm.SelLength = Len(txtSearchTerm.Text)
    End If
End Sub
