How about something like the following
Private Sub Highlight()
Dim strt As Integer = MyBox.Text.Length - 14
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
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
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
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