5 Replies - 14587 Views - Last Post: 22 February 2011 - 01:59 PM Rate Topic: -----

#1 WWW.COE_89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-August 09

vb calculator

Posted 22 February 2011 - 07:57 AM

please help me on how to program a calculator in visual basic in which for example; I ENTER a value 2*2-2+2 in a textbox upon entering the equal sign it will display the value 4 on the same textbox.. for another example I ENTER a value 2**2--2+2 in a textbox upon entering the equal sign it diplays a msgbox " error".it is a casio type calculator that performs basic arithmetic multiply,divide,add,and subtract only..im done with the gui and entering inputs but i dont know how to code the operations..thank and regards

Attached File(s)

  • Attached File  code.txt (1.96K)
    Number of downloads: 1840


Is This A Good Question/Topic? 0
  • +

Replies To: vb calculator

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,587
  • Joined: 27-December 08

Re: vb calculator

Posted 22 February 2011 - 07:59 AM

Post your code in the body of the thread, USING CODE TAGS!!!

Like so: :code:.
Was This Post Helpful? 1
  • +
  • -

#3 WWW.COE_89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-August 09

Re: vb calculator

Posted 22 February 2011 - 08:00 AM

View PostWWW.COE_89, on 22 February 2011 - 07:57 AM, said:

please help me on how to program a calculator in visual basic in which for example; I ENTER a value 2*2-2+2 in a textbox upon entering the equal sign it will display the value 4 on the same textbox.. for another example I ENTER a value 2**2--2+2 in a textbox upon entering the equal sign it diplays a msgbox " error".it is a casio type calculator that performs basic arithmetic multiply,divide,add,and subtract only..im done with the gui and entering inputs but i dont know how to code the operations..thank and regards

1	'Declare the global variables to be used throughout the form
2	Dim mfirst As Single
3	Dim msecond As Single
4	Dim manswer As Single


01	Private Sub cmd0_Click()
02	'Put the value 0 into the text box
03	Text1.text = Text1.text + "0"
04	End Sub
05	 
06	Private Sub cmd1_Click()
07	'Put the value 1 into the text box
08	Text1.text = Text1.text + "1"
09	End Sub
10	 
11	Private Sub cmd2_Click()
12	'Put the value 2 into the text box
13	Text1.text = Text1.text + "2"
14	End Sub
15	 
16	Private Sub cmd3_Click()
17	'Put the value 3 into the text box
18	Text1.text = Text1.text + "3"
19	End Sub
20	 
21	Private Sub cmd4_Click()
22	'Put the value 4 into the text box
23	Text1.text = Text1.text + "4"
24	End Sub
25	 
26	Private Sub cmd5_Click()
27	'Put the value 5 into the text box
28	Text1.text = Text1.text + "5"
29	End Sub
30	 
31	Private Sub cmd6_Click()
32	'Put the value 6 into the text box
33	Text1.text = Text1.text + "6"
34	End Sub
35	 
36	Private Sub cmd7_Click()
37	'Put the value 7 into the text box
38	Text1.text = Text1.text + "7"
39	End Sub
40	 
41	Private Sub cmd8_Click()
42	'Put the value 8 into the text box
43	Text1.text = Text1.text + "8"
44	End Sub
45	 
46	Private Sub cmd9_Click()
47	'Put the value 9 into the text box
48	Text1.text = Text1.text + "9"
49	End Sub
	
	
1	Private Sub cmdADD_Click()
Text1.text=text.text + +
End sub

Subtract:
1	Private Sub cmdSUBTRACT_Click()
Text1.text=text.text + -
End sub

Multiply:
1	Private Sub cmdMULTIPLY_Click()
Text1.text=text.text + x
End sub

Divide:
1	Private Sub cmdDIVIDE_Click()
Text1.text=text.text + /

Equals:
01	Private Sub cmdEQUALS_Click()
02	msecond = Val(txtNUMBER)
03	 
04	Select Case mbutton
05	Case Is = 1
06	manswer = mfirst + msecond
07	Case Is = 2
08	manswer = mfirst - msecond
09	Case Is = 3
10	manswer = mfirst * msecond
11	Case Is = 4
12	manswer = mfirst / msecond
13	End Select
14	txtNUMBER = manswer
15	End Sub


This post has been edited by macosxnerd101: 22 February 2011 - 08:01 AM
Reason for edit:: Fixed code tags. PLEASE, [code] Your Code Goes Here [/code]

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8371
  • View blog
  • Posts: 31,101
  • Joined: 12-June 08

Re: vb calculator

Posted 22 February 2011 - 08:06 AM

Question - what was added to the preexisting code already floating around on the net?

web page / vbscript version Feb 13th, 2009
http://www.daniweb.c...read175395.html

vb code 08-25-2010
http://vrsforums.com...read.php?tid=23
Was This Post Helpful? 1
  • +
  • -

#5 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,755
  • Joined: 26-March 09

Re: vb calculator

Posted 22 February 2011 - 08:12 AM

There is a tutorial on creating a calculator HERE, which looks like where you may have got some of your code from.

One question....do you want it to work as a basic calculator, or does it really need to take your input string "2+2*3-1" etc. and then do the calculation based on that when you press the equals button?

If it is the latter then you need to break the string down into it's separate parts and work from that. Look at using the MID$ function for parsing through your string.
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,979
  • Joined: 19-May 09

Re: vb calculator

Posted 22 February 2011 - 01:59 PM

Quote

01	Private Sub cmd0_Click()
02	'Put the value 0 into the text box
03	Text1.text = Text1.text + "0"
04	End Sub
05	 
06	Private Sub cmd1_Click()
07	'Put the value 1 into the text box
08	Text1.text = Text1.text + "1"
09	End Sub

10	 
11	Private Sub cmd2_Click()
12	'Put the value 2 into the text box
13	Text1.text = Text1.text + "2"
14	End Sub
15	 
16	Private Sub cmd3_Click()
17	'Put the value 3 into the text box
18	Text1.text = Text1.text + "3"
19	End Sub
20	 
21	Private Sub cmd4_Click()
22	'Put the value 4 into the text box
23	Text1.text = Text1.text + "4"
24	End Sub
25	 
26	Private Sub cmd5_Click()
27	'Put the value 5 into the text box
28	Text1.text = Text1.text + "5"
29	End Sub
30	 
31	Private Sub cmd6_Click()
32	'Put the value 6 into the text box
33	Text1.text = Text1.text + "6"
34	End Sub
35	 
36	Private Sub cmd7_Click()
37	'Put the value 7 into the text box
38	Text1.text = Text1.text + "7"
39	End Sub
40	 
41	Private Sub cmd8_Click()
42	'Put the value 8 into the text box
43	Text1.text = Text1.text + "8"
44	End Sub
45	 
46	Private Sub cmd9_Click()
47	'Put the value 9 into the text box
48	Text1.text = Text1.text + "9"
49	End Sub

This is far better rendered as a control array. Create a control array called cmdNumPad. Make sure the Caption property equals the Index property in all of them. Add this code:
Private Sub cmdNumPad_Click(Index as Integer)
Text1.Text = Text1.Text & cStr(Index)
End Sub

Just that simple.

This post has been edited by BobRodes: 22 February 2011 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1