Using a Drop Stack
Edwina uses two drop stacks—one for the current file and another for the find text. XEditor’s FSearch form also uses two drop stacks—one for the find text and another for the replace text. To make matters even more interesting, the find stack on Edwina’s toolbar needs to be synchronized with the find stack on the FSearch form. And both find stacks are intimately tied to the lower-level FindWhat and ReplaceWith properties of the XEditor control. We’re not going to cover every detail, but we’ll hit the high points.
Let’s start on the FSearch form with the dropWhat control that specifies what to find. By the time the XEditor control gets a command to load the FSearch form, it might already have a list of search strings. In Edwina, those search strings could have been entered with the drop stack on the toolbar. Your client program might create the drop stack in some other way. In any case, the FSearch form has to initialize the list with the following code (inside a With Editor block) in Form_Load:
Dim i As Long
dropWhat.MaxCount = .FindWhatMax
For i = .FindWhatCount To 1 Step -1
dropWhat.Text = .FindWhat(i)
Next
There are two sides to this operation. On the right side are the FindWhatMax, FindWhatCount, and FindWhat properties of the XEditor control. They manage the real search list that will be used by the FindNext method. The MaxCount and Text properties of the XDropStack control manage the display list.
I’ll get back to the related XEditor properties shortly, but first let’s look at the only XDropStack event that really matters:
Private Sub dropWhat_Completed(Text As String)
If fInCompleted Then Exit Sub
fInCompleted = True
lblMessage.Caption = sEmpty
Editor.FindWhat = Text
fInCompleted = False
End Sub
In a combo box, several events can create a new entry. First, there’s clicking an item in the drop-down list. Second, there’s typing in an entry in the text field. And third, there’s losing focus. When you handle a combo box, you usually need to handle all of these events separately to recognize new entries. Well, the XDropStack takes care of all three, combining them into a Completed event that signals a new entry without telling you where it came from. You can use the result as you see fit. Here, the FSearch form just assigns the result back to the XEditor control and clears the status message.
The only other place the FSearch form uses the dropWhat control is in the Click event procedure of the Find Next button:
Private Sub cmdFindNext_Click()
With Editor
Dim i As Integer
' Must be something to find
If dropWhat.Text = sEmpty Then
dropWhat.SetFocus
Exit Sub
End If
' When Replace user selects Next once, make it default
cmdFindNext.Default = True
' Find next item
i = .FindNext(dropWhat)
' Deal with failed search
If i = 0 Then
lblMessage.Caption = "Text not found"
.SelLength = 0
Else
lblMessage.Caption = "Text found: " & .Line & "," & .Column
End If
dropWhat.SetFocus
End With
End Sub
This code doesn’t even need to read the Text property because the text was already assigned in the Completed event. The only thing it needs the dropWhat control for is to manage the focus.