PDA

View Full Version : VB calc code

jmoney3457
11-02-2007, 05:04 AM
trying to do VBsimple calculator codes ..i got all the visible aspect done just need the code...can anyone give me calculator code?

Roelf
11-02-2007, 10:26 AM
you mean you dont know how to subtract, add, multiply and divide in code?

jmoney3457
11-02-2007, 12:35 PM
that & pretty much everything else to do w/ the code like allowing more then 1 digit in the input (text) box to make calculation i.e being able to add multiple digit #s not just single

jmoney3457
11-02-2007, 09:17 PM
anyone?

sage45
11-02-2007, 09:49 PM
One nice thing about programming is that the number of digits in your entries would not matter...

I.E.

If I wanted to add two variables I would simply use

C = A + B Where C becomes the result of A and B

So for a calculator using a text box, you would have to use essentially 4 - 5 steps.

1. Capture the first entry

numVarA = InputBox1.Text

2. Capture the function (addition, subtraction, multiplcation and division)

funcVarOperation = whichever button is pushed (so if I pushed the addition button the funcVarOperation would store that I am going to do addition)
InputBox1.Text = ""

3. Capture the second entry

numVarB = InputBox1.Text

4. If equals is pushed (Run the function specified by funcVarOperation and print the results) else if another function is chosen (do the first function, clear one of your variables and continue with step 2)

If equals is pushed
varResults = funcVarOperation(numVarA,numVarB)
InputBox1.Text = varResults
else
numVarA = funVarOperation(numVarA,numVarB)
numVarB = ""
End If
funcVarOperation = whichever button is pushed (so if I pushed the addition button the funcVarOperation would store that I am going to do addition)
InputBox1.Text = ""

HTH,

-saige-

jmoney3457
11-02-2007, 10:35 PM
thanks for info sage..only thing is whats code for the operators (+,-,/,*)?
1 other thing..could someone provide couple of examples of if..else statements that could be utilized in a calc

sage45
11-03-2007, 12:20 AM
Well...

C = A + B
C = A - B
C = A * B
C = A / B or A \ B

They are what you think they are...

http://www.netcoole.com/VB6TOCS/vbhtml/vbopers.htm

-saige-

jmoney3457
11-03-2007, 02:16 AM
the #s (digits) & the operators on the form part would be clickable labels correct? also can someone write an example of an if-then-else statement for a calc? lastly, what is Converting variable types in VB mean?

Rappa
11-03-2007, 02:46 AM
No, lables are usually for text, you can have a button for each digit, or have a textbox for one value, and one for the other, then the user clicks the appropriate mathmatical operator, and you could display the result in a label.

But with such a simple question, you should try google. If your just starting out, maybe try some VB tutorials? MSDN has like 30 free video tutorials that you could watch to keep you busy.

as for converting variable types, a textbox by default is a string so in order to take 2 textboxes with numeric inputs and do somthing to them, youd need to parse the text box into an integer first.

for example in C#: int.Parse(textbox1.Text);

jmoney3457
11-03-2007, 03:20 AM
No, lables are usually for text, you can have a button for each digit, or have a textbox for one value, and one for the other, then the user clicks the appropriate mathmatical operator, and you could display the result in a label.

But with such a simple question, you should try google. If your just starting out, maybe try some VB tutorials? MSDN has like 30 free video tutorials that you could watch to keep you busy.

as for converting variable types, a textbox by default is a string so in order to take 2 textboxes with numeric inputs and do somthing to them, youd need to parse the text box into an integer first.

for example in C#: int.Parse(textbox1.Text);
how do you parse the text box into integer 1st so the 2 txtboxes w/ num inputs can do something w/ them in VB since your example was for C#? also what could someone write out what a if then else statement in calculator would look like?

Rappa
11-03-2007, 04:15 AM
i wont write the code, but ill give you some pseudocode:

If condition Then
[statements]
Else If condition Then
[statements]
-
-
Else
[statements]
End If

or you can use a case statement, whos syntax is as such:

Select [ Case ] testexpression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select

heres sample code:

Option Explicit

End Sub

Private Sub cmdCalculate_Click()
If cmbOperator.Text = "" Then
MsgBox ("You most chose a operator!!")
Else
Select Case cmbOperator.Text
Case "+"

Case "-"

Case "*"

Case "/"

End Select
lblResult.Caption = Val(txtNumber1.Text) + Val(txtNumber2.Text)
lblResult.Caption = Val(txtNumber1.Text) - Val(txtNumber2.Text)
lblResult.Caption = Val(txtNumber1.Text) * Val(txtNumber2.Text)
lblResult.Caption = Val(txtNumber1.Text) / Val(txtNumber2.Text)
End If
End Sub

jmoney3457
11-03-2007, 04:26 AM
thx rappa really appreciate it bro..just 1 last thing..how do you parse the 2 text boxes into integer? and when you say that what do you exactly mean?

jmoney3457
11-03-2007, 05:43 PM
anyone?

Rappa
11-03-2007, 06:04 PM
if you look at my code, i used the Val() function, which converts a string to an integer. Its not the best practice to use, but i assume your not dealing with percents so it souldnt matter. So Val() is a crude equal to C#'s int.Parse() function.

sage45
11-03-2007, 08:20 PM
In other words:

Integers are whole numbers both positive and negative - i.e. {-34, -33, 5, 0, 5, 33, 34}

If you wanted to use decimals you would use a float or long.

-saige-

jmoney3457
11-04-2007, 02:42 AM
oh ok is C# in VB or something totally different/seperate?

Rappa
11-04-2007, 03:18 AM
huh? C# and VB are totally different languages. Visual Basic is a very simple language compared to C#. So you should start with VB first

jmoney3457
11-04-2007, 04:57 AM
when i do my form..how do i make the digits (buttons) display the correct digit in the output (textbox) i.e push 1 ..1 shows up in textbox?

sage45
11-04-2007, 04:59 AM
Rappa how do you figure that Visual Basic is very simple compared to C#???

Are you saying in verbage or in capability?

-saige-

Rappa
11-04-2007, 05:12 AM
Im just saying visual basic is based off more easy going complexes, C# is harder to learn because it has more variable types ect, but i was tought in college that VB is much simpler than C#

youd have to do it on the click event i suppose

button_1 click
{
textbox1.Text += 1
}

jmoney3457
11-04-2007, 05:24 AM
Im just saying visual basic is based off more easy going complexes, C# is harder to learn because it has more variable types ect, but i was tought in college that VB is much simpler than C#

youd have to do it on the click event i suppose

button_1 click
{
textbox1.Text += 1
}
oh ok so for 2 it'd be button_2 click {textbox1.text +=2}?

Rappa
11-04-2007, 05:30 AM
correct, im sure theres an easier way but i cant think of it, unless you use a case/break statement. but i wont go into that.

but after your user enters numbers, youd have to have the onclick for the assignment operator create an integer out of the textbox value and store it and clear the textbox, then the user can enter new numbers for the old number to be addes/sub ect to, then when the user click equals, it makes a number out of the new value. and calculates them and outputs it to the textbox.

jmoney3457
11-04-2007, 05:58 AM
but after your user enters numbers, youd have to have the onclick for the assignment operator create an integer out of the textbox value and store it and clear the textbox, then the user can enter new numbers for the old number to be addes/sub ect to, then when the user click equals, it makes a number out of the new value. and calculates them and outputs it to the textbox.

how do i do that? can you provide example code

jmoney3457
11-04-2007, 08:01 AM
edit*

Rappa
11-04-2007, 05:38 PM
ok so say your user enters a 4 then hits * then hits a 5 then hits equals:

btn4_click
textbox.Text += 4
}
multiply_click
string firstNumber = Val(textbox.Text);
textbox.Text = "";
}
btn5_click
textbox.Text += 5;
}
equals_click
string secondNumber = Val(textbox.Text);
textbox.Text = firstNumber * secondNumber;
}

believe me thats a very crude example and i dont know much about vb, but that should hopefully give you a general idea.

jmoney3457
11-07-2007, 03:26 AM
here is my code:Public Class Form1
Dim first As Double
Dim second As Double
Dim sign As String

Private Sub btnzero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnzero.Click

txtdisplay.Text = btnzero.Text
'this allows for 0 to enter textbox when clicked
End Sub

Private Sub btnone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnone.Click
txtdisplay.Text = btnone.Text
'this allows for 1 to enter textbox when clicked
End Sub

Private Sub btntwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntwo.Click
txtdisplay.Text = btntwo.Text
'this allows for 2 to enter textbox when clicked
End Sub

Private Sub btnthree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthree.Click
txtdisplay.Text = btnthree.Text
'this allows for 3 to enter textbox when clicked
End Sub

Private Sub btnfour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfour.Click
txtdisplay.Text = btnfour.Text
'this allows for 4 to enter textbox when clicked
End Sub

Private Sub btnfive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfive.Click
txtdisplay.Text = btnfive.Text
'this allows for 5 to enter textbox when clicked
End Sub

Private Sub btnsix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsix.Click
txtdisplay.Text = btnsix.Text
'this allows for 6 to enter textbox when clicked
End Sub

Private Sub btnseven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnseven.Click
txtdisplay.Text = btnseven.Text
'this allows for 7 to enter textbox when clicked
End Sub

Private Sub btneight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneight.Click
txtdisplay.Text = btneight.Text
'this allows for 8 to enter textbox when clicked
End Sub

Private Sub btnnine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnine.Click
txtdisplay.Text = btnnine.Text
'this allows for 9 to enter textbox when clicked
End Sub

Private Sub cmdclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclear.Click
txtdisplay.Text = ""
End Sub
End Class

so far i've gotten it so when you press the digits 0-9 they will display in the textbox (txtdisplay) but now how do i get the operators (commands) to function and get the = command to display the answer in the textbox? also i need some if..else statements where would i put those in the code/how would i use them?

jmoney3457
11-08-2007, 02:01 AM
anyone:confused:

jmoney3457
11-09-2007, 03:23 AM
update: i've decided to leave equal button for display purposes only so now how would i get it to display answer in the textbox after pressing a digit-the operator then another digit i.e 2 + 2 then displays 4 in textbox & also i figured out if..else so all set on that

jmoney3457
11-09-2007, 06:10 AM
all set:thumbsup: thx to rappa:) much thanks to him:D