problem with calculator code in vb.6

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4148 Views - Last Post: 19 October 2007 - 11:17 PM Rate Topic: ***** 1 Votes

#1 sendrof  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 07

problem with calculator code in vb.6

Posted 14 October 2007 - 08:17 PM

Hello

I am working on making a calculator in vb.6

the issue is:

i am using three text boxes one for for the first number, another for the second number, last one for the result
so if type "1" in the first txt, and "1" in thew second txt, then clicking a cmdPlus (+), the result "2" will appear in the third txt.

the problem is that i wanna use just one textbox so that if u type "1" then u click the command (+), number 1 will be saved and the textbox will be clear again for putting another number, then u put another number and then press command (=) and result will appear in the same textbox.

so my main question is how to use one textbox for putting the first number and the second number and the result instead of 3 textboxes.

my code with the 3 textboxes is something like this:

Private Sub txt1_Change()		  'here u put first number
End Sub 

Private Sub txt2_Change()		   'here u put second number
End Sub

Private Sub txtAnswer_Change()  ' here result will appear if u click on (+) command
End Sub

Private Sub cmdPlus_Click()
txtAnswer.Text = Val(txt1.Text) + Val(txt2.Text)
End Sub

'notice that i can only type the number from the keyboard 
'because if i put a command button for number 1 for example,
' i can make it only appear in one text box but not the other
' some thing like this:

Private Sub cmd1_Click()
	If txt1.Text = "" Then
								 txt1.Text = 1
	ElseIf txt1.Text <> "" Then
								  txt1.Text = txt1.Text & 1
	End If

'number one will appear only in txt1 and i dont know
'how to make it appear in txt2 once u click inside it
' thats why i wanna use just one text box for
'putting numbers and getting results




hopefully someone can help me with this
1. how to make cmd1 represent a number 1 in txt1 OR txt2 depends on which txt i click inside?
2. how to use only one textbox for putting numbers and getting results instead of 3?

Is This A Good Question/Topic? 0
  • +

Replies To: problem with calculator code in vb.6

#2 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: problem with calculator code in vb.6

Posted 14 October 2007 - 08:36 PM

Even though this is a VB.NET tutorial, PsychoCoder's tutorial should help you out some.

Basic Calculator in VB.NET
Was This Post Helpful? 0
  • +
  • -

#3 sendrof  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 07

Re: problem with calculator code in vb.6

Posted 14 October 2007 - 11:15 PM

Thanks for the post

i already went through that some time ago, and it didnt really help me though.

I really hope if someone can specifically answer my two questions or one of them at least.

Thanks for the reply anyway.
Was This Post Helpful? 0
  • +
  • -

#4 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: problem with calculator code in vb.6

Posted 14 October 2007 - 11:20 PM

Quote

2. how to use only one textbox for putting numbers and getting results instead of 3?

You could use a variable to check if you have used an operator or not. If not, stay on the first number otherwise move to (I was about to type goto :crazy:) the second number. Use two strings to represent your number. So:
Number1 = Number1 & "<digit>"


Do likewise for number2. Then use the Val() function to get the number equivalent.
Hope this helps :).
Was This Post Helpful? 0
  • +
  • -

#5 sendrof  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 07

Re: problem with calculator code in vb.6

Posted 15 October 2007 - 02:26 AM

Thank u for ur reply.

it really helped me, but it will more if u give an example.

lets say i have a textbox and 3 command buttons
cmd.Caption = 1
cmd.Caption = +
cmd.Caption = =

what confuses me is which code should be in which cmd_Click?

Can u plz explain what u said before by typing a simple code using those 3 command buttons and a textbox?

Thanks agian.
Was This Post Helpful? 0
  • +
  • -

#6 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: problem with calculator code in vb.6

Posted 15 October 2007 - 02:33 AM

Before I answer your question, do you intend to add only two numbers or more than that in one go?
Was This Post Helpful? 0
  • +
  • -

#7 sendrof  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 07

Re: problem with calculator code in vb.6

Posted 15 October 2007 - 02:39 AM

View PostLouisda16th, on 15 Oct, 2007 - 02:33 AM, said:

Before I answer your question, do you intend to add only two numbers or more than that in one go?


just two... thanks
Was This Post Helpful? 0
  • +
  • -

#8 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: problem with calculator code in vb.6

Posted 15 October 2007 - 03:17 AM

Sorry for the late reply. There was a power cut (happens here often :(). Basically do this. Declare 4 variables: 1 boolean, two strings and one integer(or double if you want to handle fractions as well.). Use the boolean variable to check whether "+" has been clicked. The strings hold your input numbers while the last one holds your answer (you could alternatively choose to put the result directly into the textbox). Now in your button which represents 1, use:
num1 = num1+"1"


when the plus hasn't been clicked and,
num2 = num2+"1"


The code in the button for "+" should change the value of the boolean variable.
You will require an if statement to take care of the two cases. That quite finishes it. Use the Val function to get the values stored in each string variable (convert string to number) and add them to get your result.
Hope this helps :).
Was This Post Helpful? 0
  • +
  • -

#9 sendrof  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 07

Re: problem with calculator code in vb.6

Posted 15 October 2007 - 04:28 AM

Its working :)

really appreciate ur replies :)

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#10 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: problem with calculator code in vb.6

Posted 15 October 2007 - 05:03 AM

Glad I could help. :)
Was This Post Helpful? 0
  • +
  • -

#11 sendrof  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 07

Re: problem with calculator code in vb.6

Posted 19 October 2007 - 10:39 AM

View PostLouisda16th, on 15 Oct, 2007 - 02:33 AM, said:

Before I answer your question, do you intend to add only two numbers or more than that in one go?


Now that i can add two numbers, Can u help with adding more than two numers in one go?
Was This Post Helpful? 0
  • +
  • -

#12 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: problem with calculator code in vb.6

Posted 19 October 2007 - 10:43 AM

You can use a stack. Use one stack to store numbers and another to store operators. Pop two numbers and an operator. Now perform your operation. Now pop one number and one operator and then perform operations on this number with your previous answer.
This method works for all arithmetic operators : +, -, *, /
Hope this helps :).

This post has been edited by Louisda16th: 19 October 2007 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#13 sendrof  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 07

Re: problem with calculator code in vb.6

Posted 19 October 2007 - 11:30 AM

View PostLouisda16th, on 19 Oct, 2007 - 10:43 AM, said:

You can use a stack. Use one stack to store numbers and another to store operators. Pop two numbers and an operator. Now perform your operation. Now pop one number and one operator and then perform operations on this number with your previous answer.
This method works for all arithmetic operators : +, -, *, /
Hope this helps :).


Whats a stack?
Can u demonstrate more about this method?
A code example will be best
Thnaks in advance.
Was This Post Helpful? 0
  • +
  • -

#14 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: problem with calculator code in vb.6

Posted 19 October 2007 - 11:34 AM

Its a storage concept. Read Here.
As far as code is concerned, you'll need arrays to hold the numbers you need.
Was This Post Helpful? 0
  • +
  • -

#15 sendrof  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 07

Re: problem with calculator code in vb.6

Posted 19 October 2007 - 06:19 PM

View PostLouisda16th, on 19 Oct, 2007 - 11:34 AM, said:

Its a storage concept. Read Here.


I still dont knoe how to do it.

View PostLouisda16th, on 19 Oct, 2007 - 11:34 AM, said:

As far as code is concerned, you'll need arrays to hold the numbers you need.


Please, dont choose the shortest way to explain with the shortest number of words as if i was an expert, because I've started learning vb just from 2 weeks and every thing is totally new to me including the term Stack.
I know arrays though. :rolleyes:

SO, could u plz explain with a real code so i can understand more?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2