Option Explicit
'=======================================================
' The Declaration to keep the search box on top
'=======================================================
Private Declare Function SetWindowPos _
Lib "user32" ( _
ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal CX As Long, _
ByVal CY As Long, _
ByVal wFlags As Long) As Long
'=======================================================
' Private Declarations
'=======================================================
Dim ck1%, ck2%
Dim FindText$, FindStart&, ReplaceText$, SearchText$
'=======================================================
' Public Declarations and One Procedure
'=======================================================
Public Owner As Form
Public OwnerTxtBox As TextBox
Public Sub NoShow()
If OwnerTxtBox.SelLength = 0 _
And Len(FindText) = 0 Then
Show
Else
Act1
Command1_Click
End If
End Sub
'=======================================================
' General Finding Procedures
'=======================================================
Private Function CheckFound&(ByVal start&)
Static L&, Ls&
L = Len(FindText)
Ls = Len(SearchText)
If start > Ls Then start = 1
Do While start > 0 And start <= Ls
start = InStr(start, SearchText, FindText)
If ck1 <> vbChecked Then Exit Do
If CheckWord(start - 1, start + L) Then Exit Do
If start <> 0 Then start = start + 1
Loop
CheckFound = start
End Function
'Words not deliminated by Chars A-Z, a-z, 0-9, "_"
' Exiting the function early returns default False
Private Function CheckWord(ByVal start&, _
ByVal last&) As Boolean
'Check the leading char
If start > 0 Then
Select Case Asc(Mid$(SearchText, start, 1)) Or 32
Case 97 To 122, 48 To 57, 127
Exit Function
End Select
End If
'Check the trailing char
If (last) <= Len(SearchText) Then
Select Case Asc(Mid$(SearchText, last, 1)) Or 32
Case 97 To 122, 48 To 57, 127
Exit Function
End Select
End If
CheckWord = True
End Function
'=======================================================
' Mouse and Keyboard Events
'=======================================================
Private Sub Command1_Click()
Static found&
found = CheckFound(FindStart + 1)
If found = 0 Then found = CheckFound(1)
If found Then
FindStart = found
OwnerTxtBox.SelStart = found - 1
OwnerTxtBox.SelLength = Len(FindText)
Else
MsgBox "Nada"
End If
End Sub
Private Sub Command2_Click()
OwnerTxtBox.SelText = Text2
Check2_Click
Command1_Click
End Sub
Private Sub Check1_Click()
ck1 = Check1.Value
End Sub
Private Sub Check2_Click()
ck2 = Check2.Value
If ck2 = vbChecked Then
SearchText = OwnerTxtBox.Text
Else
SearchText = LCase$(OwnerTxtBox.Text)
End If
Text1_Change
End Sub
Private Sub Text1_Change()
If ck2 = vbChecked Then
FindText = Text1.Text
Else
FindText = LCase$(Text1.Text)
End If
End Sub
Private Sub Text1_GotFocus()
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End Sub
Private Sub Text2_Change()
ReplaceText = Text2.Text
End Sub
Private Sub Text2_GotFocus()
Text2.SelStart = 0
Text2.SelLength = Len(Text2.Text)
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyEscape Then Unload Me
End Sub
'=======================================================
' Initialization Procedures
'=======================================================
Private Sub Form_Load()
Text1.Move 120, 120, 2775, 375
Text2.Move 120, 600, 2775, 375
Command1.Move 3000, 120, 855, 375
Command1.Caption = "Find"
Command1.Default = True
Command2.Move 3000, 600, 855, 375
Command2.Caption = "Replace"
Check1.Move 120, 1080, 1695, 255
Check1.Caption = "Whole word only"
Check2.Move 1920, 1080, 1575, 255
Check2.Caption = "Match case"
Caption = "Find & Replace"
End Sub
Private Sub Form_Activate()
Const MyWidth! = 4095, MyHeight! = 1845
Static x&, y&, sx&, sy&, tx!, ty!
tx = Screen.TwipsPerPixelX
ty = Screen.TwipsPerPixelY
x = CLng((Owner.Left + Owner.Width - MyWidth) / tx)
y = CLng((Owner.Top + MyHeight) / ty)
sx = CLng(MyWidth / tx)
sy = CLng(MyHeight / ty)
'Const SWP_NOSIZE = 1
'Const SWP_NOMOVE = 2
'Const HWND_TOPMOST = -1
'Const HWND_NOTOPMOST = -2
'The big API call for on top
' also used here for size and position
SetWindowPos hwnd, -1, x, y, sx, sy, 0
Act1
Text1.SetFocus
End Sub
Private Sub Act1()
If OwnerTxtBox.SelLength Then
FindText = OwnerTxtBox.SelText
End If
FindStart = OwnerTxtBox.SelStart + 1
Text1.Text = FindText
Text2.Text = ReplaceText
Check1.Value = ck1
Check2.Value = ck2
Check2_Click
End Sub
|