![]() 圖一、程序實現"查找替換"功能時的效果圖 |
&Edit ...&Find and Replace mnuFindandreplace E&xit mnuExit |
Private Sub mnuExit_Click() End End Sub Private Sub mnuFindandreplace_Click() frmFindReplace.FindnReplace txtClientArea End Sub |
從上面代碼中可以非常明顯地看出, 當點擊Exit菜單時,我們想終結應用程序,當點擊"Find and Replace"菜單時,想通過共用接口frmFindReplace及FindnReplace()方法來激活frmFindReplace窗體。這個公用的接口使查找算法具有普遍性,使用這個接口時,需要提供一個TextBox作為參數(在這里面,搜尋將被執行)。通過使用你自己的TextBox的名字來代替txtClientArea的名字,可以在多個文本框內執行"查找替換"功能,而不用更改代碼。"查找和替換"的實現代碼主要是在frmFindReplace窗體內,這個模塊的代碼如下:
' This variable is used for making the algorithm generic. Dim txtClient As TextBox ' This method is the public interface to SnR functionality. Public Sub FindnReplace(ByRef Tb As TextBox) Set txtClient = Tb Me.Show , txtClient.Parent End Sub Private Sub cmdReplace_Click() Dim CaseSense As Integer Dim SourceText As String Dim SourceTextCopy As String Dim Cnt As Integer ' Check for the case sensitivity options If (chkCaseSense.Value = vbChecked) Then CaseSense = 0 Else CaseSense = 1 End If ' One contains the original text and another contains replaced ' (updated) one. ' Used to check whether a replacement was done or not. SourceText = txtClient.Text SourceTextCopy = SourceText If Len(SourceText) = 0 Then Exit Sub End If On Error GoTo ErrHandler Dim SearchTermLen As Integer Dim FndPos As Integer SearchTermLen = Len(txtSearchTerm.Text) ' Search from the begining of the document. Cnt = 1 ' This is endless loop (terminated on a condition checked inside ' the loop body). While (1) FndPos = InStr(Cnt, SourceText, txtSearchTerm.Text, CaseSense) ' When a match is found, replace it appropriately. If (FndPos > 0) Then SourceText = ReplaceFun(SourceText, FndPos, Len(txtSearchTerm.Text), txtReplaceWithString.Text) Cnt = FndPos + SearchTermLen Else Cnt = Cnt + 1 End If ' Whether a replacement was done at all or not If (Cnt >= Len(SourceText)) Then txtClient.Text = SourceText If (SourceTextCopy <> SourceText) Then MsgBox "Finished replacing all occurrences.", vbInformation + vbOKOnly, "Replaced All" Else MsgBox "No matching strings found. No text replaced.", vbInformation + vbOKOnly, "No Replacement" End If Unload Me Exit Sub End If ' Else Restart from henceforth Wend Exit Sub ErrHandler: Response = MsgBox("An error ocurred while searching. Inform the developer with details.", _ vbExclamation + vbOKOnly, "Error Searching") End Sub Private Sub Form_Load() ' Default SearchTerm must be the one selected by the user in ' MainForm If Len(txtClient.SelText) <> 0 Then txtSearchTerm.Text = txtClient.SelText End If End Sub Function ReplaceFun(Source As String, FromPos As Integer, _ Length As Integer, StringTBReplaced _ As String) As String ' Replaces a source string with new one appropriately Dim ResultStr As String ResultStr = Left(Source, FromPos - 1) ResultStr = ResultStr & StringTBReplaced ResultStr = ResultStr & Right(Source, Len(Source) - FromPos - Length + 1) ReplaceFun = ResultStr End Function Private Sub txtReplaceWithString_Change() Call EnableDisableReplaceButton End Sub Private Sub txtReplaceWithString_GotFocus() ' Select the contents of the textbox If Len(txtReplaceWithString.Text) <> 0 Then txtReplaceWithString.SelStart = 0 txtReplaceWithString.SelLength = Len(txtReplaceWithString.Text) End If End Sub Private Sub txtSearchTerm_Change() Call EnableDisableReplaceButton End Sub Private Sub EnableDisableReplaceButton() If Len(txtSearchTerm.Text) <> 0 _ And Len(txtReplaceWithString.Text) <> 0 Then cmdReplace.Enabled = True Else cmdReplace.Enabled = False End If End Sub Private Sub txtSearchTerm_GotFocus() ' Select the contents of textbox If Len(txtSearchTerm.Text) <> 0 Then txtSearchTerm.SelStart = 0 txtSearchTerm.SelLength = Len(txtSearchTerm.Text) End If End Sub |
原文轉自:http://www.anti-gravitydesign.com