Program using visual basic.net You will create a very simple two numbers calcula
ID: 3885204 • Letter: P
Question
Program using visual basic.net
You will create a very simple two numbers calculator with save options; here is the specifications for the application. Create a form divided vertically in two halves with right and left panels.
1- In the left panel, you will create the following controls, the labels or the value of the control will be in "" and the type of the control will in []
a- "First Number" [textbox1]
b- "Second number" [textbox2]
c- "Result" [textbox3] - read only
d- "+" [push button] "-" [push button] "/" [push button] "*" [push button] "Save" [push button] "Display" [push button]
2- Operations. The user will enter two numbers in the "First Number" and the "Second Number", then press the "+" for addition, "-" for subtraction, "*" multiplication and "/" the result will be displayed in the result [textbox3], if the user clicked "[save]" then the operation can be displayed in the right panel when the user press [Display]
Please note: that the user can save up to 10 operations. Add "clear" [push button] on the right panel to clear the content, make sure that you trap all the errors and set the focus to the appropriate control after the error occurred
. Make sure that the result contains the operations, I should see in the right panel something like this.
1+1=2
1*2=2 and so on.
Instructions: You must trap divided by Zero and a display a message to the user “Cannot Divide by Zero”, allow the user to renter another value.
The main issue is figuring out how to limit the saved items added to the file to 10
You have some aswer in your website but are not working.
Explanation / Answer
Public Class Form1
Dim decNum1 As Decimal
Dim decNum2 As Decimal
Dim decNum3 As Decimal
Dim count As Integer = 0
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Decimal.TryParse(txtNum1.Text, decNum1)
Decimal.TryParse(txtNum2.Text, decNum2)
' add num 1 and 2 and display results
decNum3 = decNum1 + decNum2
txtNum3.Text = decNum1 & " + " & decNum2 & " = " & decNum3
End Sub
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
' accept input from text box 1 and 2
Decimal.TryParse(txtNum1.Text, decNum1)
Decimal.TryParse(txtNum2.Text, decNum2)
' subtract num 2 from num 1 and display results
decNum3 = decNum1 - decNum2
txtNum3.Text = decNum1 & " - " & decNum2 & " = " & decNum3
End Sub
Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
' accept input from text box 1 and 2
Decimal.TryParse(txtNum1.Text, decNum1)
Decimal.TryParse(txtNum2.Text, decNum2)
' multiply num 1 and 2 and display results
decNum3 = decNum1 * decNum2
txtNum3.Text = decNum1 & " X " & decNum2 & " = " & decNum3
End Sub
Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
' accept input from text box 1 and 2
Decimal.TryParse(txtNum1.Text, decNum1)
Decimal.TryParse(txtNum2.Text, decNum2)
' divide num 1 by num 2 and display results
Try
decNum3 = decNum1 / decNum2
Catch ex As DivideByZeroException When decNum2 = 0
MessageBox.Show("Cannot divide by 0. Please enter a valid number.")
txtNum2.Focus()
End Try
txtNum3.Text = decNum1 & " / " & decNum2 & " = " & decNum3
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
' clears list box
lstOperations.Items.Clear()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If (count < 10) Then
' writes up to 10 operations to sequential access files
Dim outFile As IO.StreamWriter
outFile = IO.File.AppendText("operations.txt")
outFile.WriteLine(txtNum3.Text)
outFile.Close()
count = count + 1
End If
End Sub
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
' reads operations from a sequential access file and displays them in list box
' declare variables
Dim inFile As IO.StreamReader
Dim strOperation As String
' clears previous operations from list box
lstOperations.Items.Clear()
' determine wether file exists
If IO.File.Exists("operations.txt") Then
' open the file for input
inFile = IO.File.OpenText("operations.txt")
' process loop instructions until end of file
Do Until inFile.Peek = -1
' read operation
strOperation = inFile.ReadLine
' add operation to list box
lstOperations.Items.Add(strOperation)
Loop
' close the file
inFile.Close()
Else
MessageBox.Show("No savefile exists to display.")
End If
End Sub
Private Sub input_validation(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtNum1.KeyPress, txtNum2.KeyPress
' validate input is a number 0-9
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
End Class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.