Microsoft & .NETVisual BasicShow the Find and Replace Dialogs

Show the Find and Replace Dialogs

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This HowTo uses calls to the ComDlg32.dll file to load the
dialogs. This DLL also contains functions for the Open, Save, Print and Colour dialogs
which can be found in a different How-To. We also use the FINDREPLACE type structure to
hold the different parameters needed to show the dialogs. Apart from that, it is pretty
straight forward.

Add a new form. Add two command buttons, cmdFind and
cmdReplace. Copy the following code into the form:

Option Explicit
'Find/Replace Type Structure
Private Type FINDREPLACE
  lStructSize As Long
  hwndOwner As Long
  hInstance As Long
  flags As Long
  lpstrFindWhat As String
  lpstrReplaceWith As String
  wFindWhatLen As Integer
  wReplaceWithLen As Integer
  lCustData As Long
  lpfnHook As Long
  lpTemplateName As String
End Type

'Common Dialog DLL Calls
Private Declare Function FindText _
  Lib "comdlg32.dll" _
  Alias "FindTextA" (pFindreplace As FINDREPLACE) _
  As Long
Private Declare Function ReplaceText _
  Lib "comdlg32.dll" _
  Alias "ReplaceTextA" (pFindreplace As FINDREPLACE) _
  As Long

'Delcaration of the type structure
Dim frText As FINDREPLACE

Private Sub cmdFind_Click()
'Call the find text function
FindText frText
End Sub

Private Sub cmdReplace_Click()
'Call the replace text function
ReplaceText frText
End Sub

Private Sub Form_Load()
'Set the Find/Replace Type properties
With frText
  .lpstrReplaceWith = "Replace Text"
  .lpstrFindWhat = "Find Text"
  .wFindWhatLen = 9
  .wReplaceWithLen = 12
  .hInstance = App.hInstance
  .hwndOwner = Me.hWnd
  .lStructSize = LenB(frText)
End With
End Sub

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories