“插入符位置”在 VB.NET 中用于语法突出显示

发布于 2024-08-15 13:52:55 字数 189 浏览 1 评论 0原文

我正在尝试制作一个带有语法突出显示的文本框(对于 VB.NET 2008 中的(HTML/CSS))。

我想如果我使用 RichTextBox.Find(),我可以为特定文本着色,但随后我需要调用 RichTextBox。 DeselectAll()。

问题是光标跳到了 RTB 的开头。


有什么想法吗?

I'm trying to make a TextBox with syntax highlighting (for (HTML/CSS) in VB.NET 2008.

I figured that if I use RichTextBox.Find(), I can color specific text, but then I need to call RichTextBox.DeselectAll().

The problem is that the the cursor jumps to the beginning of the RTB.

I'm using WinForms.
Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千笙结 2024-08-22 13:52:55

您可以使用 获取和设置光标位置SelectionStart 属性。

因此,你可以写,

Dim selStart As Integer = rtb.SelectionStart
'Do things
rtb.SelectionStart = selStart

You can get and set the cursor position using the SelectionStart property.

Therefore, you can write,

Dim selStart As Integer = rtb.SelectionStart
'Do things
rtb.SelectionStart = selStart
被你宠の有点坏 2024-08-22 13:52:55
Imports System.Text.RegularExpressions

Public Class Form1

'Create a Html Keyword Regex
Dim htmlkeywords As New System.Text.RegularExpressions.Regex("<html>|</html>|<head>|</head>|<meta|<p>|</p>|<div>|</div>")   <----add as many terms as you like between the () don't forget the pipe symbol between each term in the Regex.

'Then in your Richtextbox textchanged event add this 
Private Sub rtText_TextChanged(sender As Object, e As EventArgs) Handles rtText.TextChanged

    Dim selStart As Integer = rtText.SelectionStart

     Do Until False

        For Each keyWordMatch As Match In htmlkeywords.Matches(rtText.Text)

            rtText.Select(keyWordMatch.Index, keyWordMatch.Length)
            rtText.SelectionColor = Color.Purple
            rtText.SelectionStart = rtText.Text.Length 'this puts the caret at the end 
            rtText.SelectionLength = 0                 ' of the word

        Next keyWordMatch

        Exit Do
    Loop

    rtText.SelectionColor = Color.Black
    rtText.SelectionStart = selStart ' this makes sure that if your caret is behind a word and you press enter to move a text down a line; the caret will stay in position on the next line that you start typing on. You can remove this code to see what I'm talking about
End Sub

rtText 是我的 RichTextBox 名称。这会将您想要的单词更改为任何颜色,然后将其更改回黑色,您可以更改哪种颜色做什么。希望这有帮助!

Imports System.Text.RegularExpressions

Public Class Form1

'Create a Html Keyword Regex
Dim htmlkeywords As New System.Text.RegularExpressions.Regex("<html>|</html>|<head>|</head>|<meta|<p>|</p>|<div>|</div>")   <----add as many terms as you like between the () don't forget the pipe symbol between each term in the Regex.

'Then in your Richtextbox textchanged event add this 
Private Sub rtText_TextChanged(sender As Object, e As EventArgs) Handles rtText.TextChanged

    Dim selStart As Integer = rtText.SelectionStart

     Do Until False

        For Each keyWordMatch As Match In htmlkeywords.Matches(rtText.Text)

            rtText.Select(keyWordMatch.Index, keyWordMatch.Length)
            rtText.SelectionColor = Color.Purple
            rtText.SelectionStart = rtText.Text.Length 'this puts the caret at the end 
            rtText.SelectionLength = 0                 ' of the word

        Next keyWordMatch

        Exit Do
    Loop

    rtText.SelectionColor = Color.Black
    rtText.SelectionStart = selStart ' this makes sure that if your caret is behind a word and you press enter to move a text down a line; the caret will stay in position on the next line that you start typing on. You can remove this code to see what I'm talking about
End Sub

rtText is my RichTextBox name. This will change the word you want to whatever color then change it back to black, which you can change which colors do what. Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文