Public Class Find_Repl    
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
   Friend WithEvents ToolBarButton8 As System.Windows.Forms.ToolBarButton
   Friend WithEvents tbb_Find As System.Windows.Forms.ToolBarButton
   Friend WithEvents tbb_Replace As System.Windows.Forms.ToolBarButton
   Friend WithEvents ToolBarButton3 As System.Windows.Forms.ToolBarButton
   Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
   Friend WithEvents cm_Find As System.Windows.Forms.ContextMenu
   Friend WithEvents cm_Repl As System.Windows.Forms.ContextMenu
   Friend WithEvents mi_Find_Next As System.Windows.Forms.MenuItem
   Friend WithEvents mi_Replace_All As System.Windows.Forms.MenuItem
   Friend WithEvents ToolBarButton17 As System.Windows.Forms.ToolBarButton
   Friend WithEvents tbb_Exit As System.Windows.Forms.ToolBarButton
   Friend WithEvents Panel5 As System.Windows.Forms.Panel
   Friend WithEvents ReplaceWord As System.Windows.Forms.TextBox
   Friend WithEvents searchWord As System.Windows.Forms.TextBox
   Friend WithEvents tb_Find_Repl1 As System.Windows.Forms.ToolBar
   Friend WithEvents tb_Find_Repl2 As System.Windows.Forms.ToolBar
   Friend WithEvents tbb_Find_Next As System.Windows.Forms.ToolBarButton
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.components = New System.ComponentModel.Container
      Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Find_Repl))
      Me.tb_Find_Repl1 = New System.Windows.Forms.ToolBar
      Me.tbb_Find = New System.Windows.Forms.ToolBarButton
      Me.cm_Find = New System.Windows.Forms.ContextMenu
      Me.mi_Find_Next = New System.Windows.Forms.MenuItem
      Me.ToolBarButton8 = New System.Windows.Forms.ToolBarButton
      Me.tbb_Replace = New System.Windows.Forms.ToolBarButton
      Me.cm_Repl = New System.Windows.Forms.ContextMenu
      Me.mi_Replace_All = New System.Windows.Forms.MenuItem
      Me.ToolBarButton3 = New System.Windows.Forms.ToolBarButton
      Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
      Me.tb_Find_Repl2 = New System.Windows.Forms.ToolBar
      Me.tbb_Find_Next = New System.Windows.Forms.ToolBarButton
      Me.ToolBarButton17 = New System.Windows.Forms.ToolBarButton
      Me.tbb_Exit = New System.Windows.Forms.ToolBarButton
      Me.Panel5 = New System.Windows.Forms.Panel
      Me.ReplaceWord = New System.Windows.Forms.TextBox
      Me.searchWord = New System.Windows.Forms.TextBox
      Me.Panel5.SuspendLayout()
      Me.SuspendLayout()
      '
      'tb_Find_Repl1
      '
      Me.tb_Find_Repl1.AutoSize = False
      Me.tb_Find_Repl1.Buttons.AddRange(New System.Windows.Forms.ToolBarButton() {Me.tbb_Find, Me.ToolBarButton8, Me.tbb_Replace, Me.ToolBarButton3})
      Me.tb_Find_Repl1.ButtonSize = New System.Drawing.Size(80, 26)
      Me.tb_Find_Repl1.Dock = System.Windows.Forms.DockStyle.Left
      Me.tb_Find_Repl1.DropDownArrows = True
      Me.tb_Find_Repl1.ImageList = Me.ImageList1
      Me.tb_Find_Repl1.Location = New System.Drawing.Point(0, 0)
      Me.tb_Find_Repl1.Name = "tb_Find_Repl1"
      Me.tb_Find_Repl1.ShowToolTips = True
      Me.tb_Find_Repl1.Size = New System.Drawing.Size(95, 71)
      Me.tb_Find_Repl1.TabIndex = 17
      Me.tb_Find_Repl1.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right
      '
      'tbb_Find
      '
      Me.tbb_Find.DropDownMenu = Me.cm_Find
      Me.tbb_Find.ImageIndex = 19
      Me.tbb_Find.Text = " Find"
      Me.tbb_Find.ToolTipText = "Find"
      '
      'cm_Find
      '
      Me.cm_Find.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mi_Find_Next})
      '
      'mi_Find_Next
      '
      Me.mi_Find_Next.Index = 0
      Me.mi_Find_Next.Text = "Find Next"
      '
      'ToolBarButton8
      '
      Me.ToolBarButton8.Style = System.Windows.Forms.ToolBarButtonStyle.Separator
      '
      'tbb_Replace
      '
      Me.tbb_Replace.DropDownMenu = Me.cm_Repl
      Me.tbb_Replace.ImageIndex = 21
      Me.tbb_Replace.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton
      Me.tbb_Replace.Text = " Replace"
      Me.tbb_Replace.ToolTipText = "Replace"
      '
      'cm_Repl
      '
      Me.cm_Repl.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mi_Replace_All})
      '
      'mi_Replace_All
      '
      Me.mi_Replace_All.Index = 0
      Me.mi_Replace_All.Text = "Replace All"
      '
      'ToolBarButton3
      '
      Me.ToolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator
      '
      'ImageList1
      '
      Me.ImageList1.ImageSize = New System.Drawing.Size(24, 24)
      Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
      Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
      '
      'tb_Find_Repl2
      '
      Me.tb_Find_Repl2.AutoSize = False
      Me.tb_Find_Repl2.Buttons.AddRange(New System.Windows.Forms.ToolBarButton() {Me.tbb_Find_Next, Me.ToolBarButton17, Me.tbb_Exit})
      Me.tb_Find_Repl2.ButtonSize = New System.Drawing.Size(80, 26)
      Me.tb_Find_Repl2.Dock = System.Windows.Forms.DockStyle.Right
      Me.tb_Find_Repl2.DropDownArrows = True
      Me.tb_Find_Repl2.ImageList = Me.ImageList1
      Me.tb_Find_Repl2.Location = New System.Drawing.Point(250, 0)
      Me.tb_Find_Repl2.Name = "tb_Find_Repl2"
      Me.tb_Find_Repl2.ShowToolTips = True
      Me.tb_Find_Repl2.Size = New System.Drawing.Size(80, 71)
      Me.tb_Find_Repl2.TabIndex = 46
      Me.tb_Find_Repl2.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right
      '
      'tbb_Find_Next
      '
      Me.tbb_Find_Next.ImageIndex = 23
      Me.tbb_Find_Next.Text = "Find Next"
      Me.tbb_Find_Next.ToolTipText = "Find_Next"
      '
      'ToolBarButton17
      '
      Me.ToolBarButton17.Style = System.Windows.Forms.ToolBarButtonStyle.Separator
      '
      'tbb_Exit
      '
      Me.tbb_Exit.ImageIndex = 1
      Me.tbb_Exit.Text = " Exit"
      Me.tbb_Exit.ToolTipText = "Exit"
      '
      'Panel5
      '
      Me.Panel5.BackColor = System.Drawing.Color.Gainsboro
      Me.Panel5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
      Me.Panel5.Controls.Add(Me.ReplaceWord)
      Me.Panel5.Controls.Add(Me.searchWord)
      Me.Panel5.Location = New System.Drawing.Point(95, 0)
      Me.Panel5.Name = "Panel5"
      Me.Panel5.Size = New System.Drawing.Size(152, 71)
      Me.Panel5.TabIndex = 47
      '
      'ReplaceWord
      '
      Me.ReplaceWord.AutoSize = False
      Me.ReplaceWord.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(235, Byte))
      Me.ReplaceWord.Font = New System.Drawing.Font("Courier New", 9.75!, System.Drawing.FontStyle.Bold)
      Me.ReplaceWord.Location = New System.Drawing.Point(10, 42)
      Me.ReplaceWord.Name = "ReplaceWord"
      Me.ReplaceWord.Size = New System.Drawing.Size(130, 20)
      Me.ReplaceWord.TabIndex = 13
      Me.ReplaceWord.Text = ""
      '
      'searchWord
      '
      Me.searchWord.AutoSize = False
      Me.searchWord.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(235, Byte))
      Me.searchWord.Font = New System.Drawing.Font("Courier New", 9.75!, System.Drawing.FontStyle.Bold)
      Me.searchWord.Location = New System.Drawing.Point(10, 8)
      Me.searchWord.Name = "searchWord"
      Me.searchWord.Size = New System.Drawing.Size(130, 20)
      Me.searchWord.TabIndex = 12
      Me.searchWord.Text = ""
      '
      'Find_Repl
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(330, 71)
      Me.Controls.Add(Me.Panel5)
      Me.Controls.Add(Me.tb_Find_Repl2)
      Me.Controls.Add(Me.tb_Find_Repl1)
      Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
      Me.MaximizeBox = False
      Me.MinimizeBox = False
      Me.Name = "Find_Repl"
      Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
      Me.Text = "  Find & Replace"
      Me.TopMost = True
      Me.Panel5.ResumeLayout(False)
      Me.ResumeLayout(False)

   End Sub

#End Region

   Dim TxtBox As RichTextBox


   Private Sub frm_Find_Repl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      TxtBox = Form_Start.rtxBox
      tbb_Replace.Enabled = False
   End Sub

 
'------------- tb_Find_Repl1 ------------------------------------------------------------------------------------------ tb_Find_Repl1
'------------- tb_Find_Repl1 ------------------------------------------------------------------------------------------ tb_Find_Repl1

   Private Sub tb_Find_Repl1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) _
       Handles tb_Find_Repl1.ButtonClick
      If e.Button Is tbb_Find    Then FindNorm( 1)
      If e.Button Is tbb_Replace Then Replace_()
   End Sub


'------------- tb_Find_Repl2 ------------------------------------------------------------------------------------------ tb_Find_Repl2
'------------- tb_Find_Repl2 ------------------------------------------------------------------------------------------ tb_Find_Repl2

   Private Sub tb_Find_Repl2_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) _
       Handles tb_Find_Repl2.ButtonClick
      If e.Button Is tbb_Find_Next Then FindNorm(TxtBox.SelectionStart + 2)
      If e.Button Is tbb_Exit      Then Me.Hide'Close()
   End Sub


   Private Sub mi_Replace_All_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mi_Replace_All.Click
      Replace_All()
   End Sub


'----------------------------------------------------------------------------------------------- 
   Private Sub FindNorm(ByVal FindStart As Integer)
      Dim selStart As Integer
      With TxtBox
         selStart = InStr(FindStart, .Text, searchWord.Text, CompareMethod.Text)
         If selStart = 0 Then
            MsgBox("No more matches"):    Exit Sub:    End If
         .Select(selStart - 1, searchWord.Text.Length):    '.ScrollToCaret()
      End With
   End Sub


   Private Sub ReplaceWord_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceWord.TextChanged
      If ReplaceWord.Text.Length > 0 Then tbb_Replace.Enabled = True _
      Else                                tbb_Replace.Enabled = False 
   End Sub


   Private Sub Replace_()
      With TxtBox
         If .SelectedText <> "" Then
            .SelectedText = ReplaceWord.Text:    End If
      End With
      FindNorm(TxtBox.SelectionStart + 2)
   End Sub


   Private Sub Replace_All()
      Dim curPos, curSel As Integer
      With TxtBox
         curPos = .SelectionStart:    curSel = .SelectionLength
         .Text = Replace(.Text, Trim(SearchWord.Text), Trim(ReplaceWord.Text))
         .SelectionStart = curPos:    .SelectionLength = curSel
      End With
   End Sub

End Class
