Find and Replace

Category: Apps/Dialogs

Date: 07-23-2010

Return to Index


 
'Compilable Example:
#Compile Exe
#Dim All
#Include "Win32API.inc"
#Include "RichEdit.inc"
#Include "CommCtrl.inc"
#Resource "gbsnippets.pbr"
Global hDlg as DWord, hRichEdit as DWord, hSearch as DWord, hMenu as DWord, hMenuEdit as DWord
Global SearchTerm$, ReplaceTerm$, SearchStart&
%IDC_RichEdit = 500 : %IDM_Find = 600
Function PBMain() As Long
   Local style&, buf$
   buf$ = "This is a solution to the"
   buf$ = buf$ + $CrLf + "problem of finding and replacing"
   buf$ = buf$ + $CrLf + "the specified text in a RichEdit"
   buf$ = buf$ + $CrLf + "control. There are several"
   buf$ = buf$ + $CrLf + "options, including the Window"
   buf$ = buf$ + $CrLf + "built-in dialog."    
   searchterm$ = "the" : replaceterm$ = "XX"
   style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
             Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
   Dialog New Pixels, 0, "Test Code",300,300,200,300, %WS_OverlappedWindow To hDlg
   AddMenu
   Control Add Button, hDlg, 100,"Find", 30,10,140,20
   LoadLibrary("riched32.dll") : InitCommonControls
   Control Add "RichEdit", hDlg, %IDC_RichEdit, buf$,20,40,160,190, style&, %WS_Ex_ClientEdge
   Control Add TextBox, hDlg, 550, "for test purposes",20,240,160,20, style&, %WS_Ex_ClientEdge
   Control Handle hDlg, %IDC_RichEdit To hRichEdit
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Select Case CB.Msg
       Case %WM_Destroy
          Control Set Focus hDlg, %IDC_RichEdit
       Case %WM_Command
          If CB.Ctl = %IDM_Find AND CB.Ctlmsg = %BN_Clicked Then SetFocus hRichEdit  : DisplayFindDialog
          If CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then DisplayFindDialog
          If CB.Ctl = %IDC_RichEdit AND CB.Ctlmsg = %EN_SetFocus Then
             Control Post CB.HndlCB.Ctl, %EM_SETSEL, 0, 0
          End If
   End Select
End Function
 
Sub DisplayFindDialog()
     Local h As Long, w As Long
     Dialog Get Client hDlg To h,w
     Dialog New Pixels, hDlg, "Find & Replace", 100, 100, 330, 100, %WS_SysMenu Or %WS_Caption Or %WS_ClipChildren To hSearch
     Dialog Set Icon hSearch, "aainfo"
 
     Control Add Label, hSearch, 1050, "Find This:", 5, 10, 60, 20
     Control Add TextBox, hSearch, 1100, SearchTerm$, 80, 10, 185, 20
 
     Control Add Label, hSearch, 1060, "Replace With:", 5, 40, 70, 20
     Control Add TextBox, hSearch, 1125, ReplaceTerm$, 80, 40, 185, 20
     Control Add Button, hSearch, 1070, "Replace", 270, 40, 55, 20
 
 
     Control Add ImgButton, hSearch, 1110, "downhand", 270, 10, 25, 25
     Control Add ImgButton, hSearch, 1120, "uphand", 300, 10, 25, 25
 
     Control Add Checkbox, hSearch, 1130, "Case Sensitive", 15, 70, 90, 20
     Control Add Button, hSearch, 1140, "Next Snippet", 125, 70, 85, 20
     Control Add Button, hSearch, 1150, "Previous Snippet", 220, 70, 100, 20
 
     Control Add Button, hSearch, %IdOk, "Ok", 200, 230, 20, 20
     Control Add Button, hSearch, %IdCancel, "Cancel", 230, 230, 20, 20
     Control Show State hSearch, %IdOk, %SW_Hide
     Control Show State hSearch, %IdCancel, %SW_Hide
 
 
     Dialog Show Modeless hSearch Call SearchProc()
End Sub
 
CallBack Function SearchProc() As Long
    Local iCheck&, x As Long, y As Long
    Select Case CB.Msg
      Case %WM_SYSCOMMAND
         If (CB.wParam AND &HFFF0) = %SC_Close Then          'trap Alt-F4 and X Button
            Control Set Focus hDlg, %IDC_RichEdit
         End If
       Case %WM_Command
          Select Case CB.Ctl
             Case %IdCancel
                Dialog End hSearch
             Case 1070           'replace selected text
                Control Send hDlg, %IDC_RichEdit, %EM_GETSEL, VarPTR(x), VarPTR(y)
                If x <> y Then    'apply only to a selection
                   Control Get Text hSearch, 1125 To ReplaceTerm$
                   If Len(ReplaceTerm$) Then
                      Control Send hDlg, %IDC_RichEdit, %EM_ReplaceSel, %True, StrPTR(ReplaceTerm$)
                      Control Get Text hSearch, 1100 To SearchTerm$
                      Control Get Check hSearch, 1130 To iCheck&
                      FindTextInRichEdit iCheck&, 1                    '1 means forward search
                   End If
                End If
             Case 1110, %IdOk    'search forward
                Control Get Text hSearch, 1100 To SearchTerm$
                Control Get Check hSearch, 1130 To iCheck&
                FindTextInRichEdit iCheck&, 1                    '1 means forward search
             Case 1120    'search backward
                Control Get Text hSearch, 1100 To SearchTerm$
                Control Get Check hSearch, 1130 To iCheck&
                FindTextInRichEdit iCheck&, 0                   '0 means backward search
          End Select
    End Select
End Function
 
Sub FindTextinRichEdit(CaseSensitive&, iDirection&)  'SearchTerm$, SearchStart& are global
 
   Local temp$, P As CharRange, sTerm$
   Control Get Text hDlg, %IDC_RichEdit To temp$                   'get text from RichEdit
   Control Send hDlg, %IDC_RichEdit, %EM_ExGetSel, 0, VarPTR(P)    'get caret/selection boundaries
 
   If CaseSensitive& Then
      sTerm$ = SearchTerm$
   Else
      sTerm$ = LCase$(SearchTerm$)
      temp$ = LCase$(temp$)
   End If
 
   If iDirection& Then
      SearchStart& = Instr(P.cpMax+1, temp$, sTerm$)
   Else
      SearchStart& = Instr(-1*(Len(temp$)-P.cpMin), temp$, sTerm$)
   End If
 
   If SearchStart& Then
      P.cpMin = SearchStart& - 1
      P.cpMax = SearchStart& + Len(sTerm$) - 1
 
      If iDirection& Then
          Control Send hDlg, %IDC_RichEdit, %EM_ExSetSel, 0, VarPTR(P)
      Else
          Control Send hDlg, %IDC_RichEdit, %EM_ExSetSel, 0, VarPTR(P)
      End If
   Else
      Beep
   End If
 
End Sub
          
 
Sub BuildAcceleratorTable
   Local c As Long, ac() As ACCELAPI, hAccelerator As DWord  ' for keyboard accelator table values
   Dim ac(1)
   ac(c).fvirt = %FVIRTKEY Or %FCONTROL : ac(c).key   = %VK_F : ac(c).cmd   = %IDM_FIND       : Incr c
   ac(c).fvirt = %FVIRTKEY              : ac(c).key   = %VK_F3 : ac(c).cmd   = %IDM_FIND      : Incr c
   Accel Attach hDlg, AC() To hAccelerator
End Sub
 
Sub AddMenu
    'Create Bar -------------------------
    Menu New Bar To hMenu
    'Create Edit + Children -------------------------
    Menu New Popup To hMenuEdit
    Menu Add Popup, hMenu, "&Edit", hMenuEdit, %MF_Enabled
    Menu Add String, hMenuEdit, "Find" + $Tab + "F3", %IDM_Find, %MF_Enabled
    'Attach Bar to Dialog -------------------------
    Menu Attach hMenu, hDlg    
End Sub
 
'gbs_00042


created by gbSnippets: http://www.garybeene.com