ljvictorio
Member
- Joined
- Apr 26, 2013
- Messages
- 8
- Programming Experience
- Beginner
I have a simple calculator app. The user enters numbers into two separate text boxes. The user can press one of four buttons for each arithmetic operand. The result is displayed in the result label. If the user presses the Display button, the entire operation is displayed in a list box, which can store 10 math problems. This is my code, which works fine.
Public Class Calculator
What I want to do now is to include an Import and Export button on the form. The export button should save the 10 equations currently in the list box into an Access database file. Similarly, the Import button should bring the equations saved in the database file into the listbox. I've read up on using a DataGridView control, but I'm not sure this is my answer. Can I make this work using the current list box. Does anyone have any advice on what approach I should take? Any help would be greatly appreciated.
Public Class Calculator
VB.NET:
Dim strEquation As String
Dim myMathClass As New MathOp2
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
lblResult.Text = myMathClass.Add(CSng(txtFirstNumber.Text), CSng(txtSecondNumber.Text)).ToString
strEquation = myMathClass.EquationUsed
btnSave.Focus()
End Sub
Private Sub btnSubtract_Click(sender As Object, e As System.EventArgs) Handles btnSubtract.Click
lblResult.Text = myMathClass.Subtract(CSng(txtFirstNumber.Text), CSng(txtSecondNumber.Text)).ToString
strEquation = myMathClass.EquationUsed
btnSave.Focus()
End Sub
Private Sub btnDivide_Click(sender As Object, e As System.EventArgs) Handles btnDivide.Click
lblResult.Text = myMathClass.Divide(CSng(txtFirstNumber.Text), CSng(txtSecondNumber.Text)).ToString
strEquation = myMathClass.EquationUsed
btnSave.Focus()
End Sub
Private Sub btnMultiply_Click(sender As Object, e As System.EventArgs) Handles btnMultiply.Click
lblResult.Text = myMathClass.Multiply(CSng(txtFirstNumber.Text), CSng(txtSecondNumber.Text)).ToString
strEquation = myMathClass.EquationUsed
btnSave.Focus()
End Sub
Private Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
'The save button concatenates variables into the arithmetic operation
'Set the focus to Display
btnDisplay.Focus()
End Sub
Private Sub btnDisplay_Click(sender As Object, e As System.EventArgs) Handles btnDisplay.Click
lstResultsBox.Items.Add(myMathClass.EquationUsed)
txtFirstNumber.Focus()
If lstResultsBox.Items.Count > 10 Then
MessageBox.Show("You have saved 10 equations. Please clear the list to save additional problems.",
"Maximum number of saved problems", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub btnClear_Click(sender As Object, e As System.EventArgs) Handles btnClear.Click
'Clear all saved operations in the listbox
lstResultsBox.Items.Clear()
txtFirstNumber.SelectAll()
txtFirstNumber.Focus()
End Sub
Private Sub txtFirstNumber_GotFocus(sender As Object, e As System.EventArgs) Handles txtFirstNumber.GotFocus
'Select all text when this textbox receives focus
txtFirstNumber.SelectAll()
End Sub
Private Sub txtSecondNumber_GotFocus(sender As Object, e As System.EventArgs) Handles txtSecondNumber.GotFocus
'Select all text when this textbox receives focus
txtSecondNumber.SelectAll()
End Sub
Private Sub lstResultsBox_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles lstResultsBox.SelectedValueChanged
'Declare variables
Dim strSelected As String
Dim strSplitSelected() As String
'Use Split method to turn the selected result into an array and used the
'Array index to place the user selected number into the proper textbox.
strSelected = lstResultsBox.SelectedItem.ToString
strSplitSelected = strSelected.Split(" "c)
txtFirstNumber.Text = strSplitSelected(0)
txtSecondNumber.Text = strSplitSelected(2)
lblResult.Text = String.Empty
End Sub
End Class
What I want to do now is to include an Import and Export button on the form. The export button should save the 10 equations currently in the list box into an Access database file. Similarly, the Import button should bring the equations saved in the database file into the listbox. I've read up on using a DataGridView control, but I'm not sure this is my answer. Can I make this work using the current list box. Does anyone have any advice on what approach I should take? Any help would be greatly appreciated.