[英]How do I apply a Find and Replace only to the selected text and not the whole document in VB.net
正確,我已經創建了允許用戶對RTF文本框中的所有文本執行“ 查找和替換”的代碼。 但是,我現在允許用戶選擇要執行查找和替換的部分文本
這是我目前正在使用的代碼:
Private Sub btnFFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFFindNext.Click
Dim length as String
length = textToFind.Text.Length
lastposition = frm1.RichTextBox.Find(textToFind.Text, lastposition, RichTextBoxFinds.None)
frm1.RichTextBox.SelectionStart = lastposition
frm1.RichTextBox.SelectionLength = length
lastposition = lastposition + 1
我還在form1選擇更改事件處理程序的RTB內添加了代碼,以便在更改時將當前光標位置設置為lastposition 。
希望以上代碼和我的描述將幫助您了解我的情況。 因此,為澄清起見,我將如何修改我的代碼,以便如果用戶選擇一些文本,則僅對該文本執行“ Find and Replace
”。 一旦到達選擇的結尾,它將結束。
謝謝。
我建議使用MouseUp
和MouseDown
事件捕獲選擇,然后在選擇范圍內找出搜索字符串。
您還可以查看此 MSDN示例,或者類似的方法可以幫助您:
Private selectionStart As Integer
Private selectionEnd As Integer
Private searchString As String
Private Sub btnFindAll_Click(sender As Object, e As EventArgs)
searchString = textToFind.Text ' Set string to find here
' Swap position due to reverse selection
If selectionStart > selectionEnd Then
Dim x As Integer = selectionStart
selectionStart = selectionEnd
selectionEnd = x
End If
'Every time find index and focus the result
Dim index As Integer = richTextBox1.Find(searchString, selectionStart, selectionEnd, RichTextBoxFinds.None)
If index > 0 Then
richTextBox1.Focus()
richTextBox1.Select(index, searchString.Length)
selectionStart = index + searchString.Length
Else
' not found
End If
End Sub
Private Sub richTextBox1_MouseUp(sender As Object, e As MouseEventArgs)
selectionEnd = richTextBox1.GetCharIndexFromPosition(New System.Drawing.Point(e.X, e.Y))
End Sub
Private Sub richTextBox1_MouseDown(sender As Object, e As MouseEventArgs)
selectionStart = richTextBox1.GetCharIndexFromPosition(New System.Drawing.Point(e.X, e.Y))
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.