Creating a VB.NET Fill-In-The-Blank quiz application

Imports System.Security.Permissions

Public Class Form1
    Dim Deffinitons As ArrayList
    Dim myRandom As New Random()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadExnayWordList()
        ComboBox2.Items.Add("1 - Few Blanks")
        ComboBox2.Items.Add("2 - Here and there Blanks")
        ComboBox2.Items.Add("3 - Alot of Blanks")
        ComboBox2.SelectedIndex = 0
        DownloadDefinitions("torts.txt")
        DownloadDefinitions("criminal.txt")
        DownloadDefinitions("contracts.txt")
        ComboBox1.Items.AddRange(IO.Directory.GetFiles(".", "*.txt"))

        If ComboBox1.Items.Count > 0 Then
            ComboBox1.SelectedIndex = 1
        End If
        Return
        For Each MyFile In IO.Directory.GetFiles(".", "*.txt")
            Debug.WriteLine(MyFile)
            ComboBox1.Items.Add(MyFile)
        Next
    End Sub

    Private Function DownloadDefinitions(ByVal FileDef As String)
        If Not IO.File.Exists(".\" & FileDef) And System.Security.SecurityManager.IsGranted(New System.Security.Permissions.FileIOPermission(FileIOPermissionAccess.Write, System.IO.Directory.GetCurrentDirectory() & "\" & FileDef)) Then
            My.Computer.Network.DownloadFile("http://controllingtheinter.net/dcaupload/" & FileDef, ".\" & FileDef)
        End If
    End Function

    Private Function LoadDefinitons(ByVal FilePath As String) As ArrayList
        Deffinitons = New ArrayList
        Dim Data As String = IO.File.ReadAllText(FilePath)
        Dim SplitString As String = vbCrLf & vbCrLf
        Dim Deffs() As String = Data.Split({SplitString}, StringSplitOptions.RemoveEmptyEntries)
        For Each MyString In Deffs
            Debug.WriteLine("[" & MyString & " ]")
            Deffinitons.Add(MyString)
        Next
    End Function

    Private Function GrabRandomDef() As String
        Return Deffinitons.Item(myRandom.Next(0, Deffinitons.Count - 1))
    End Function

    Dim GlobalAnswer As String = Nothing
    Private Function FillInTheBlank() As Boolean
        Dim WasABlankFilledByChange As Boolean = False
        GlobalAnswer = GrabRandomDef()
        Dim DefSplit() As String = GlobalAnswer.Split(" ")

        While Not WasABlankFilledByChange
            'Debug.WriteLine("Cycle")
            For i = 2 To DefSplit.Length - 1
                If RandomChance(15) And Not DefSplit(i).ToString.Contains(vbCrLf) And CheckForAlphaNumericCharacters(DefSplit(i).ToString) And Not ExnayWordList.Contains(DefSplit(i).ToString) Then
                    'Debug.WriteLine("True")
                    DefSplit(i) = StrDup(DefSplit(i).Length, New String("_"))
                    WasABlankFilledByChange = True
                Else
                    'Debug.WriteLine("false")
                End If
            Next
        End While

        Dim Final As String = String.Join(" ", DefSplit)
        RichTextBox1.Text = Final
        Debug.WriteLine(Final)
        Application.DoEvents()
        MakeFirstWordRed()

        Return WasABlankFilledByChange
    End Function

    Dim ExnayWordList As New ArrayList
    Private Sub LoadExnayWordList()
        ExnayWordList.AddRange({"a", "an", "as", "at", "and", "is", "to", "in", "if", "by", "the", "or", "on", "when", "were"})
    End Sub
    Function CheckForAlphaNumericCharacters(ByVal StringToCheck As String)
        For i = 0 To StringToCheck.Length - 1
            If Not Char.IsLetter(StringToCheck.Chars(i)) And Not IsNumeric(StringToCheck.Chars(i)) Then
                Return False
            End If
        Next

        Return True 'Return true if all elements are characters
    End Function
    Private Function RandomChance(PercentChangeToBeTrue As Integer) As Boolean 'Passing the value 10 gives a 10% change to be true
        Dim RandomINT As Integer = myRandom.Next(0, 100)

        If ComboBox2.Text.StartsWith("1") Then
            PercentChangeToBeTrue = 15
        End If

        If ComboBox2.Text.StartsWith("2") Then
            PercentChangeToBeTrue = 30
        End If

        If ComboBox2.Text.StartsWith("3") Then
            PercentChangeToBeTrue = 50
        End If

        'Debug.WriteLine(RandomINT)
        If RandomINT < PercentChangeToBeTrue Then
            Return True
        Else
            Return False
        End If
        RandomINT = 0
    End Function

    Private Sub MakeFirstWordRed()
        Dim DefSplit() As String = RichTextBox1.Text.Split(" ")
        Dim StartingIndex As Integer = 0
        For Each MyString In DefSplit
            If MyString.StartsWith("_") Then
                Dim returnFirstPos = RichTextBox1.Find(MyString)
                'Debug.WriteLine(returnFirstPos)
                RichTextBox1.Select(returnFirstPos, MyString.Length)
                RichTextBox1.SelectionColor = Color.Red
                RichTextBox1.SelectionStart = returnFirstPos
                'RichTextBox1.ReadOnly = True
                RichTextBox1.Focus()
                Return
            End If
        Next
    End Sub

    Private Sub WriteFirstLetter()
        Dim returnFirstPos = RichTextBox1.Find("_")
        If returnFirstPos = -1 Then
            RichTextBox1.ReadOnly = True
            Return
        Else
            RichTextBox1.ReadOnly = False
        End If
        'Debug.WriteLine(returnFirstPos)
        RichTextBox1.Select(returnFirstPos, 1)
        RichTextBox1.SelectionColor = Color.Red
        RichTextBox1.SelectionStart = returnFirstPos
    End Sub

    Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged

    End Sub

    Private Sub RichTextBox1_Enter(sender As Object, e As EventArgs) Handles RichTextBox1.Enter
        WriteFirstLetter()
    End Sub

    Private Sub RichTextBox1_LocationChanged(sender As Object, e As EventArgs) Handles RichTextBox1.LocationChanged

    End Sub

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
        WriteFirstLetter()
        If RichTextBox1.Find("_") = -1 Then
            If StrComp(RichTextBox1.Text, GlobalAnswer, CompareMethod.Text) = 0 Then
                Debug.WriteLine("Got it!")
                ListBox1.Items.Add("Correct: " & GlobalAnswer.Replace(vbCrLf, "[crlf]"))
            Else
                ListBox1.Items.Add("Q: " & RichTextBox1.Text.Replace(vbCrLf, "[crlf]"))
                ListBox1.Items.Add("A: " & GlobalAnswer.Replace(vbCrLf, "[crlf]"))
                Debug.WriteLine("failed!")
            End If
            FillInTheBlank()
            RichTextBox1.ReadOnly = False
        End If
    End Sub

    Private Sub RichTextBox1_CursorChanged(sender As Object, e As EventArgs) Handles RichTextBox1.CursorChanged
        WriteFirstLetter()
    End Sub

    Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
        'Debug.WriteLine("Back2" & e.KeyChar)
        'WriteFirstLetter()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        FillInTheBlank()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        LoadDefinitons(ComboBox1.SelectedItem)
        FillInTheBlank()
    End Sub

    Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
        If e.KeyData = Keys.Back Then
            e.Handled = True
            BackSpace()
        Else
            WriteFirstLetter()
        End If
    End Sub

    Private Sub BackSpace()
        Dim returnFirstPos = RichTextBox1.Find("_")
        If returnFirstPos = -1 Then
            RichTextBox1.ReadOnly = True
            Return
        Else
            RichTextBox1.ReadOnly = False
        End If

        For i = (returnFirstPos - 1) To 1 Step -1
            RichTextBox1.Select(i, 1)
            'Debug.WriteLine(i & ": " & RichTextBox1.SelectionColor.ToString)
            If RichTextBox1.SelectionColor = Color.Red Then
                RichTextBox1.Select(i, 1)
                RichTextBox1.SelectionStart = i
                RichTextBox1.SelectedText = "_"
                Exit For
            End If
        Next

        WriteFirstLetter()

    End Sub
End Class

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *