Memory buttons in calculator
I'm making a basic calculator, so far I have programmed the addition, subtraction, multiplication and division without any problems. I want to add a memory, memory recall and memory clear button but I'm not sure how to go about them.
Here is the code I have for the other buttons so far.
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1, num2, ans
Try
num1 = Val(TextBox1.Text)
num2 = Val(TextBox2.Text)
ans = num1 + num2
MessageBox.Show(ans)
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim num1, num2, ans
Try
num1 = Val(TextBox1.Text)
num2 = Val(TextBox2.Text)
ans = num1 - num2
MessageBox.Show(ans)
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim num1, num2, ans
Try
num1 = Val(TextBox1.Text)
num2 = Val(TextBox2.Text)
ans = num1 * num2
MessageBox.Show(ans)
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim num1, num2, ans
Try
num1 = Val(TextBox1.Text)
num2 = Val(TextBox2.Text)
ans = num1 / num2
MessageBox.Show(ans)
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
End Sub
End Class
Could somebody give me the code for the memory buttons?
(Thanks)
Re: Memory buttons in calculator
Just think about what you're trying to do. What exactly do you want to happen when you click the memory button? You want the answer to be stored in the memory? If so then what would happen?
Re: Memory buttons in calculator
What would happen is the answer would be saved somehow. Sorry, my mind is a little blank.
Re: Memory buttons in calculator
So have a variable for memory. You should have all your variables declared as something specific. In your code snippet num1, num2 and ans don't have an as clause. You should set Option Strict On, then it wouldn't allow what you are doing there.
Code:
Option Strict On
Public Class Form1
Dim num1, num2, ans, memory As Integer ' or if not Integer then whatever you want. Double, Single, etc.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
You don't need to keep on declaring the variables for every different button. You can just declare them once at the top and then set the values of the variables within the different button click events.
Re: Memory buttons in calculator
Thanks for pointing that out, never noticed that.
I understand I need a variable for memory but I just don't have a clue on how to code it.
Re: Memory buttons in calculator
well i believe u need to store it in a file, is that what u want?
Re: Memory buttons in calculator
Is there anyway to store it without putting it in a file?
Re: Memory buttons in calculator
The data has to go somewhere, whether it is a file or database. Even the built in settings for Visual Studio (which is what I recommend for this) use an XML file in the Users folder. With that said, you should start here.
Re: Memory buttons in calculator
If the memory function needs to remember the textbox values until the next time your calculator is run then you will need to use a file/application setting. Otherwise you can use a Global Variable. Something like this should do the job:
VB Code:
Dim Memory As New List(Of Integer) 'Global Variable
Sub RestoreMemory() 'Use your button click events (these subs are just for the example)
If Memory.Count = 0 Then Exit Sub
textbox1.text = Memory(0)
textbox2.text = Memory(1)
End Sub
Sub ClearMemory()
Memory.Clear()
End Sub
Sub SaveMemory()
Memory.Add(textbox1.text)
Memory.Add(textbox2.text)
End Sub