|
-
Dec 14th, 2009, 12:07 AM
#1
Thread Starter
New Member
(help) basic calculator in vb 2008
Hello. I am new to visual basic 2008, and i started with simple project-calculator. I made it to the point where "+" sign works, and "=" sign execute the command of "+" sign. Now i try to make "-" sign work: the problem is, idk how to bind it with "=". Here is the code:
Code:
Public Class Form1
Dim total1 As Integer
Dim total2 As Integer
Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
txtDisplay.Text = txtDisplay.Text & btnOne.Text
End Sub
Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
txtDisplay.Text = txtDisplay.Text & btnTwo.Text
End Sub
Private Sub btnThree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThree.Click
txtDisplay.Text = txtDisplay.Text & btnThree.Text
End Sub
Private Sub btnFour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFour.Click
txtDisplay.Text = txtDisplay.Text & btnFour.Text
End Sub
Private Sub btnFive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFive.Click
txtDisplay.Text = txtDisplay.Text & btnFive.Text
End Sub
Private Sub btnSix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSix.Click
txtDisplay.Text = txtDisplay.Text & btnSix.Text
End Sub
Private Sub btnSeven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeven.Click
txtDisplay.Text = txtDisplay.Text & btnSeven.Text
End Sub
Private Sub btnEight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEight.Click
txtDisplay.Text = txtDisplay.Text & btnEight.Text
End Sub
Private Sub btnNine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNine.Click
txtDisplay.Text = txtDisplay.Text & btnNine.Text
End Sub
Private Sub btnZero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZero.Click
txtDisplay.Text = txtDisplay.Text & btnZero.Text
End Sub
Private Sub cmdPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlus.Click
total1 = total1 + Val(txtDisplay.Text)
txtDisplay.Clear()
End Sub
Private Sub cmdEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEquals.Click
total2 = total1 + Val(txtDisplay.Text)
txtDisplay.Text = total2
total1 = 0
End Sub
Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
txtDisplay.Clear()
End Sub
Private Sub cmdMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMinus.Click
total1 = total1 - Val(txtDisplay.Text)
txtDisplay.Clear()
End Sub
End Class
Please help. Thank you.
-
Dec 14th, 2009, 12:18 AM
#2
Re: (help) basic calculator in vb 2008
Well, you need to draw out your program first on paper. Plan out what the program is actually going to do.
You say you're going to make a calculator, that's fine and dandy. Saying and Doing are two very different things.
Whenever you press a button, you need to have that value be either tacked into a textbox, or into a global variable right, depending on if you did or did not press the +, -, /, or * key.
If you weren't a beginner, I would explain some more processes on how to differentiate on this, but for simplicity's sake, let's do this:
1. Get a piece of paper.
2. Write out what you want the program to ultimately accomplish, the goal of it.
3. Think of the process the program must follow to accomplish this goal.
4. Slowly write out some form of Psuedo-Code
5. Once you have done ALL of the above steps, then you can start using Google (or us) to answer specific questions about your program.
Your question for 'binding' the = key to the - key just doesn't make any sense. The reason you need to do those 5 steps is to build your logical thinking skills. They are so important in programming that all schools require you to take at least Calculus Level Math to develop your logical thinking skills.
Once you've done those steps, come back and ask a few more questions.
-
Dec 14th, 2009, 12:28 AM
#3
Thread Starter
New Member
Re: (help) basic calculator in vb 2008
 Originally Posted by formlesstree4
Well, you need to draw out your program first on paper. Plan out what the program is actually going to do.
You say you're going to make a calculator, that's fine and dandy. Saying and Doing are two very different things.
Whenever you press a button, you need to have that value be either tacked into a textbox, or into a global variable right, depending on if you did or did not press the +, -, /, or * key.
If you weren't a beginner, I would explain some more processes on how to differentiate on this, but for simplicity's sake, let's do this:
1. Get a piece of paper.
2. Write out what you want the program to ultimately accomplish, the goal of it.
3. Think of the process the program must follow to accomplish this goal.
4. Slowly write out some form of Psuedo-Code
5. Once you have done ALL of the above steps, then you can start using Google (or us) to answer specific questions about your program.
Your question for 'binding' the = key to the - key just doesn't make any sense. The reason you need to do those 5 steps is to build your logical thinking skills. They are so important in programming that all schools require you to take at least Calculus Level Math to develop your logical thinking skills.
Once you've done those steps, come back and ask a few more questions.
My logical thinking skills are above average, i am 10th grader, taking calculus AP. Question is:
how can i use "if" (conditional) statement in this code:
Code:
Private Sub cmdEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEquals.Click
total2 = total1 + Val(txtDisplay.Text)
txtDisplay.Text = total2
total1 = 0
? And do i need to add another value like:
Code:
Dim total3 As Integer
and use it as a "-" button:
Code:
Private Sub cmdMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMinus.Click
total3 = total3 - Val(txtDisplay.Text)
txtDisplay.Clear()
End Sub
?
Thank you for replying.
-
Dec 14th, 2009, 06:32 PM
#4
Re: (help) basic calculator in vb 2008
Very well, I'm an 11th grader taking AP Calculus as well. If your logic skills were more developed you could see that you can use the button clicks to set a global variable. When you press the 'equal' symbol, you can use a Select Case on that Global variable.
Like I said, write this out on the piece of paper so you know what you're doing.
-
Dec 14th, 2009, 06:54 PM
#5
Fanatic Member
Re: (help) basic calculator in vb 2008
best way to do globals is in a module, i use something like this
vb Code:
Module modGlobals Friend SomeString As String Friend AnotherString As String Friend SomeDouble As Double Friend content As String = Form1.TextBox1.Text End Module
add this at the very bottom of the code (under End Class). Also note my globals always start with a Caps Letter, helps me to tell a global from a non global instantly.
@formlesstree4 absolutely right, never underestimate the use of pen and paper.
If debugging is the process of removing bugs, then programming must be the process of putting them in.
-
Dec 14th, 2009, 07:01 PM
#6
Re: (help) basic calculator in vb 2008
You may want to look at this
Parsing Infix Notation
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
|