1

我是一个新手(请温柔)。我已经研究了我的问题 - 找不到答案。我很确定我忽略了一些非常明显的东西。我下面的代码在富文本框中的字符串中找到第一个匹配项。我希望能够在每次单击按钮时在比赛(下一场比赛/上一场比赛)之间移动(一个按钮用于下一场比赛/第二个按钮用于上一场比赛)。有人可以帮帮我吗?谢谢。

Private Sub Button3_Click_5(sender As System.Object, e As System.EventArgs) Handles btnFocus.Click

    Dim MatchStart As Integer = -1
    Dim MatchLength = -1
    Dim MatchResult = Regex.Match(rtb.Text, rgxMyRegex)

    If MatchResult.success Then
        MatchStart = MatchResult.index
        MatchLength = MatchResult.length

        rtb.SelectionStart = MatchStart
        rtb.ScrollToCaret()
        rtb.Focus()

    End If

End Sub
4

3 回答 3

1

使用类似的东西

findTextAndHighlight(TextBox1.Text, RichTextBox1)

调用一个函数

Sub findTextAndHighlight(ByVal searchtext As String, ByVal rtb As RichTextBox)
    Dim textEnd As Integer = rtb.TextLength
    Dim index As Integer = 0
    Dim fnt As Font = New Font(rtb.Font, FontStyle.Bold)
    Dim lastIndex As Integer = rtb.Text.LastIndexOf(searchtext)
    While (index < lastIndex)
        rtb.Find(searchtext, index, textEnd, RichTextBoxFinds.WholeWord)
        rtb.SelectionFont = fnt
        rtb.SelectionLength = searchtext.Length
        rtb.SelectionColor = Color.Red
        rtb.SelectionBackColor = Color.Cyan
        index = rtb.Text.IndexOf(searchtext, index) + 1
    End While
End Sub

当然,它突出了所有这些,但您当然可以修改此代码以满足您的需要:P

希望这有帮助

来源

于 2013-07-25T18:56:28.567 回答
0

在这里,您拥有执行所需操作的代码的更新版本:

Private Sub Button3_Click_5(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    Dim MatchStart As Integer = -1
    Dim MatchLength = -1

    Dim count As Integer = -1
    Dim remainingText As String = rtb.Text
    Dim curText As String = ""
    Dim completed As Boolean = False
    Dim curStartIndex As Integer = 0
    Do
        completed = True
        Dim MatchResult = Regex.Match(remainingText, rgxMyRegex)

        If MatchResult.Success Then
            completed = False
            MatchStart = MatchResult.Index
            MatchLength = MatchResult.Length

            curStartIndex = curStartIndex + MatchLength 'Referred to the rtb index
            curText = remainingText.Substring(MatchStart, MatchLength)
            remainingText = rtb.Text.Substring(curStartIndex, rtb.Text.Length - curStartIndex)

            'If you want to select the last bit being analysed, you have to do:
            rtb.Select(curStartIndex - MatchLength, MatchLength)

        End If
    Loop While (Not completed)

End Sub

如果rgxMyRegex等于“12”并rtb包含“121212”,则此代码将迭代 3 次并相应地填充相应的变量(即,“12”和“1212”作为剩余文本,“12”和“12”作为剩余文本。 ..)。

于 2013-07-25T19:19:29.287 回答
0

您的应用程序需要在按钮单击之间至少保留足够的状态以突出显示下一个或上一个匹配项。我建议使用“匹配”正则表达式方法预先提取所有匹配项的集合,然后将该集合保留为您使用点击处理程序访问的状态变量。看起来像这样:

Public Class Form1
    Private RegexPattern As String = "sample"
    Private MatchState As MatchCollection  'The current state of all matches
    Private MatchPosition As Integer  'The current index into the MatchState collection

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Put some sample text in the RTB
        Me.RichTextBox1.Text = "Here sample is sample some sample text sample."

        'Load the initial state of the application
        Me.MatchState = Regex.Matches(Me.RichTextBox1.Text, Me.RegexPattern)

        'Highlight the first match
        Me.MatchPosition = 0
        Me.HighlightCurrentPosition()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Move the match backward
        Me.MatchPosition -= 1
        If Me.MatchPosition < 0 Then
            Me.MatchPosition = Me.MatchState.Count - 1
        End If

        Me.HighlightCurrentPosition()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'Move the match forward
        Me.MatchPosition += 1
        If Me.MatchPosition >= Me.MatchState.Count Then
            Me.MatchPosition = 0
        End If

        Me.HighlightCurrentPosition()
    End Sub

    Private Sub HighlightCurrentPosition()
        If Me.MatchPosition >= 0 And Me.MatchPosition < Me.MatchState.Count Then
            Dim match = Me.MatchState(Me.MatchPosition)
            Me.RichTextBox1.Focus()
            Me.RichTextBox1.Select(match.Index, match.Length)
        End If
    End Sub
End Class

You'll have to decide which event in your app's lifecycle makes the most sense to run the regex to generate the state. Here I just ran it when the form loads but you can adapt it to whatever works for you.

于 2013-07-26T07:35:28.293 回答