Search code examples
arraysvb.netstringrichtextboxunderline

Underlining repeated words in VB


Homework, I need to create a program that works like http://typeracer.com/.

Heres what I've done so far:

Dim strContent As String = "the texts the text the text"
Dim arrNum As Integer = 0

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    arrContent = strContent.Split(" ")
    RichTextBox2.Text = strContent
End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    If TextBox1.Text = arrContent(arrNum) + " " Then
        TextBox1.Clear()
        arrNum = arrNum + 1
    End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim strSearch As String = arrContent(arrNum)
    Dim intIndex As Integer = RichTextBox2.Find(strSearch, 0, RichTextBoxFinds.WholeWord)
    If intIndex <> -1 Then
        RichTextBox2.SelectionStart = intIndex 
        RichTextBox2.SelectionLength = strSearch.Length            
        RichTextBox2.SelectionFont = New Font(RichTextBox2.Font, FontStyle.Bold)
    End If
End Sub

The problem is that repeated words doesn't get underlined, why?


Solution

  • It doesn't get underlined because you have it set to Bold the text, not underline it. And I would imagine if you put a breakpoint at Timer1_Tick, you wouldn't hit it because you haven't started the timer; you need to enable the timer if it isn't already, and start it.

    Add this to Form1_Load

    Timer1.Enabled = True
    Timer1.Start()
    

    And change

    RichTextBox2.SelectionFont = New Font(RichTextBox2.Font, FontStyle.Bold)
    

    to

    RichTextBox2.SelectionFont = New Font(RichTextBox2.Font, FontStyle.Underline)
    

    I tried this and the underline works, but you have some other logic issues you're going to need to sort out. If the same word is in the content more than once, your underline logic fails. It also does not underline the first word when the program first starts, and it will also error once you've finished typing all the words in the textbox because the array's index will be out of bounds. But now that the underline part is working, you can begin to debug the rest of that stuff