更改 RichTextBox 中所有字符串实例的字体样式
Change Font Style on All Instances of a String in a RichTextBox
我正在使用 VS2013 开发 VB.NET 4.5 项目。
我在表单上有一个 richtextbox,当单击一个按钮时,我需要在 richtextbox 中找到的特定字符串的所有实例上切换粗体设置。
我根据 this question.
整理了一些代码
Private Sub ToggleBold()
rtxtOutputText.SelectionStart = rtxtOutputText.Find("@#$%", RichTextBoxFinds.None)
rtxtOutputText.SelectionFont = New Font(rtxtOutputText.Font, FontStyle.Bold)
End Sub
但是,当单击切换粗体按钮时,它只会将字符串“@#$%”的第一个实例加粗。
如何将字符串的所有实例设置为粗体?也可以有几个串在一起(“@#$%@#$%@#$%”),所以每个都需要加粗。
(我知道我提到了 切换 粗体,但我稍后会设置切换部分,现在我只是想让粗体在所有情况下都能正常工作...)
只需为其添加一个循环并使用 RichTextBox.Find(String, Int32, RichTextBoxFinds)
overload 指定从何处开始查找。从当前索引 + 1 看,它不会 return 再次相同。
您实际上还应该 select 这个词,这样您才能确定粗体仅适用于当前实例 而不是它周围的文字。
Private Sub ToggleBold()
'Stop the control from redrawing itself while we process it.
rtxtOutputText.SuspendLayout()
Dim LookFor As String = "@#$%"
Dim PreviousPosition As Integer = rtxtOutputText.SelectionStart
Dim PreviousSelection As Integer = rtxtOutputText.SelectionLength
Dim SelectionIndex As Integer = -1
Using BoldFont As New Font(rtxtOutputText.Font, FontStyle.Bold)
While True
SelectionIndex = rtxtOutputText.Find(LookFor, SelectionIndex + 1, RichTextBoxFinds.None)
If SelectionIndex < 0 Then Exit While 'No more matches found.
rtxtOutputText.SelectionStart = SelectionIndex
rtxtOutputText.SelectionLength = LookFor.Length
rtxtOutputText.SelectionFont = BoldFont
End While
End Using
'Allow the control to redraw itself again.
rtxtOutputText.ResumeLayout()
'Restore the previous selection.
rtxtOutputText.SelectionStart = PreviousPosition
rtxtOutputText.SelectionLength = PreviousSelection
End Sub
感谢 Plutonix 告诉我处理字体。
我正在使用 VS2013 开发 VB.NET 4.5 项目。
我在表单上有一个 richtextbox,当单击一个按钮时,我需要在 richtextbox 中找到的特定字符串的所有实例上切换粗体设置。
我根据 this question.
整理了一些代码Private Sub ToggleBold()
rtxtOutputText.SelectionStart = rtxtOutputText.Find("@#$%", RichTextBoxFinds.None)
rtxtOutputText.SelectionFont = New Font(rtxtOutputText.Font, FontStyle.Bold)
End Sub
但是,当单击切换粗体按钮时,它只会将字符串“@#$%”的第一个实例加粗。
如何将字符串的所有实例设置为粗体?也可以有几个串在一起(“@#$%@#$%@#$%”),所以每个都需要加粗。
(我知道我提到了 切换 粗体,但我稍后会设置切换部分,现在我只是想让粗体在所有情况下都能正常工作...)
只需为其添加一个循环并使用 RichTextBox.Find(String, Int32, RichTextBoxFinds)
overload 指定从何处开始查找。从当前索引 + 1 看,它不会 return 再次相同。
您实际上还应该 select 这个词,这样您才能确定粗体仅适用于当前实例 而不是它周围的文字。
Private Sub ToggleBold()
'Stop the control from redrawing itself while we process it.
rtxtOutputText.SuspendLayout()
Dim LookFor As String = "@#$%"
Dim PreviousPosition As Integer = rtxtOutputText.SelectionStart
Dim PreviousSelection As Integer = rtxtOutputText.SelectionLength
Dim SelectionIndex As Integer = -1
Using BoldFont As New Font(rtxtOutputText.Font, FontStyle.Bold)
While True
SelectionIndex = rtxtOutputText.Find(LookFor, SelectionIndex + 1, RichTextBoxFinds.None)
If SelectionIndex < 0 Then Exit While 'No more matches found.
rtxtOutputText.SelectionStart = SelectionIndex
rtxtOutputText.SelectionLength = LookFor.Length
rtxtOutputText.SelectionFont = BoldFont
End While
End Using
'Allow the control to redraw itself again.
rtxtOutputText.ResumeLayout()
'Restore the previous selection.
rtxtOutputText.SelectionStart = PreviousPosition
rtxtOutputText.SelectionLength = PreviousSelection
End Sub
感谢 Plutonix 告诉我处理字体。