Home > Software design >  Visual_basic : How to get single letters and letterstrings of 2 and 3 consecutive letters in the sam
Visual_basic : How to get single letters and letterstrings of 2 and 3 consecutive letters in the sam

Time:12-26

Trying to make a Windows app with Visual basic (2015) for my kids to help them in their reading process.
Made 3 labels (Box1, Box2, Box3) that can contain 1 letter each, but also 2 or 3 letterstrings (like "ea" "ou", "tr", "str"...).
And a label with clickfunction, so the predefined letters change randomly, with each click, in each box.

I managed to get individual predefined single letters in the boxes, but can't find a way to get 2 consecutive letters in those boxes.
Anyone can help me.

Examples of outcome:

consonants  vowels      consonants
m           a           p           => map
m           a           rs          => mars
m           ea          n           => mean
m           ee          t           => meet
spr         i           ng          => spring

My VB-code uptill now is:

Public Class VLLK1_2

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rand As New Random
        Dim letter As String = "gkmnprstv "
        Dim consonant As String = "gkmnprts "
        Dim vowel As String = "aei"
        letter = letter.Substring(rand.Next(letter.Length), 1)
        Box1.Text = letter
        vowel = vowel.Substring(rand.Next(vowel.Length), 1)
        Box2.Text = vowel
        consonant = consonant.Substring(rand.Next(consonant.Length), 1)
        Box3.Text = consonant
    End Sub

End Class

CodePudding user response:

Im no word expert but wouldnt it be easier like the coment just mentioned to create List(of String) containing words that you want to learn. Then select randomly each word and divide it into 3 parts and add those parts into your textboxes?

Private rand As New Random
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim ListW As New List(Of String)
        ListW.Add("apple")
        ListW.Add("pinaple")
        ListW.Add("pen")
        ListW.Add("orange")
        ListW.Add("water")

        'select random word from list

        Dim randW As String = ListW(rand.Next(0, ListW.Count - 1))
        'Dim randW = "pinaple"

        'find the integer divider value
        Dim div As Integer = randW.Length / 3


        'divide the word into 3 parts
        Dim leftP As String = Strings.Left(randW, div)
        Dim midP As String = Strings.Left(Strings.Mid(randW, div   1), div)

        Dim reminderLenght = randW.Length - (leftP.Length   midP.Length)

        Dim rightP As String = Strings.Right(randW, reminderLenght)

        'now add the parts to text boxes

        tb1.Text = leftP
        tb2.Text = midP
        tb3.Text = rightP
    End Sub
End Class

Preview:

enter image description here

  • Related