|
-
Apr 27th, 2011, 10:11 PM
#1
Thread Starter
Member
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)
-
Apr 27th, 2011, 10:18 PM
#2
Fanatic Member
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?
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Apr 27th, 2011, 10:25 PM
#3
Thread Starter
Member
Re: Memory buttons in calculator
What would happen is the answer would be saved somehow. Sorry, my mind is a little blank.
-
Apr 28th, 2011, 12:04 AM
#4
Fanatic Member
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.
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Apr 28th, 2011, 12:14 AM
#5
Thread Starter
Member
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.
-
Apr 28th, 2011, 01:48 AM
#6
Hyperactive Member
Re: Memory buttons in calculator
well i believe u need to store it in a file, is that what u want?
-
Apr 28th, 2011, 03:19 PM
#7
Thread Starter
Member
Re: Memory buttons in calculator
Is there anyway to store it without putting it in a file?
-
Apr 28th, 2011, 03:22 PM
#8
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.
-
Apr 28th, 2011, 03:56 PM
#9
Hyperactive Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|