Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Open the Average Solution (Average Solution.sln) file contained in the ClearlyVB

ID: 3539460 • Letter: O

Question



Open the Average Solution (Average Solution.sln) file contained in the ClearlyVB2010

Chap11Average Solution folder. Open the Code Editor window and review the existing

code. Start and then test the application, using the testing information included in the

btnCalc control%u2019s Click event procedure. Notice that the code does not always produce

the expected result. Make the necessary changes to the code. (Hint: You can determine

whether a text box is empty by comparing its Text property to the String.Empty

constant.) Save the solution and then start and test the application again. Close the

Code Editor window and then close the solution.


Public Class frmMain


Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click

Me.Close()

End Sub


Private Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click

' calculates and displays the average of two test scores


' Test data Expected result

' no data entered message, 0.0

' test 1 is 50, test 2 is 0 25.0

' test 1 is 50, test 2 is 40 45.0

' test 1 is 50, test 2 is 100 75.0

' test 1 is 50, test 2 is 47 48.5

' test 1 is 0, test 2 is 50 25.0

' test 1 is 40, test 2 is 50 45.0

' test 1 is 100, test 2 is 50 75.0

' test 1 is 47, test 2 is 50 48.5

' test 1 is 95, test 2 is empty message, 0.0

' test 1 is empty, test 2 is 70 message, 0.0


Dim intTest1 As Integer

Dim intTest2 As Integer

Dim decAverage As Decimal


' assign test scores to variables

Integer.TryParse(txtTest1.Text, intTest1)

Integer.TryParse(txtTest2.Text, intTest2)


' calculate and display average

decAverage = intTest1 + intTest2 / 2

lblAverage.Text = decAverage.ToString("N1")

End Sub


Private Sub txtTest1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTest1.KeyPress

' allows the text box to accept only numbers

' and the Backspace key for editing


If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then

e.Handled = True

End If

End Sub


Private Sub txtTest2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTest2.KeyPress

' allows the text box to accept only numbers

' and the Backspace key for editing


If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then

e.Handled = True

End If

End Sub

End Class


I fixed the    decAverage = intTest1 + intTest2 / 2 line. I need help with the messagebox.show so that it shows the desired message as well as 0.0.


I tried something along the lines of :

  

Dim empty =

Me.Controls.OfType(Of TextBox)().Where(Function(txt) txt.Text.Length = 0)

If empty.Any Then

MessageBox.Show(String.Format("Please fill following textboxes: {0}",

String.Join(",", empty.Select(Function(txt) txt.Name))))

End If


However, when you click ok on the message box it continues to average the single input.


Some explination would be awesome too.

Thanks in advance.

Explanation / Answer

What you can do is put an "if" statement in your btnCalc_Click function to check if txtTest1.Text or txtTest2.Text are empty (Note I said OR and not AND, this is important). If either of the text boxes are empty then you will not compute for the average, you will simply output the number 0.0.


The reason why you use OR and not AND is because is because OR requires at least 1 statement to be true to run, whereas AND requires all statements to be true.


Here is the if statement


If txtTest1.Text = "" Or txtTest2.text = "" Then

'insert message box code here

' EXAMPLE

  MessageBox.Show(String.Format("Please fill following textboxes: {0}",

String.Join(",", empty.Select(Function(txt) txt.Name))))

Else

  ' calculate and display average

decAverage = intTest1 + intTest2 / 2

lblAverage.Text = decAverage.ToString("N1")

End If