Results 1 to 6 of 6

Thread: How to find the last 7 words in single line richtextbox and highlight them

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    15

    How to find the last 7 words in single line richtextbox and highlight them

    Name:  Project1.jpg
Views: 227
Size:  9.7 KB

    Hi guys sorry I have no code written because I'm not sure where to start.

    I have a simple program with a single line richtextbox. I have buttons for all the numbers and characters in a deck of cards. For example buttons Joker A 2 3 4 5 6 7 8 9 10 J Q K and also a ? button which I want to treat as a word as well.

    When I press a button it adds the corresponding num or letter to the richtextbox with a space following right after it.

    What I am trying to achieve is when the word count is greater then 7 the last 7 words in the line of text will be highlighted. Which in this case except for the number 10 they are all 1 letter words

    Does anyone have the expertise to know how to achieve this?

    By the way my code sucks I know because I am just learning but everything works so far just need that feature to work. But here is all my code

    Thanks in advance

    Code:
    Imports System.Text.RegularExpressions
    Public Class HF_Helper
        Public resultsArray(0) As String
        Public results As Integer
        Private Sub Joker_Click(sender As Object, e As EventArgs) Handles Joker.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "JK "
            MyBox.SelectionColor = Color.DodgerBlue
            MyBox.AppendText("J ")
        End Sub
    
        Private Sub Wild2_Click(sender As Object, e As EventArgs) Handles Wild2.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "2 "
            MyBox.SelectionColor = Color.DarkOrange
            MyBox.AppendText("2 ")
        End Sub
    
        Private Sub Red3_Click(sender As Object, e As EventArgs) Handles Red3.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "R3 "
            MyBox.SelectionColor = Color.Red
            MyBox.AppendText("3 ")
        End Sub
    
        Private Sub Black3_Click(sender As Object, e As EventArgs) Handles Black3.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "3 "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("3 ")
        End Sub
    
        Private Sub Card4_Click(sender As Object, e As EventArgs) Handles Card4.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "4 "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("4 ")
        End Sub
    
        Private Sub Card5_Click(sender As Object, e As EventArgs) Handles Card5.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "5 "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("5 ")
        End Sub
    
        Private Sub Card6_Click(sender As Object, e As EventArgs) Handles Card6.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "6 "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("6 ")
        End Sub
    
        Private Sub Card7_Click(sender As Object, e As EventArgs) Handles Card7.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "7 "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("7 ")
        End Sub
    
        Private Sub Card8_Click(sender As Object, e As EventArgs) Handles Card8.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "8 "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("8 ")
        End Sub
    
        Private Sub Card9_Click(sender As Object, e As EventArgs) Handles Card9.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "9 "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("9 ")
        End Sub
    
        Private Sub Card10_Click(sender As Object, e As EventArgs) Handles Card10.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "10 "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("10 ")
        End Sub
    
        Private Sub CardJ_Click(sender As Object, e As EventArgs) Handles CardJ.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "J "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("J ")
        End Sub
    
        Private Sub CardQ_Click(sender As Object, e As EventArgs) Handles CardQ.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "Q "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("Q ")
        End Sub
    
        Private Sub CardK_Click(sender As Object, e As EventArgs) Handles CardK.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "K "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("K ")
        End Sub
    
        Private Sub CardA_Click(sender As Object, e As EventArgs) Handles CardA.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "A "
            MyBox.SelectionColor = Color.Black
            MyBox.AppendText("A ")
        End Sub
    
        Private Sub CardQuest_Click(sender As Object, e As EventArgs) Handles CardQuest.Click
            results = resultsArray.Count - 1
            ReDim Preserve resultsArray(results + 1)
            resultsArray(results) = "? "
            MyBox.SelectionColor = Color.DarkOliveGreen
            MyBox.AppendText("? ")
        End Sub
    
        Private Sub MyBox_TextChanged(sender As Object, e As EventArgs) Handles MyBox.TextChanged
            MyBox.SelectionAlignment = HorizontalAlignment.Left
        End Sub
    
        Private Sub UndoButton_Click(sender As Object, e As EventArgs) Handles UndoButton.Click
    
            If resultsArray.Count <= 1 Then
                MyBox.Lines = MyBox.Lines.Take(MyBox.Lines.Count - 1).ToArray
                ReDim resultsArray(0)
            Else
                MyBox.Lines = MyBox.Lines.Take(MyBox.Lines.Count - 1).ToArray
                ReDim Preserve resultsArray(resultsArray.Count - 2)
            End If
    
            Dim i As Integer
            For i = 0 To resultsArray.Count - 2
                If resultsArray(i) = "JK " Then
                    MyBox.SelectionColor = Color.DodgerBlue
                    MyBox.AppendText("J ")
                ElseIf resultsArray(i) = "R3 " Then
                    MyBox.SelectionColor = Color.Red
                    MyBox.AppendText("3 ")
                ElseIf resultsArray(i) = "? " Then
                    MyBox.SelectionColor = Color.DarkOliveGreen
                    MyBox.AppendText("? ")
                ElseIf resultsArray(i) = "2 " Then
                    MyBox.SelectionColor = Color.DarkOrange
                    MyBox.AppendText("2 ")
                Else
                    MyBox.SelectionColor = Color.Black
                    MyBox.AppendText(resultsArray(i))
                End If
                'MyBox.AppendText(resultsArray(i))
            Next i
    
        End Sub
    
        Private Sub HF_Helper_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.TopMost = True
        End Sub
    
        Private Sub CLR_Click(sender As Object, e As EventArgs) Handles CLR.Click
            MyBox.Lines = MyBox.Lines.Take(MyBox.Lines.Count - 1).ToArray
            ReDim resultsArray(0)
        End Sub
    
        Private Sub UP_Click(sender As Object, e As EventArgs) Handles UP.Click
            MyBox.Lines = MyBox.Lines.Take(MyBox.Lines.Count - 1).ToArray
    
            If resultsArray.Count - 1 >= 7 Then
                ReDim Preserve resultsArray(resultsArray.Count - 8)
            Else
                ReDim Preserve resultsArray(resultsArray.Count - resultsArray.Count)
            End If
    
            Dim i As Integer
            For i = 0 To resultsArray.Count - 2
                If resultsArray(i) = "JK " Then
                    MyBox.SelectionColor = Color.DodgerBlue
                    MyBox.AppendText("J ")
                ElseIf resultsArray(i) = "R3 " Then
                    MyBox.SelectionColor = Color.Red
                    MyBox.AppendText("3 ")
                ElseIf resultsArray(i) = "? " Then
                    MyBox.SelectionColor = Color.DarkOliveGreen
                    MyBox.AppendText("? ")
                ElseIf resultsArray(i) = "2 " Then
                    MyBox.SelectionColor = Color.DarkOrange
                    MyBox.AppendText("2 ")
                Else
                    MyBox.SelectionColor = Color.Black
                    MyBox.AppendText(resultsArray(i))
                End If
    
            Next i
        End Sub
    
        Private Sub Close_Click(sender As Object, e As EventArgs) Handles Close.Click
            Application.Exit()
        End Sub
    
        Private Sub Rollup_Click(sender As Object, e As EventArgs) Handles Rollup.Click
            If Me.FormBorderStyle = FormBorderStyle.FixedToolWindow Then
                Me.FormBorderStyle = FormBorderStyle.None
            Else
                Me.FormBorderStyle = FormBorderStyle.FixedToolWindow
            End If
    
        End Sub
    End Class

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: How to find the last 7 words in single line richtextbox and highlight them

    This may not be perfect, but I think it is pretty close to what you want:
    Code:
    Private Sub MyBox_TextChanged(sender As Object, e As EventArgs) Handles MyBox.TextChanged
        Dim selectionStart = MyBox.SelectionStart
        ' Reset the existing formatting
        MyBox.SelectionStart = 0
        MyBox.SelectionLength = If(MyBox.TextLength > 0, MyBox.TextLength - 1, 0)
        MyBox.SelectionBackColor = MyBox.BackColor
    
        ' Color the last 7 words
        Dim words = MyBox.Text.Split(" "c)
        If (words.Length > 7) Then
            Dim allButLast7Words = words.Take(words.Length - 7)
            Dim allButLast7WordsLength = allButLast7Words.Sum(Function(word) word.Length + 1)
            MyBox.SelectionStart = allButLast7WordsLength
            MyBox.SelectionLength = MyBox.TextLength - allButLast7WordsLength
            MyBox.SelectionBackColor = Color.LightGoldenrodYellow
        End If
    
        ' Reset the cursor
        MyBox.SelectionStart = selectionStart
    End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    15

    Re: How to find the last 7 words in single line richtextbox and highlight them

    Quote Originally Posted by dday9 View Post
    This may not be perfect, but I think it is pretty close to what you want:
    Code:
    Private Sub MyBox_TextChanged(sender As Object, e As EventArgs) Handles MyBox.TextChanged
        Dim selectionStart = MyBox.SelectionStart
        ' Reset the existing formatting
        MyBox.SelectionStart = 0
        MyBox.SelectionLength = If(MyBox.TextLength > 0, MyBox.TextLength - 1, 0)
        MyBox.SelectionBackColor = MyBox.BackColor
    
        ' Color the last 7 words
        Dim words = MyBox.Text.Split(" "c)
        If (words.Length > 7) Then
            Dim allButLast7Words = words.Take(words.Length - 7)
            Dim allButLast7WordsLength = allButLast7Words.Sum(Function(word) word.Length + 1)
            MyBox.SelectionStart = allButLast7WordsLength
            MyBox.SelectionLength = MyBox.TextLength - allButLast7WordsLength
            MyBox.SelectionBackColor = Color.LightGoldenrodYellow
        End If
    
        ' Reset the cursor
        MyBox.SelectionStart = selectionStart
    End Sub
    wow this was perfect thank you so much the only change i made was changed -7 to -8 so it did 7 words instead of 6.. You rock bro going to go and study it now to see why that works.

    tysm again

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: How to find the last 7 words in single line richtextbox and highlight them

    Quote Originally Posted by bOywOnder123 View Post
    ...going to go and study it now to see why that works...
    The stuff before and after the conditional statement are pretty straight forward. Here is some explanation on the important bits:
    • Declaring the allButLast7Words will take everything except for the last 7 items in the words collection.
    • Declaring the allButLast7WordsLength will get the length (plus 1 for the space separator) of each word in the words collection and then add them all up
    • The rest inside the conditional statement specifies the selection and then sets the BackColor of said selection
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    15

    Re: How to find the last 7 words in single line richtextbox and highlight them

    Quote Originally Posted by dday9 View Post
    The stuff before and after the conditional statement are pretty straight forward. Here is some explanation on the important bits:
    • Declaring the allButLast7Words will take everything except for the last 7 items in the words collection.
    • Declaring the allButLast7WordsLength will get the length (plus 1 for the space separator) of each word in the words collection and then add them all up
    • The rest inside the conditional statement specifies the selection and then sets the BackColor of said selection
    hey tysm for the explanation.. I wonder if I can ask you one more thing and maybe you can give me the lingo I need to research...For the same app when I get to the end of the textbox line it continues to fill in the numbers I press but they are off the screen and since scroll bars are shut off I have to click into the textbox and drag to the end just to see the end of the line..The app is a fixed size and scrollbars aren't useful to me.

    Is there a way to make it autoscroll to the end of the line when it is no longer in view.. I only found docs to autoscroll to the end of the rtb down to the bottom...but im a one line text box im already at the bottom lol

    thanks again in advance

  6. #6
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,500

    Re: How to find the last 7 words in single line richtextbox and highlight them

    Quote Originally Posted by bOywOnder123 View Post
    Which in this case except for the number 10 they are all 1 letter words
    Here's just a suggestion, instead of using A to K etc. as text for your buttons, don't use text at all... Use an image. Use your current code for your application but don't use the Button's .Text... Use the Button's .Tag instead and add the relevant image.

    Just use any image utility to produce an image of each of the characters that you need... including one with an image of No. 10. Add these 14 (?) images to the Resources folder.
    Declare and initiate an Image variable for each image to use in your project.

    I recommend using an image in .png format, especially if you'd like to use a transparent background.



    Poppa
    Along with the sunshine there has to be a little rain sometime.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width