Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

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.

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. Tried to upload a pic of the gui but couldn't figure it out. But here is all my

Thanks in advance

What I have tried:

VB.NET
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
Posted
Updated 9-Feb-21 7:20am

You're duplicating a lot of code; you could create a method that takes your arguments / parameters (for example: "R3", Red, "3") and adds them to your array and textbox.

Since you're calculating a new index ("results"), you know when you have more than "7 words), so you can apply your "highlight" (I assume you mean "bold") when the count > 7 in your new method.

Set Font Attributes for RichTextBox Control - Windows Forms .NET Framework | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Maciej Los 9-Feb-21 14:27pm    
Triple Yes's for this statement: You're duplicating a lot of code; you could create a method that takes your arguments.
5ed!
How about something like the following
VB
Private Sub Highlight()

   Dim strt As Integer = MyBox.Text.Length - 14 'because each "word" has a space after it

   If strt > 0 Then
      MyBox.SelectionStart = strt
      MyBox.SelectionLength = 14
      MyBox.SelectionColor = Color.Yellow
   End If
End Sub
Caveat - this is completely untested and may not even compile, it's just to give you an idea.

It definitely won't work if you have Joker = JK because that ruins the "each word is 2 characters" assumption I've made in my simple example.
For that to work properly you will need to use the Split function (example) [^], count the "words" and find some way of determining exactly where the 7th from last word starts in the original text - see String.IndexOf Method (System) | Microsoft Docs[^] - Warning - make sure you are looking at the right occurrence if the letters can be duplicated.

You would call this after adding each "word" to the richtextbox.

I feel duty bound to point out that you cannot distinguish between a Jack and a Joker in your current code.

Another thing to point out - look how often you have the code
VB
results = resultsArray.Count - 1
ReDim Preserve resultsArray(results + 1)
resultsArray(results) = [some text here]
MyBox.SelectionColor = [some colour here]
MyBox.AppendText([some other text here])
The only thing that is different in each sub is the colour and the text value. You could pull that out into another sub - then instead of 111 lines of "click" code you could have something like
VB
Private Sub HandleClick(resText as String, colour As Color, dispText as String)
	results = resultsArray.Count - 1
	ReDim Preserve resultsArray(results + 1)
	resultsArray(results) = resText
	MyBox.SelectionColor = colour
	MyBox.AppendText(dispText)
    Highlight()
End Sub
Private Sub Joker_Click(sender As Object, e As EventArgs) Handles Joker.Click
	HandleClick("JK ",Color.DodgerBlue,"J ")
End Sub

Private Sub Wild2_Click(sender As Object, e As EventArgs) Handles Wild2.Click
	HandleClick("2 ",Color.DarkOrange,"2 ")
End Sub

Private Sub Red3_Click(sender As Object, e As EventArgs) Handles Red3.Click
	HandleClick("R3 ",Color.Red,"3 ")
End Sub
'Etc
which I would argue is clearer - not because it's much shorter than the original code but because it is now obvious that "something" is getting done with some specific values in each case - it's very easy to spot straight away that Jokers are going to be in DodgerBlue
 
Share this answer
 
Comments
Maciej Los 9-Feb-21 14:29pm    
Seems, you did what Gerry said.
5ed!
CHill60 9-Feb-21 15:32pm    
Took me so long to type it out Gerry got in with the concise version :laugh:
tgebrael 10-Feb-21 0:04am    
TYVM for your effort doing a rebuild to implement your style..very helpful ty.
CHill60 10-Feb-21 7:14am    
That is one of the nicest thank-yous I've had. Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900