3

RichTextBox特定の単語を入力すると強調表示されるようにするにはどうすればよいですか?

テキスト内で使用する単語を見つける方法SelectionColorまたはSelectionFont

例:「こんにちは」という単語が表示されるたびに、太字または色に変わるようにしRichTextBoxたい...

次に、プログラムを開いて「こんにちは、お元気ですか?」と入力すると、こんにちはという言葉が太字に変わります...何か考えはありますか? (私の考えは、単語を正しく指定しない構文ハイライトを備えたテキストエディターを作成することです)

(そのような別の質問がある場合は申し訳ありませんが、検索しようとしましたが、私を助ける答えが見つかりませんでした)

そのウィンドウ フォーム、ビジュアル ベーシック

4

5 に答える 5

3

このコードで作業を行う必要があります。

Dim searchstring As String = "hello"
' The word you're looking for
Dim count As New List(Of Integer)()
For i As Integer = 0 To richTextBox1.Text.Length - 1
    If richTextBox1.Text.IndexOf(searchstring, i) <> -1 Then
        'If the word is found
            'Add the index to the list
        count.Add(richTextBox1.Text.IndexOf(searchstring, i))
    End If
Next
Try
    For i As Integer = 0 To count.Count - 1

        richTextBox1.[Select](count(i), searchstring.Length)
        richTextBox1.SelectionFont = New Font(richTextBox1.Font, FontStyle.Bold)
        count.RemoveAt(i)
    Next
Catch
End Try
richTextBox1.[Select](richTextBox1.Text.Length, 0)
richTextBox1.SelectionFont = New Font(richTextBox1.Font, FontStyle.Regula

インデックスごとにテキストを選択し、太字にします。

このコードをTextChanged-Event に追加して、単語のテキストがいつ変更されたかを確認します。

于 2013-01-22T22:04:10.517 に答える
3

私は別の方法でそれを得ました:

   While Not RichTextBox1.Text.IndexOf("hello", startIndex) = -1
               selectedIndex= RichTextBox1.SelectionStart
        Try
                RichTextBox1.Select(RichTextBox1.Text.IndexOf("test", startIndex) - 1, 1)
        Catch
        End Try
        If RichTextBox1.SelectedText = " " Or RichTextBox1.SelectedText = Nothing Then
            RichTextBox1.Select(RichTextBox1.Text.IndexOf("hello", startIndex) + "test".Length, 1)
            If RichTextBox1.SelectedText = " " Or RichTextBox1.SelectedText = Nothing Then
                RichTextBox1.Select(RichTextBox1.Text.IndexOf("hello", startIndex), "test".Length)
                RichTextBox1.SelectionColor = Color.Blue
            End If
        End If

        startIndex = RichTextBox1.Text.IndexOf("hello", startIndex) + "hello".Length
        RichTextBox1.SelectionStart = selectedIndex
        RichTextBox1.SelectionLength = 0
        RichTextBox1.SelectionColor = Color.Black
    End While

それが最善の方法かどうかはわかりませんが、うまくいきます。

于 2013-01-23T01:14:30.277 に答える
0

Private Sub RichTextBox1_DragOver(sender As Object, e As DragEventArgs) RichTextBox1.DragOver を処理します

    Dim p As Point
    p.X = e.X
    p.Y = e.Y
    Dim num As Integer
    Dim rightTXT As String
    Dim leftTXT As String
    Dim textpart As String
    Dim TSelect As Boolean
    Dim curpos As Integer = RichTextBox1.GetCharIndexFromPosition(RichTextBox1.PointToClient(p))
    Dim PosStart As Integer

    TSelect = False
    If e.Data.GetDataPresent(DataFormats.StringFormat) Then

        e.Effect = DragDropEffects.All

        Try
            leftTXT = Microsoft.VisualBasic.Left(RichTextBox1.Text, curpos)
            If InStr(leftTXT, "%", CompareMethod.Text) Then
                rightTXT = Microsoft.VisualBasic.Right(RichTextBox1.Text, Len(RichTextBox1.Text) - curpos)

                If InStr(rightTXT, "%", CompareMethod.Text) Then
                    PosStart = curpos - InStr(StrReverse(leftTXT), "%") + 1
                    num = curpos + InStr(rightTXT, "%") - PosStart - 1

                    textpart = (RichTextBox1.Text.Substring(PosStart, num).TrimEnd)

                    Label3.Text = "mouse drag over:" + textpart
                    Label5.Text = num.ToString()

                    If ListBox1.Items.Contains(textpart) Then
                        TSelect = True
                    End If
                End If
            End If
        Catch ex As Exception
            Label4.Text = ex.ToString()
        End Try

    End If

    If TSelect Then      
        Me.RichTextBox1.Select(PosStart - 1, num + 2)
        wordSearch = RichTextBox1.SelectedText

        Label4.Text = "word drag state: true"
        match = True   
    Else
        Label3.Text = "mouse drag over:"
        Label4.Text = "word drag state: false"
        Me.RichTextBox1.Select(0, 0)
    End If
End Sub
于 2013-09-12T12:11:08.667 に答える
0

これは、選択したテキストを見つけた後、黄色で強調表示するコードです (他の色に置き換えることができます)。

    'find the text that need to be highlighted.
    foundIndex = RichTextBox1.Find("hello", foundIndex + 1, -1, selectedFinds)
    RichTextBox1.Focus()

    If foundIndex = -1 Then
        MessageBox.Show("This document don't contains the text you typed, or any of the text you typed as a whole word or mach case.", "Find Text Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
 else
'now the text will be highlighted.
 RichTextBox1.SelectionBackColor = Color.Yellow
Richtextbox1.focus
    End If

そのコードが役立つことを願っています。

于 2013-04-26T05:40:09.523 に答える