0

I want to highlight the text inside the text Box when a "#" is typed followed by any character as Facebook does. I tried to use SelectionStart ,SelectionLength and SelectionBackColor properties but I am facing many problems.

I need a help, what is the best event to handle the characters that are being typed? I used TextChanged event but its not working will. And is there another properties instead of SelectionStart and SelectionLength?

Please just give me tips and I will search. I searched a lot but not finding useful topics.

Areej Qadomi
  • 143
  • 1
  • 1
  • 14
  • Are you doing this in Excel? vba <> vb.net. vba is for Excel, word, Access,... vb.net is in visual studios. – Scott Craner Apr 05 '17 at 14:04
  • @ScottCraner Oh sorry, No its vb.net in visual studio. do you have any idea how to do this? – Areej Qadomi Apr 05 '17 at 14:21
  • Nope but now that I fixed the tags you will get better help, from those who know. I would add any code you have tried. – Scott Craner Apr 05 '17 at 14:22
  • This would detect as the user types. 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 – codeMonger123 Apr 05 '17 at 14:27

2 Answers2

0

Unfortunately, I don't think there is a way to do that with vb.net as it requires a postback to the server to run. In order to have it highlight the text as the user types you would need a frontend script to run.

Here is a link that may be helpful: https://forums.asp.net/t/1597892.aspx?Highlight+text+in+a+textbox

Wenadin
  • 396
  • 5
  • 17
0

I made it ! Its working :) ... This code detect hashtag entered by user and color it with green, also when the number of char exceed 140 it start highlighting them with red. I used the KeyPress event instead of 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
Areej Qadomi
  • 143
  • 1
  • 1
  • 14