10.13 ( Arithmetic Calculator App) Write an app that allows the user to enter a
ID: 3549130 • Letter: 1
Question
10.13 ( Arithmetic Calculator App) Write an app that allows the user to enter a series of numbers and manipulate them. The app should provide users with the option of adding or multiplying the numbers. Users should enter each number in a TextBox. After entering the number, the user clicks a Button, and the number is inserted in a ListBox. The GUI should behave as in Fig. 10.20.
a) Copying the template to your working directory. Copy the directory C: Examples ch10 Exercises ArithmeticCalculator to your C: SimplyVB2010 directory.
b) Opening the apps template file. Double click ArithmeticCalculator. sln in the ArithmeticCalculator directory to open the app.
c) Adding a ListBox to display the entered numbers. Add a ListBox. Place and size it as in Fig. 10.20.
d) Creating an event handler for the Enter Button. Create the Click event handler for the Enter Button. If the result of a previous calculation is displayed, this event han-dler should clear the result, clear the ListBox and disable the addition and multipli-cation Buttons. It should then insert the current number in the Operands list: ListBox. When the ListBox contains at least two numbers, the event handler should then enable the addition and multiplication Buttons.
e) Summing the values in the ListBox. Define the Click event handler for the Add Button. This event handler should compute the sum of all the values in the Oper-ands list: ListBox and display the result in resultLabel.
f) Multiplying the values in the ListBox. Define the Click event handler for the Mul-tiply Button. This event handler should compute the product of all the values in the Operands list: ListBox and display the result in resultLabel.
g) Running the app. Select Debug > Start Debugging to run your app. Enter two val-ues, then click the Add and Multiply Buttons. Verify that the results displayed are correct. Also, make sure that the Add and Multiply Buttons are not enabled until two values have been entered. Enter a new value and verify that the previous result and the ListBox are cleared. Enter two more values, then click the Add and Multiply Buttons. Verify that the results displayed are correct.
h) Closing the app. Close your running app by clicking its close box.
i) Closing the IDE. Close the Visual Basic IDE by clicking its close box.
Explanation / Answer
vb.net Code:- Public Class ArithmeticCalculator
- Private Property operands As Integer
- Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click
- Dim operand As Integer
- If enterOperandTextBox.Text <> String.Empty Then
- OperandsListBox1.Items.Add(enterOperandTextBox.Text)
- OperandsListBox1.ClearSelected()
- enterOperandTextBox.Clear()
- End If
- operand = 0
- If OperandsListBox1.Items.Count = 0 Then
- enterButton.Enabled = True
- addButton.Enabled = False
- multiplyButton.Enabled = False
- End If
- End Sub
- Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
- Dim result As Integer
- Dim operandCounter As Integer
- result = 0
- operandCounter = 0
- Do While operandCounter < OperandsListBox1.Items.Count
- result = OperandsListBox1.Items(operands)
- result += operands
- operandCounter += 1
- Loop
- If operandCounter <> 0 Then result = result + OperandsListBox1.Items.Count
- resultLabel.Text = result
- End Sub
- Private Sub multiplyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles multiplyButton.Click
- Dim result As Integer
- Dim operandCounter As Integer
- result = 0
- operandCounter = 0
- Do While operandCounter < OperandsListBox1.Items.Count
- result = OperandsListBox1.Items(operands)
- result += operands
- operandCounter += 1
- Loop
- If operandCounter <> 0 Then result = result * OperandsListBox1.Items.Count
- resultLabel.Text = result
- End Sub
- End Class
- Public Class ArithmeticCalculator
- Private Property operands As Integer
- Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click
- Dim operand As Integer
- If enterOperandTextBox.Text <> String.Empty Then
- OperandsListBox1.Items.Add(enterOperandTextBox.Text)
- OperandsListBox1.ClearSelected()
- enterOperandTextBox.Clear()
- End If
- operand = 0
- If OperandsListBox1.Items.Count = 0 Then
- enterButton.Enabled = True
- addButton.Enabled = False
- multiplyButton.Enabled = False
- End If
- End Sub
- Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
- Dim result As Integer
- Dim operandCounter As Integer
- result = 0
- operandCounter = 0
- Do While operandCounter < OperandsListBox1.Items.Count
- result = OperandsListBox1.Items(operands)
- result += operands
- operandCounter += 1
- Loop
- If operandCounter <> 0 Then result = result + OperandsListBox1.Items.Count
- resultLabel.Text = result
- End Sub
- Private Sub multiplyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles multiplyButton.Click
- Dim result As Integer
- Dim operandCounter As Integer
- result = 0
- operandCounter = 0
- Do While operandCounter < OperandsListBox1.Items.Count
- result = OperandsListBox1.Items(operands)
- result += operands
- operandCounter += 1
- Loop
- If operandCounter <> 0 Then result = result * OperandsListBox1.Items.Count
- resultLabel.Text = result
- End Sub
- End Class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.