我想在vb.net的文本框中为另一个单词选择一个单词,并突出显示它们之间的所有内容。
一个例子是
我去了海边,和家人一起玩耍,然后6点钟回家。
起始字为had
,结束字为home
,其间突出显示所有内容。
我已经使用了一些代码
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim a As String
Dim b As String
a = TextBox2.Text 'means what ever is in textbox2 string to the location where "a" is
b = InStr(RichTextBox1.Text, a)
If b Then
RichTextBox1.Focus()
RichTextBox1.SelectionStart = b - 1
RichTextBox1.SelectionLength = Len(a)
但它不完全是我想要的。
除此之外还使用RegEx功能,如下所示
'gets rid of the enter line break eg <enter> command no new lines
Dim content As String = Replace(TextBox1.Text, Global.Microsoft.VisualBasic.ChrW(10), Nothing)
'searches for this tag in the brackets between ".*" will be the contents
Dim Regex As New Regex("<div.*class=""answer_text"".*id=editorText"".*""")
'Show the string
For Each M As Match In Regex.Matches(content)
'This will get the values, there are 3 atm meta.name des and content
Dim Description As String = M.Value.Split("""").GetValue(3)
'displays the content in the label
TextBox3.Text = "" & Description
Next
答案 0 :(得分:1)
这将选择startWord和endWord之间的所有内容,将它们都排除在外
Dim startWord As String = "had"
Dim endWord As String = "home"
Dim index As Integer = richTextBox1.Text.IndexOf(startWord)
richTextBox1.[Select](index + startWord.Length, richTextBox1.Text.IndexOf(endWord) - index - startWord.Length)
答案 1 :(得分:0)
这是一个涉及两个TextBox
控件的解决方案:
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim a As String
Dim b As String
Dim index_a As Integer
Dim index_b As Integer
a = TextBox1.Text
b = TextBox2.Text
index_a = InStr(RichTextBox1.Text, a)
index_b = InStr(RichTextBox1.Text, b)
If index_a And index_b Then
RichTextBox1.Focus()
RichTextBox1.SelectionStart = index_a - 1
RichTextBox1.SelectionLength = (index_b - index_a) + Len(b)
End If
End Sub
TextBox1
包含第一个单词,TextBox2
包含第二个单词。单击按钮时,它将从第一个单词突出显示到第二个单词的结尾。