21.1k views
3 votes
Visual Basic Help:

Code a program that reads a test score (number) from a text-box, each time a button is clicked. In other words, every time a new score is entered in the text-box, the score should be taken by the program, stored and the text-box should be cleared, so that the user can enter a new score. The program should be able to check that the user is entering a number. If the user enters something different than a number (or leaves the text-box empty), the program should not continue and it should display a warning message to the user. After entering as many scores as wanted, the user should be able to click on a second button to display the two highest scores, among all the ones entered until that point. The program should also display the total number of scores entered by the user. Also, once the user clicks on the button to display the results and the results are displayed, the program should get ready to get a new set of scores and start again. If the user starts writing a new score, the text-boxes with the results should be cleared.
Example: Assume the user enters the following scores: 10, 5, 20. The scores are entered once at the time in the top text-box; when a score is entered, the user presses the button to "Read" the score. The score is taken by the program and the text-box is cleared, so that the next score can be entered. This process can be repeated as many times as the user wants and for as many scores as the user wants. Once all the scores are entered and read(or taken-in)by the program, the user clicks on the second button to display the results. The program should then display the two highest scores, among the ones entered. So, in the example, this would be 10, 20. And it should display the total number of scores entered by the user. In the example, it would be 3.
After that, the user should be able to repeat the process and enter a new set of scores, if wanted. The program should therefore be ready to take in new inputs and start the process again. When the user starts typing the new score, the text-boxes with the previous results (two highest scores and total num. of scores) should get cleared

User Blue Toque
by
3.1k points

1 Answer

7 votes

Answer:

See text attachment for complete source code (for the answer) where I used comments to explain each line of the program

Step-by-step explanation:

First, design the form using the following tools (and the accompanying properties) :

1 Label: Property: Text: Score

2 Buttons

Button1: Property: Name: btnAdd; Text: Add

Button2: Property: Name: btnDisplay; Text: Display

1 ListBox: Property: Visible: Hidden

1 TextBox: Property: Name: txtNum1

First, create the following function.

The function (named typecheck) validates input from the user (i.e. integer input only)

Function typecheck(ByVal input As String) As Boolean

Try

Convert.ToInt32(input)


Return\ True


Catch\ ex\ As\ Exception


Return\ False

End Try

End Function

It returns true if the input is integer and false, if otherwise.

Next, double-click on the "Add" button and write the following lines of code:

Dim num1 As String

num1 = txtNum1.Text

If num1 IsNot String.Empty Then

If typecheck(num1) Then


ListBox1.Items.Add(num1)


txtNum1.Text = String.Empty

Else


MsgBox(
Integer
Inpu t


txtNum1.Text = String.Empty


End\ If

Else


MsgBox(
can\ not\ be\ empty


txtNum1.Text = String.Empty


End\ If

Next, double-click on the "Display" button and write the following lines of code:

If ListBox1.Items.Count > 0 Then


Dim\ max1, max2\ As\ Integer

max1 = Convert.ToInt32(ListBox1.Items(0))

max2 = Convert.ToInt32(ListBox1.Items(0))


For\ count\ = 0\ To\ ListBox1.Items.Count - 1

If Convert.ToInt32(ListBox1.Items(
count)) >= max1 Then

max1 = Convert.ToInt32(ListBox1.Items(
count))


End\ If

Next


For\ count\ = 0\ To\ ListBox1.Items.Count - 1

If Convert.ToInt32(ListBox1.Items(
count)) >= max2 And Convert.ToInt32(ListBox1.Items(
count)) < max1 Then

max2 = Convert.ToInt32(ListBox1.Items(
count))


End\ If

Next

MsgBox("Highest Scores: " + max1.ToString() + " " + max2.ToString())

MsgBox("
Total: " + ListBox1.Items.Count.ToString())


End\ If

ListBox1.Items.Clear()

Visual Basic Help: Code a program that reads a test score (number) from a text-box-example-1
Visual Basic Help: Code a program that reads a test score (number) from a text-box-example-2
User Incomputable
by
3.6k points