【问题标题】:How to make Hashtag highlighting in visual basic 2012如何在 Visual Basic 2012 中突出显示 Hashtag
【发布时间】:2017-08-31 04:50:54
【问题描述】:

当键入“#”后跟任何字符时,我想突出显示文本框中的文本,就像 Facebook 所做的那样。我尝试使用 SelectionStart 、SelectionLength 和 SelectionBackColor 属性,但我遇到了很多问题。

我需要帮助,处理正在输入的字符的最佳事件是什么?我使用了 TextChanged 事件,但它不起作用。是否还有其他属性而不是 SelectionStart 和 SelectionLength?

请给我提示,我会搜索。 我搜索了很多,但没有找到有用的主题。

【问题讨论】:

  • 您是在 Excel 中执行此操作吗? vba vb.net。 vba 适用于 Excel、word、Access,... vb.net 在视觉工作室中。
  • @ScottCraner 哦,对不起,Visual Studio 中没有它的 vb.net。你知道怎么做吗?
  • 不,但现在我修复了标签,您将从知道的人那里获得更好的帮助。我会添加您尝试过的任何代码。
  • 这将检测为用户类型。 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles TextBox1.TextChanged System.Diagnostics.Debug.WriteLine("TextBox1.Text = " & TextBox1.Text) End Sub

标签: vb.net cursor hashtag textselection


【解决方案1】:

不幸的是,我认为 vb.net 没有办法做到这一点,因为它需要回发到服务器才能运行。为了让它在用户键入时突出显示文本,您需要一个前端脚本来运行。

这是一个可能有用的链接:https://forums.asp.net/t/1597892.aspx?Highlight+text+in+a+textbox

【讨论】:

    【解决方案2】:

    我做到了! 它的工作:) ... 此代码检测用户输入的主题标签并将其着色为绿色,当字符数超过 140 时,它开始用红色突出显示它们。 我使用 KeyPress 事件而不是 textChanged。

    Public Class Form1
    Dim previous As Char = " "
    Dim indexOfHash As Integer = -1
    Dim extraChar As Integer = 0
    Private Const charLimit As Integer = 140 ' maximum char allowed in the textbox
    Private Sub RichTextBox_keyPressed(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox.KeyPress
            Try
    
                Dim remainingCharCount As Integer
                Dim textBoxLength As Integer = RichTextBox.TextLength
    
                ' if input is BACKSPACE
                If (Not String.IsNullOrEmpty(e.KeyChar) AndAlso Asc(e.KeyChar) = 8) Then
                    remainingCharCount += 1
                End If
                remainingCharCount = charLimit - RichTextBox.TextLength
                charRemainingLabel.Text = remainingCharCount & " Characters Left"
                If remainingCharCount < 0 Then
                    extraChar += 1
                    Dim temp As Integer = RichTextBox.SelectionStart
                    RichTextBox.SelectionStart = RichTextBox.TextLength
                    RichTextBox.SelectionLength = remainingCharCount * -1
                    RichTextBox.SelectionBackColor = Color.LightCoral
                    RichTextBox.SelectionColor = Color.Black
                    charRemainingLabel.Text = String.Empty
                    charMinusLabel.Text = remainingCharCount & " Characters"
                    RichTextBox.SelectionStart = temp
                ElseIf remainingCharCount >= 0 AndAlso extraChar > 0 Then
                    Dim temp As Integer = RichTextBox.SelectionStart
                    RichTextBox.SelectionStart = RichTextBox.TextLength
                    RichTextBox.SelectionLength = extraChar
                    RichTextBox.SelectionBackColor = Color.Transparent
                    RichTextBox.SelectionColor = Color.Black
                    RichTextBox.SelectionStart = temp
                    extraChar = 0
                End If
    
                Dim input As Char = e.KeyChar
                Dim inputAsInteger As Integer = Asc(e.KeyChar)
                previous = RichTextBox.GetCharFromPosition(RichTextBox.GetPositionFromCharIndex(RichTextBox.TextLength))
    
                ' if Hash and preceeded with SPACE or ENTER
                If (Not String.IsNullOrEmpty(input) AndAlso input = "#" AndAlso (previous = " " Or String.IsNullOrEmpty(previous) Or previous = vbLf Or Asc(previous) = 13 Or RichTextBox.TextLength = 0)) Then
                    indexOfHash = RichTextBox.TextLength
                End If
    
                ' if Hash exists STOP when find SPACE or ENTER 
                If indexOfHash <> -1 AndAlso (input = " " Or String.IsNullOrEmpty(input) Or Asc(input) = 13 Or input = vbLf) AndAlso (RichTextBox.TextLength - indexOfHash) > 1 Then
                    RichTextBox.SelectionStart = indexOfHash
                    RichTextBox.SelectionLength = RichTextBox.TextLength - indexOfHash
                    RichTextBox.SelectionColor = Color.Green
                    RichTextBox.SelectionStart = RichTextBox.TextLength
                    'RichTextBox.DeselectAll()
                    RichTextBox.SelectionColor = Color.Black
                    indexOfHash = -1 ' end of HashTag 
                End If
    
                ' if input is BACKSPACE
                If (Not String.IsNullOrEmpty(input) AndAlso Asc(input) = 8 AndAlso (previous = "#")) Then
                    indexOfHash = RichTextBox.TextLength - 1
                    remainingCharCount += 1
                End If
    
            Catch ex As Exception
    
            End Try
        End Sub
    end class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      相关资源
      最近更新 更多