top
Loading...
VB應用程序中實現“查找和替換”功能
下載本文源代碼

一、前言

盡管Visual Basic并不是我最喜歡的開發工具,但我喜歡它簡單而又豐富的庫集。每當開發一個需要處理大量文本數據的應用程序時,需要具有拼寫錯誤糾正功能,例如,微軟的Word程序,當運行"拼寫檢查"時,將提供給你一個改正錯誤的機會(盡管是建議),它同時也提供了"查找替換"工具,用以進行用戶定義的單詞替換。這篇文章我將解釋如何在VB應用程序中實現"查找替換"功能。

二、前提條件

在解釋代碼的時候,我假定讀者朋友們已經有使用Visual Basic的經驗,熟悉Visual Studio開發環境中各種內置的控件及庫函數(盡管我使用的不多)。我已經嘗試著盡可能地簡化程序代碼,用不了多久你就可以明白程序的邏輯。如果想對一些庫函數(如參數,語法)進一步詳細地理解,可以參閱MSDN。圖一是程序運行后的效果圖:


圖一、程序實現"查找替換"功能時的效果圖

三、基礎工作

首先創建一個標準的EXE類型的VB工程,將默認窗體更名為frmMainForm ,在默認窗體上增添一個菜單,具體設置如下(符號"&"用于加速鍵,單詞mnu后的名字用來說明菜單項的名字(在代碼中使用)):

&Edit
...&Find and Replace mnuFindandreplace
E&xit mnuExit

向默認窗體添加一個TextBox控件,命名為txtClientArea。使用鼠標調整控件位置和尺寸,使它覆蓋窗體的整個客戶區,在屬性窗口將這個TextBox控件的 MultiLine屬性設置為"True"。

使用Project > Add Form菜單向工程中添加另外一個窗體,將這個窗體命名為"frmFindReplace",并在屬性窗口中將它的BorderStyle屬性設置為"4 - FixedToolWindow"。現在,添加兩個TextBox控件,并分別命名為"txtSearchTerm"和"txtReplaceWithString"。添加一個復選框,命名為chkCaseSense。最后,添加一個命令按鈕控件,命名為"cmdReplace"。

在frmMainForm窗體中添加如下代碼:

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

四、代碼說明

公用接口SearchnReplace的"查找替換"算法帶有普遍性,使用這種方法,"查找替換"功能可以在任何應用程序中實現,而不用更改frmFindReplace的代碼。只是在調用公用接口的地方需要做一些小的改動。

"查找和替換"代碼在cmdReplace_Click()過程與ReplaceFun() 函數下,讓我們從cmdReplace_Click()過程開始。

首先,變量CaseSense用于跟蹤用戶的選擇,是否是大小寫敏感,如果用戶選擇大小寫敏感的話,它的值設置為0,否則,設置為1。變量SourceText和SourceTextCopy用于保存frmMainForm窗體中 txtClientArea的內容(或者是用戶在主窗體中為TextBox提供的內容,對于本程序來說,它就是txtClientArea),兩個變量保存同樣的內容將在下面進行解釋(一個是臨時變量)。變量Cnt用來跟蹤文檔的結束,當重新從文檔開始處進行"查找替換"時它將非常有用。這個變量含蓄地說明了當前光標的位置,下次的"查找替換"將從這個位置開始。

主要的算法在while循環內,只要替換或是源TextBox被掃描時,這個循環都將被激活。

第二步,我使用了InStr()庫函數,它將從第一個參數規定的位置處開始搜索,如果在第二個參數字符串中發現與第三個字符串相匹配的子串,將返回該子串的位置。最后一個參數是大小寫敏感選項。

InStr()函數的返回值傳遞給FndPos,如果在源文檔中發現了所要查找的字符串,這個值將大于0,否則將返回一個0值。在緊接著的IF語句中,首先替換SourceText變量,然后是通過FndPos + SearchTermLen語句修改Cnt變量,這是必須的, 因為下一次的"查找和替換"要從新的位置開始。另一方面,在Else部分,Cnt變量加一,說名查找和替換要從下一個字符開始。緊接著的幾個語句用來通知用戶查找結果,它們非常容易理解,也非常容易實現。可以直接跳過它們。

最后,ReplaceFun()函數進行實際的替換工作,它從源字符串的左邊開始,挑選出所有的字符,直到發現所搜索的字符串位置處為止。緊接著,StringTBReplace被添加到結果的后面,它將代替所匹配的源字符串。最后,所有的保留的源字符(排除所要更換的子字符串)被添加到這個結果中去,并作為結果返回。 五、結論

有很多方法來實現這一功能,但我認為我所使用的方法是最簡單也是最直接的,通過例子程序,你可以在應用程序中實現自己的"查找替換"功能。
作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗