Vb.net 在windows窗体中突出显示richtextbox中的文本
当我在Vb.net 在windows窗体中突出显示richtextbox中的文本,vb.net,winforms,fonts,richtextbox,syntax-highlighting,Vb.net,Winforms,Fonts,Richtextbox,Syntax Highlighting,当我在RichTextBox中键入某个单词时,如何使其变得更高级 如何在文本中查找要使用的单词SelectionColor或SelectionFont 例如:我希望单词“hello”出现在RichTextBox中的所有时间都变成bold或变成颜色 然后,如果我打开我的程序并键入“你好,你好吗?”hello一词会变成粗体。。。有什么想法吗?(我的想法是制作一个语法突出显示的文本编辑器,它将指定单词) (很抱歉,如果还有这样的问题,我试图搜索,但没有找到对我有帮助的答案) 其windows窗体、vi
RichTextBox
中键入某个单词时,如何使其变得更高级
如何在文本中查找要使用的单词SelectionColor
或SelectionFont
例如:我希望单词“hello”出现在RichTextBox
中的所有时间都变成bold或变成颜色
然后,如果我打开我的程序并键入“你好,你好吗?”hello一词会变成粗体。。。有什么想法吗?(我的想法是制作一个语法突出显示的文本编辑器,它将指定单词)
(很抱歉,如果还有这样的问题,我试图搜索,但没有找到对我有帮助的答案)
其windows窗体、visual basic此代码应完成以下工作:
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
Dim searchstring As String=“hello”
“你要找的词
作为新列表的Dim计数(整数)()
对于i As Integer=0到richTextBox1.Text.Length-1
如果richTextBox1.Text.IndexOf(searchstring,i)-1,则
“如果找到了这个词
'将索引添加到列表中
count.Add(richTextBox1.Text.IndexOf(searchstring,i))
如果结束
下一个
尝试
对于i作为整数=0进行计数。计数-1
richTextBox1.[Select](计数(i),搜索字符串.Length)
richTextBox1.SelectionFont=新字体(richTextBox1.Font,FontStyle.Bold)
计数移除(i)
下一个
抓住
结束尝试
richTextBox1.[选择](richTextBox1.Text.Length,0)
richTextBox1.SelectionFont=新字体(richTextBox1.Font,FontStyle.Regula
对于每个索引,选择文本并将其加粗
现在将此代码添加到
TextChanged
-事件中,以便在您的单词的文本发生任何更改时进行检查。我以另一种方式获得它:
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
我不知道这是否是最好的方法,但它是有效的。这是一种代码,用于在找到选定文本后,以黄色突出显示选定文本(可替换为任何其他颜色):
'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
我希望这段代码能有所帮助。私有的Sub-RichTextBox1\u DragOver(发送者作为对象,e作为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
我发现上面的代码对于一个简单的任务来说太长/复杂了
Dim c As Integer = 0
Dim o As Integer = 0
Dim s As Integer = 0
Dim txt As String = RTB.Text
RTB.BackColor = Color.Black
Dim starts As Integer = 0
Do While txt.Contains(key) ' this avoids unnecessary loops
s = txt.IndexOf(key)
starts = s + o
RTB.Select(starts, key.Length)
RTB.SelectionBackColor = Color.Yellow
RTB.SelectionColor = Color.Blue
txt = txt.Substring(s + key.Length)
o += (s + key.Length)
c += 1
Loop
Me.Status.Text = c.ToString() & " found" ' and the number found
+1但他要求使用vb,当单词被更改时,它会将光标移到单词的开头。但是很好。你确定这样做有效吗?我测试了它,但不起作用。我编辑了你的代码。你不能将
字符串添加到int
。仍然很好,所以+1@JanesAbouChleih是的,它工作正常,我不知道为什么不工作或者你,我不知道你说的是什么:慢慢看修订版,你就会明白我对string
和int
的意思。Link:但我会向你解释:RichTextBox1.Text.IndexOf(“test”“hello”,asdstartIndex)。ToString()+“test”“hello”。Length
首先。ToString()
是一种方法,尽管它不接受参数。在本例中,您需要括号。然后您尝试将索引of(“test”“hello”,asdstartIndex)与的值相加。ToString()
“hello”.Length
。这不行:一个是字符串
,另一个是int
@Janesabouchleich hum,看起来你是对的,但奇怪的是,即使使用.ToString()
,这个代码对我也有效,但我想当.ToString()
返回一个数字时,它的总和会一样吗?