2

そうですね、ユーザーがリッチ テキスト ボックス内のすべてのテキストに対して検索と置換を実行できるようにするコードは既に作成しています。ただし、検索と置換を実行するテキストの一部をユーザーが選択できるようにするにはどうすればよいですか

これは私が現在使用している私のコードです:

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

また、RTB on form1 selection changed イベント ハンドラ内にコードを追加して、変更時に現在のカーソル位置をlastpositionとして設定するようにしました。

上記のコードと私の説明が私の状況を理解するのに役立つことを願っています. 明確にするために、ユーザーがテキストを選択した場合Find and Replaceにそのテキストに対してのみ実行されるように、コードをどのように適合させるかを説明します。選択の最後に達すると、終了します。

ありがとうございました。

4

1 に答える 1

0

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
于 2012-11-14T11:41:46.210 に答える