Exercise 8 :: Text editor continued

Custom Dialogs

The common dialog controls are very useful, but in many cases we need to make our own. Making your own dialog is quite simple in Visual Basic. Dialogs are just simple forms. Here are the basic steps for creating a custom dialog:
Task 1 :: Adding a new form

In this task we will add a new form for a find dialog.

  1. Take a close look at the following screen shot. This is the dialog we will be creating.

    find dialog

  2. In your text editor project, in the VB IDE click "Project->Add Form". Choose to add a "Form".
  3. Add controls to this form to make it look like the dialog above. If you wish to match the example program you should give your controls the following properties:
    text box :  name = txtFind
                text = ""
    
    label :     name = lblFind
                caption = "Find Text"
    
    command button :  name = cmdFind
                      caption = "Find"
    									
    command button :  name = cmdCancel
                      caption = "Cancel"
    
    You can see full details of the controls from the printout.

Task 2 :: Programming the new form

In this task we will add code to our dialog to give it searching capabilities.

  1. Add the following code to the code window of the new dialog.
    Option Explicit
    
    ' Need a variable that keeps track of where to
    ' start the next search
    Public startPos As Integer
    Public currentSearch As String
    
    Private Sub cmdCancel_Click()
        Unload Me
        Me.Hide
    End Sub
    
    Private Sub cmdFind_Click()
    ' When the find button is clicked, we wish to jump to the
    ' next instance of the current string in the text
    
        ' get a local variable that is the search string
        Dim lookingFor As String
        lookingFor = txtFind.Text
        
        ' Variable to keep track of where this string is
        Dim pos As Integer
        pos = 0
        
        pos = InStr(startPos, frmMain!txtDocument.Text, lookingFor)
        
        If pos = 0 Then
            MsgBox "String not found"
        Else
            With frmMain!txtDocument
                .SelStart = pos - 1
                .SelLength = Len(lookingFor)
            End With
        End If
        
        ' set up for find next in case it is used
        startPos = pos + Len(lookingFor)
        currentSearch = lookingFor
        
        ' close down the form
        cmdCancel_Click
    End Sub
    
    Private Sub Form_Load()
        ' got to set this initially
        startPos = 1
    End Sub
    
    Public Sub findNext()
    ' This is a function that will find the next incident of
    ' the last string searched for
        Dim pos As Integer
        pos = InStr(startPos, frmMain!txtDocument.Text, currentSearch)
        
        If pos = 0 Then
            MsgBox "String not found"
            startPos = 1
        Else
            With frmMain!txtDocument
                .SelStart = pos - 1
                .SelLength = Len(currentSearch)
            End With
        End If
        
        ' set up for find next in case it is used
        startPos = pos + 1
    End Sub
    
There are a number of things to take note of in this code.
Task 2 :: Connecting the new form

We have to connect the two forms now. We must connect in both directions, from the main form to the dialog and from the dialog to the main form. Half of our connection has been made already, the dialog directly effects the main form by accessing it with the "!" notation. The connection from the main form to the dialog is simple. All we must do is add code to invoke the dialog. This will be in the form of two menu options (find and find next) which invoke functionality we have already built into the find dialog.

  1. Add two new menu items ("Find" and "Find Next") under a new heading "Search". To make a menu into a heading, use the left arrow next to the right arrow that we used in Exercise 6 Task 1. It does the exact opposite.
  2. Add the following code to the event for the "Find" menu item.
    frmFind.Show
    
    The Show method will make a form visible.
  3. Add the following code to the event for the "Find Next" menu item.
    frmFind.findNext
    
    Notice that we don't use the "!" notation to access the findNext procedure that we added (it is not connected to any event) to the find dialog. This notation is reserved for controls on the form. For accessing procedure, we can just use the simple . notation.

We have now finished the text editor. If you can think of any functionality that you would like to add to your text editor, go ahead and give it a go.

NEXT ->


Matthew Roberts, Macquarie University 2002