How to have Marquee kind of text in vb.net? - vb.net

Is there any way to make the text in a Windows Form to scroll like the text in a marquee tag in HTML?

You can use a timer and a couple of variables to help you do so. Something like this can be done...
'Class-level variables.
Private m_intMarqueeCounter As Integer = 1
Private m_bolMarqueeIncrementUp As Boolean = True
Private Sub YourMarqueeTimer_Tick()
'You can decide what number is best for your app.
If m_intMarqueeCounter = 10 Then
m_bolMarqueeIncrementUp = False
End If
If m_intMarqueeCounter = 0 Then
m_bolMarqueeIncrementUp = True
End If
Dim intX As Integer
For intX = 0 to m_intMarqueeCounter
frmYourForm.Text = " " & "Your Title"
Next
If m_bolMarqueeIncrementUp Then
m_intMarqueeCounter += 1
Else
m_intMarqueeCounter -= 1
End If
End Sub

Const marqueeText As String = "The quick brown fox... "
Dim sb As New System.Text.StringBuilder
Dim direction As Boolean = False 'true = left to right, false = right to left
Private Sub Timer1_Tick(sender As System.Object, _
e As System.EventArgs) Handles Timer1.Tick
If sb.Length = 0 Then sb.Append(marqueeText)
If direction Then
sb.Insert(0, sb(sb.Length - 1))
sb.Remove(sb.Length - 1, 1)
Else
sb.Append(sb(0))
sb.Remove(0, 1)
End If
Me.Text = sb.ToString
End Sub

Related

How to randomize a quiz on vbnet

I'm new at visual basic programming and everything was fine until our topic shifted to arrays. I tried to understand it's code using Java. (Example: method are called functions.. .)
My prof has given us an exercise to create a Quiz program that asks the user more than 5 questions (in textbox) with choices (in buttons) and computes the score at the end (All just in one form). If the user click an a button it will tell if it's right or wrong and then proceed to change the question along with the choices.
*Required: - After the user finish the quiz the score will be displayed and there should be a restart button and all the question will be asked again randomly no pattern. - Try to make functions.
I tried searching the web since yesterday and I still have made no progress at my code.
Public Class Form1
Dim questions(5) As String
Dim answers(5) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Method/Function for loading the Q&A
loadQsAndAs()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Me.Close()
End Sub
Private Sub loadQsAndAs()
'Questions
questions(0) = "What is 1 + 1?"
questions(1) = "Who is the first man to walk on the Moon?"
questions(2) = "What is the name of the main character in the movie: Yes Man! (2007)"
questions(3) = "If I gave you three apples and you ate two, how many is left?"
questions(4) = "What do you want in your final grade?"
questions(5) = "What is the name of the thing(s) that you use whenever you eat?"
'Answers
answers(0) = "2"
answers(1) = "Neil Armstrong"
answers(2) = "Jim Carrey"
answers(3) = "1"
answers(4) = "A 4.0"
answers(5) = "A Spoon and Fork"
TextBox1.Text = setTheQuestion()
Button1.Text = setTheAnswer1()
Button2.Text = setTheAnswer2()
Button3.Text = setTheAnswer3()
Button4.Text = setTheAnswer4()
End Sub
Private Function setTheQuestion() As String
Dim randomValue As New Random
Dim randomQ As String = ""
Dim i As Integer
Dim index As Integer
For i = 0 To 0
index = randomValue.Next(0, questions.Length)
randomQ &= questions(index)
Next
Return randomQ
End Function
Private Function setTheAnswer1() As String
Dim randomValue As New Random
Dim randomAns As String = ""
Dim i As Integer
Dim index As Integer
For i = 0 To 0
index = randomValue.Next(0, answers.Length)
randomAns &= answers(index)
Next
Return randomAns
End Function
Private Function setTheAnswer2() As String
Dim randomValue As New Random
Dim randomAns As String = ""
Dim i As Integer
Dim index As Integer
For i = 0 To 0
index = randomValue.Next(1, answers.Length)
randomAns &= answers(index)
Next
Return randomAns
End Function
Private Function setTheAnswer3() As String
Dim randomValue As New Random
Dim randomAns As String = ""
Dim i As Integer
Dim index As Integer
For i = 0 To 0
index = randomValue.Next(2, answers.Length)
randomAns &= answers(index)
Next
Return randomAns
End Function
Private Function setTheAnswer4() As String
Dim randomValue As New Random
Dim randomAns As String = ""
Dim i As Integer
Dim index As Integer
For i = 0 To 0
index = randomValue.Next(3, answers.Length)
randomAns &= answers(index)
Next
Return randomAns
End Function
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
loadQsAndAs()
End Sub
End Class
Public Class Form1
Dim questions As New ArrayList
Dim answers As New ArrayList
Dim dtQAMain As New DataTable
Dim questionsCopy As New ArrayList
Dim alAnsButton As New ArrayList 'arraylist to store all answer button.
Dim totalScore As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
'Method/Function for loading the Q&A
alAnsButton.Add(Button1)
alAnsButton.Add(Button2)
alAnsButton.Add(Button3)
alAnsButton.Add(Button4)
loaddtQA()
loadQsAndAs()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button5.Click
Me.Close()
End Sub
Private Sub loaddtQA()
dtQAMain = New DataTable
dtQAMain.Columns.Add("Q")
dtQAMain.Columns.Add("A")
For i = 0 To 5
questions.Add("")
answers.Add("")
Next
'Questions
questions(0) = "What is 1 + 1?"
questions(1) = "Who is the first man to walk on the Moon?"
questions(2) = "What is the name of the main character in the movie: Yes Man!(2007)"
questions(3) = "If I gave you three apples and you ate two, how many is left?"
questions(4) = "What do you want in your final grade?"
questions(5) = "What is the name of the thing(s) that you use whenever you eat?"
'Answers
answers(0) = "2"
answers(1) = "Neil Armstrong"
answers(2) = "Jim Carrey"
answers(3) = "1"
answers(4) = "A 4.0"
answers(5) = "A Spoon and Fork"
For i = 0 To questions.Count - 1
dtQAMain.Rows.Add(questions(i), answers(i)) 'assign QA in table for scoring purpose later
Next
End Sub
Private Sub loadQsAndAs()
Label1.Visible = False
For i = 0 To alAnsButton.Count - 1
alAnsButton(i).visible = True
Next
questionsCopy = New ArrayList
questionsCopy = questions.Clone 'close a copy so we dont effect the actual question copy when randomize and eliminate asked question from arraylist
TextBox1.Text = setTheQuestion()
setTheAnswer()
End Sub
Private Function setTheQuestion() As String
Dim randomValue As New Random
Dim randomQ As String = ""
Dim index As Integer
If questionsCopy.Count <> 0 Then
index = randomValue.Next(0, questionsCopy.Count - 1)
randomQ = questionsCopy(index)
questionsCopy.RemoveAt(index) 'asked question will be remove.
Else ' questions are finished, show the mark
ShowMark()
End If
Return randomQ
End Function
Private Sub setTheAnswer() 'randonmize the answer and assign to button
If TextBox1.Text = "" Then Exit Sub ' if question finish exit sub
Dim randomValue As New Random
Dim NewIndex As Integer
Dim temp As String
Dim answersCopy As ArrayList = answers.Clone
For n = answersCopy.Count - 1 To 0 Step -1
NewIndex = randomValue.Next(0, n + 1)
' Swap them.
temp = answersCopy(n)
answersCopy(n) = answersCopy(NewIndex)
answersCopy(NewIndex) = temp
Next
Dim AnswerRowCheck As Integer = questions.IndexOf(TextBox1.Text)
Dim ActualAnswer As String = dtQAMain.Rows(AnswerRowCheck).Item("A") 'check actual answer
Dim totalRemove As Integer = 0
For i = answersCopy.Count - 1 To 0 Step -1
If totalRemove = 2 Then Exit For
If answersCopy(i) <> dtQAMain.Rows(AnswerRowCheck).Item("A") Then
answersCopy.RemoveAt(i)
totalRemove += 1
End If
Next 'remove 2 of the selection,since only 4 button for answer selection and should not take out the actual answer
For i = 0 To alAnsButton.Count - 1
alAnsButton(i).text = answersCopy(i)
Next
End Sub
Private Sub ShowMark()
For i = 0 To alAnsButton.Count - 1
alAnsButton(i).text = "" 'clear the text, no more input receive from user.
Next
Label1.Visible = True
Label1.Text = totalScore & " out of 6 are correct."
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
totalScore = 0
loadQsAndAs() 'refresh question
End Sub
Private Sub Button4_MouseClick(sender As Object, e As MouseEventArgs) Handles Button4.MouseClick, Button3.MouseClick, Button2.MouseClick, Button1.MouseClick
If sender.text = "" Then Exit Sub
Dim AnswerRowCheck As Integer = questions.IndexOf(TextBox1.Text) 'search question number
If dtQAMain.Rows(AnswerRowCheck).Item("A") = sender.text Then 'checking answer
totalScore += 1
End If
TextBox1.Text = setTheQuestion() 'next question
setTheAnswer()
End Sub
End Class
The code had cover most of the necessary comment which include the idea and how it work. Random() and arraylist are the key for this program to function, just pay more attention on it. Good luck.
How about trying this?
Public Class Form1
Dim questions(5) As String
Dim answers(5) As String
Private Sub loadQsAndAs()
'Questions
questions(0) = "What is 1 + 1?"
questions(1) = "Who is the first man to walk on the Moon?"
questions(2) = "What is the name of the main character in the movie: Yes Man! (2007)"
questions(3) = "If I gave you three apples and you ate two, how many is left?"
questions(4) = "What do you want in your final grade?"
questions(5) = "What is the name of the thing(s) that you use whenever you eat?"
'Answers
answers(0) = "2"
answers(1) = "Neil Armstrong"
answers(2) = "Jim Carrey"
answers(3) = "1"
answers(4) = "A 4.0"
answers(5) = "A Spoon and Fork"
Dim random As New Random
Dim indices = { 0, 1, 2, 3, 4, 5 }.OrderBy(Function (n) random.Next()).ToArray()
Dim question = random.Next(questions.Length - 1)
TextBox1.Text = questions(indices(question))
Button1.Text = answers(indices(0))
Button2.Text = answers(indices(1))
Button3.Text = answers(indices(2))
Button4.Text = answers(indices(3))
End Sub
End Class
That's it. Nice and simple. The key trick is creating a randomize indices array to do the lookups into the questions and answers arrays.
Private Dim rnd As Integer
Private Function setTheQuestion() As String
rnd = (CInt(Math.Ceiling(Rnd() * questions.Length)) + 1)
Return questions(rnd)
End Function
Private Function setTheAnswer1() As String
Return answers(rnd)
End Function

Score not being calculated correctly

Hi I'm created a program for a project and I've now started running some tests and the score for the user isn't being calculated correctly and I believe that it can't compare the answer given to the correct answer. I'm very confused and need any help that can be given. My code looks like this, any confusing parts and I'll try and explain.
Imports System.IO
Public Class QuestionScreen
Dim score As Integer = 0
Dim count As Integer
Dim Difficulty_ext As String
Dim questions, answers As New List(Of String)()
Private i As Integer
Sub ReadFile()
If Main.Diff_DDown.Text = "Easy" Then
Difficulty_ext = "questions - Easy"
ElseIf Main.Diff_DDown.Text = "Medium" Then
Difficulty_ext = "questions - Medium"
Else
Difficulty_ext = "questions - Difficult"
End If
Randomize()
Dim countline = File.ReadAllLines("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt").Length
Dim numline As Integer
Dim values() As String
Using sr As New StreamReader("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt")
While Not sr.EndOfStream
values = sr.ReadLine().Split(","c)
questions.Add(values(0))
answers.Add(values(1))
End While
End Using
numline = Int(Rnd() * countline)
For i As Integer = 0 To numline
Question.Text = questions(i)
Act_ANS.Text = answers(i)
Next
End Sub
Private Sub Pass_Btn_Click(sender As Object, e As EventArgs) Handles Pass_Btn.Click
If count < 10 Then
Call ReadFile()
count = count + 1
Ans_TxtBx.Text = ""
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
End If
End Sub
Public Sub Submit_Btn_Click(sender As Object, e As EventArgs) Handles Submit_Btn.Click
If count < 10 Then
Call ReadFile()
count = count + 1
If Ans_TxtBx.Text = answers(i) Then
score = score + 1
End If
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
count = 0
End If
Ans_TxtBx.Text = ""
End Sub
Private Sub QuestionScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call ReadFile()
End Sub
End Class
OK..... Try this, I have debugged your code trying to leave it pretty much as you had it, there were a number of problems, nothing major just a few lines of code in the wrong place....
Imports System.IO
Public Class Form1
Dim score As Integer = 0
Dim count As Integer
Dim Difficulty_ext As String
Dim questions, answers As New List(Of String)()
Private i As Integer
Sub ReadFile()
If Diff_DDown.Text = "Easy" Then
Difficulty_ext = "questions - Easy"
ElseIf Diff_DDown.Text = "Medium" Then
Difficulty_ext = "questions - Medium"
Else
Difficulty_ext = "questions - Difficult"
End If
Randomize()
Try
Dim countline = File.ReadAllLines("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt").Length
Dim numline As Integer
Dim values() As String
' clear the list of questions and answers
answers.Clear()
questions.Clear()
'''''''''''''''''''''''''''''''''''''''''
Using sr As New StreamReader("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt")
While Not sr.EndOfStream
values = sr.ReadLine().Split(","c)
questions.Add(values(0))
answers.Add(values(1))
End While
End Using
numline = Int(Rnd() * countline)
For i = 0 To numline
Question.Text = questions(i)
Act_ANS.Text = answers(i)
Next
Catch ex As Exception
End Try
End Sub
Private Sub Pass_Btn_Click(sender As Object, e As EventArgs) Handles Pass_Btn.Click
If count < 10 Then
count = count + 1
Ans_TxtBx.Text = ""
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
End If
Call ReadFile() ' move this to the bottom
End Sub
Public Sub Submit_Btn_Click(sender As Object, e As EventArgs) Handles Submit_Btn.Click
If count < 10 Then
count = count + 1
If Ans_TxtBx.Text = answers(i - 1) Then ' need to subtract 1 here
score = score + 1
End If
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
count = 0
End If
Ans_TxtBx.Text = ""
Call ReadFile() ' move this to the bottom
End Sub
Private Sub QuestionScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call ReadFile()
End Sub
End Class

How to color the text of RichTextBox depending on the prefix of the string?

I would like to color all the strings inside of the richtextbox. For example, if the prefix of a particular string is
Received: then it should be blue
Send: then it should be red
Info: then it should be green
*The way I output the text in the RichTextBox is by ascending order, it means that all the newest messages will be outputted at the top of the RichTextBox, the old ones will go down.
Screenshot:
Codes:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim msg_info As String = Nothing
If RB_Info.Checked = True Then
msg_info = "Info: "
End If
If RB_Send.Checked = True Then
msg_info = "Send: "
End If
If RB_Received.Checked = True Then
msg_info = "Received: "
End If
RichTextBox1.Text = RichTextBox1.Text.Insert(0, msg_info & TextBox1.Text & ControlChars.NewLine)
End Sub
End Class
Edit: I've tried applying this sub, but it changes the color of all items in ListBox
Sub HighlightPhrase(box As RichTextBox, phrase As String, color As Color)
Dim pos As Integer = box.SelectionStart
Dim s As String = box.Text
Dim ix As Integer = 0
While True
Dim jx As Integer = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase)
If jx < 0 Then
Exit While
End If
box.SelectionStart = jx
box.SelectionLength = phrase.Length
box.SelectionColor = color
ix = jx + 1
End While
box.SelectionStart = pos
box.SelectionLength = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim msg_info As String = Nothing
If RB_Info.Checked = True Then
msg_info = "Info: "
HighlightPhrase(RichTextBox1, "Info", Color.Blue)
End If
If RB_Send.Checked = True Then
msg_info = "Send: "
HighlightPhrase(RichTextBox1, "Send", Color.Green)
End If
If RB_Received.Checked = True Then
msg_info = "Received: "
HighlightPhrase(RichTextBox1, "Received", Color.Red)
End If
RichTextBox1.Text = RichTextBox1.Text.Insert(0, msg_info & TextBox1.Text & ControlChars.NewLine)
End Sub
I've edited my solution so the new line appears at the top of the RichTextBox.
I think my solution is ugly because basically it re-paints the color of each line after a new line has been added. Imagine if the RichTextBox has thousands of lines. I think you can minimize this by keeping a limit on how many lines is in the RTB, eg 100 lines.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim msg_info As String = Nothing
If RB_Info.Checked = True Then
msg_info = "Info: "
End If
If RB_Send.Checked = True Then
msg_info = "Send: "
End If
If RB_Received.Checked = True Then
msg_info = "Received: "
End If
RichTextBox1.Text = RichTextBox1.Text.Insert(0, msg_info & TextBox1.Text & ControlChars.NewLine)
ChangeColor()
End Sub
Private Sub ChangeColor()
Dim lines = RichTextBox1.Text.Split(vbLf)
Dim startPos As Integer, endPos As Integer = -1
Dim myColor As Color
For i = 0 To lines.Length - 2 ' minus 2 because the last one is empty string
startPos = endPos + 1
endPos = startPos + lines(i).Length
RichTextBox1.Select(startPos, endPos)
If lines(i).StartsWith("Info: ") Then myColor = Color.Red
If lines(i).StartsWith("Send: ") Then myColor = Color.Blue
If lines(i).StartsWith("Received: ") Then myColor = Color.Green
RichTextBox1.SelectionColor = myColor
Next
End Sub
End Class
Alternative solution using a ListView
You might want to look at the ListView control. If you don't want to write anything in the list, I suggest you to replace the RichTextBox with a ListView. After you add a ListView, change the View property to List.
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim msg_info As String = Nothing
Dim myColor As Color
If RB_Info.Checked = True Then
msg_info = "Info: "
myColor = Color.Green
End If
If RB_Send.Checked = True Then
msg_info = "Send: "
myColor = Color.Red
End If
If RB_Received.Checked = True Then
msg_info = "Received: "
myColor = Color.Blue
End If
Dim li = New ListViewItem()
li.ForeColor = myColor
li.Text = msg_info & TextBox1.Text
ListView1.Items.Insert(0, li)
End Sub
End Class

VB.NET Form runs mouse.leave code when I hover over a control

I am trying to make a form grow and shrink when I enter and leave it with my mouse. However, when the mouse pointer goes over a control, it runs the mouse.leave script. How do I stop this?
Mouse.Leave code
Private Sub frm_MouseLeave(ByVal sender As System.Object, ByVal e As EventArgs)
Dim frm1 As Form = DirectCast(sender, Form)
frm1.Opacity = 0.4
frm1.Controls.Clear()
frm1.BringToFront()
frm1.Size = New Size(20, 50)
Dim test As Integer = 1
Dim counter As Integer = 0
Dim yaxis As Integer = 0
Dim fin As Boolean = False
Do Until fin = True
If frm1.Name = "frm" & test Then
yaxis = counter
fin = True
Else
counter += 10 + frm1.Height
test += 1
End If
Loop
frm1.Location = New Point(My.Computer.Screen.Bounds.Size.Width - frm1.Width, yaxis)
End Sub
Mouse.enter code
Private Sub frm_MouseEnter(ByVal sender As System.Object, ByVal e As EventArgs)
Dim frm1 As Form = DirectCast(sender, Form)
Dim lbl As Label = New Label
Dim btn As Button = New Button
Dim index As Integer = 0
For Each ch As Char In frm1.Name
If IsNumeric(ch) Then
index = index & ch
End If
Next
index -= 1
frm1.Controls.Add(lbl)
lbl.Text = listbox.Items.Item(index)
lbl.ForeColor = Color.White
lbl.AutoSize = True
lbl.Location = New Point((frm1.ClientSize.Width) / 2, (frm1.ClientSize.Height) / 2)
lbl.Show()
frm1.Controls.Add(btn)
btn.Text = "X"
btn.ForeColor = Color.White
btn.BackColor = Color.Black
btn.Font = New Drawing.Font("Arial", 12)
btn.AutoSize = True
btn.Location = New Point(200 - btn.Width, 0)
frm1.Opacity = 1
frm1.BringToFront()
frm1.Size = New Size(200, 100)
Dim test As Integer = 1
Dim counter As Integer = 0
Dim yaxis As Integer = 0
Dim fin As Boolean = False
Do Until fin = True
If frm1.Name = "frm" & test Then
yaxis = counter
fin = True
Else
counter += 60
test += 1
End If
Loop
frm1.Location = New Point(My.Computer.Screen.Bounds.Size.Width - frm1.Width, yaxis)
End Sub
Thanks for the help!!
Everytime you enter one of your controls you will generate a MouseEnter event for the control and a MouseLeave Event for the form, I would look at checking the position of the mouse and if it was contained in the Form exit the MouseLeave event. Try something like to see if it works for you.
If frm1.ClientRectangle.Contains(PointToClient(MousePosition)) Then Exit Sub
Also as an aside if you are not going to be using this code with multiple forms you can just use the Me keyword to reference your Form's Properties
If Me.ClientRectangle.Contains(PointToClient(MousePosition)) Then Exit Sub

How can limit how many times an operator shows up in my maths quiz in vb?

My task was to create a 10 question maths quiz with 2 random numbers and a random operator (only +, - or *) for each question. Also the program must ask for the users name. Which I have done.
But as my quiz has only 10 questions, there is a very small probability that one of the operators will be in the majority or that one may not show up. So basically I need something that can limit how many times each operator shows up, for example each operator can only show up 4 times.
As you can see from my code, I'm coding in form. I would be very grateful if you could help me with this as I couldn't find anything on the internet for it and I'm also not too sure how to go about solving this problem either.
Private Sub StartTheQuiz()
number1 = randomizer.Next(1, 13)
number2 = randomizer.Next(1, number1)
leftpluslbl.Text = number1.ToString
rightpluslbl.Text = number2.ToString
userinput.Value = 0
Correct.Visible = False
Incorrect.Visible = False
End Sub
Public Sub TheOperator()
Randomize()
operation = randomizer.Next(1, 4)
addmintime = operation
If addmintime = 1 Then
answer = number1 + number2
operatorlbl.Text = "+"
Userinput.Value = useranswer
ElseIf addmintime = 2 Then
answer = number1 - number2
operatorlbl.Text = "-"
Userinput.Value = useranswer
ElseIf addmintime = 3 Then
answer = number1 * number2
operatorlbl.Text = "x"
Userinput.Value = useranswer
End If
End Sub
Public Function CheckTheAnswer()
If userinput.Value = answer Then
Return True
Else
Return False
End If
End Function
Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
If NameBox.Text = "What is your name? Enter your name here" Then
NameBox.BackColor = Color.Red
NextBtn.Visible = False
Check.Visible = False
Else
StartTheQuiz()
NameBox.BackColor = Color.LimeGreen
Start.Enabled = False
Start.Visible = False
TheOperator()
question = 0
score = 0
Check.Visible = True
NameBox.Enabled = False
End If
End Sub
Private Sub Check_Click(sender As Object, e As EventArgs) Handles Check.Click
If CheckTheAnswer() = True Then
Correct.Visible = True
Incorrect.Visible = False
question = question + 1
score = score + 1
ElseIf CheckTheAnswer() = False Then
Correct.Visible = False
Incorrect.Visible = True
question = question + 1
score = score + 0
End If
NextBtn.Visible = True
Check.Visible = False
End Sub
Private Sub NextBtn_Click(sender As Object, e As EventArgs) Handles NextBtn.Click
If question < 10 Then
StartTheQuiz()
TheOperator()
Check.Visible = True
NextBtn.Visible = False
ElseIf question >= 10 Then
MessageBox.Show("You have scored " & score & "/10")
MessageBox.Show("Goodbye")
Me.Close()
End If
End Sub
End Class
Here's an example of what I mentioned in the comments. It builds a list of the desired number of operations in equal quantities, then shows how to pull a random one from that list:
Public Class Form1
Private Operations As New List(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Operations = CreateOperations(10)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Operations.Count > 0 Then
Dim operation As String = NextRandomOperation()
Debug.Print(operation)
Else
Debug.Print("No more operations in list!")
End If
End Sub
Private Function CreateOperations(ByVal NumberOperations As Integer) As List(Of String)
Dim operators() As String = {"+", "-", "X"}
Dim operations As New List(Of String)
While Operations.Count < NumberOperations
Operations.AddRange(operators)
End While
While Operations.Count > NumberOperations
Operations.RemoveAt(Operations.Count - 1)
End While
Return operations
End Function
Private Function NextRandomOperation() As String
Static R As New Random
If Operations.Count > 0 Then
Dim index As Integer = R.Next(Operations.Count)
Dim operation As String = Operations(index)
Operations.RemoveAt(index)
Return operation
End If
Return ""
End Function
End Class
The proposed solution above will always produce four +, three - and three X. If you want a more random distribution you need to randomize the operators() array.
Dim rnd As New System.Random
Dim operators() As String = New String() {"+", "-", "X"}.OrderBy(Function() rnd.Next).ToArray