Making a calculator in VB 2008

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 8004 Views - Last Post: 21 January 2010 - 09:02 PM Rate Topic: -----

#1 Tiffney83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-January 10

Making a calculator in VB 2008

Post icon  Posted 20 January 2010 - 08:00 AM

Attached ImageHello I have to write a program that allows users to specify two numbers and then adds, subtracts or multiplies them when the user clicks on the appropriate button. The out put should give the aritmatic performed and the result. I inserted an attachment on what it should look like also

Public Sub btnCompute_Click() Handles btnCompute.Click  
" here at this one I am getting an error message Error 1 Statement is not valid in a namespace. C:\Users\Tiffney\Desktop\School projects\WindowsApplication1\Form1.vb 1 1 WindowsApplication1"


Dim num1, num2, sum As Double

num1 = CDbl(txtFirstNum.Text)

num2 = CDbl(txtSecondNum.Text)

sum = num1 + num2

textSum.Text = CStr num1 & "+" CStr num2

textSum.Text = CStr num1 & "-" CStr num2

textSum.Text = CStr num1 & "*" CStr num2

End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Making a calculator in VB 2008

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Making a calculator in VB 2008

Posted 20 January 2010 - 08:32 AM

Have you read through this tutorial? It might help

http://www.dreaminco...wtopic32951.htm
Was This Post Helpful? 0
  • +
  • -

#3 Tiffney83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-January 10

Re: Making a calculator in VB 2008

Posted 20 January 2010 - 08:32 AM

Also I was wondering what should I name the text boxes under the first number, second number and where the answer is to pop up and the add subtract and multiply should I name them AddNumTxt and so on. I am so new to this it is scary
Was This Post Helpful? 0
  • +
  • -

#4 Tiffney83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-January 10

Re: Making a calculator in VB 2008

Posted 20 January 2010 - 08:43 AM

This is my new code
Public Class Form1

	Private Sub txtFirstNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFirstNum.Click
		Dim num1, num2, sum As Double
		num1 = CDbl(txtFirstNum.Text)
		num2 = CDbl(txtSecondNum.Text)
		sum = num1 + num2
	End Sub

	Private Sub frmAdd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmAdd.TextChanged
		Dim num1, num2, sum As Double
		num1 = CDbl(txtFirstNum.Text)
		num2 = CDbl(txtSecondNum.Text)
		sum = num1 + num2
	End Sub

	Private Sub txtSecondNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSecondNum.Click
		Dim num1, num2, sum As Double
		num1 = CDbl(txtFirstNum.Text)
		num2 = CDbl(txtSecondNum.Text)
		sum = num1 + num2
	End Sub

	Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
		Dim num1, num2, sum As Double
		num1 = CDbl(txtFirstNum.Text)
		num2 = CDbl(txtSecondNum.Text)
		sum = num1 + num2
	End Sub

	Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
		Dim num1, num2, sum As Double
		num1 = CDbl(txtFirstNum.Text)
		num2 = CDbl(txtSecondNum.Text)
		sum = num1 + num2
	End Sub

	Private Sub frmSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmSubtract.Click
		Dim num1, num2, sum As Double
		num1 = CDbl(txtFirstNum.Text)
		num2 = CDbl(txtSecondNum.Text)
		sum = num1 - num2
	End Sub

	Private Sub frmmultply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmmultply.Click
		Dim num1, num2, sum As Double
		num1 = CDbl(txtFirstNum.Text)
		num2 = CDbl(txtSecondNum.Text)
		sum = num1 * num2
	End Sub

	Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Making a calculator in VB 2008

Posted 20 January 2010 - 08:52 AM

Your names are pretty descriptive and you appear to be staying in a convention of prefixing the object type with the name so all I can say is, as long as they are descriptive to the code it is fine. As for the textbox that displays the results, you could use txtResult.

I personally use the txt type prefixes as they help when using intellisense to find a controls name. In a large form where you could have a lot of textboxes but don't know exactly what you called it, you can just type txt and all the objects starting with txt will show up and allow you to find the objects name.

This to me is better than names like CustomerTextBox for this reason alone.

This post has been edited by CharlieMay: 20 January 2010 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

#6 Tiffney83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-January 10

Re: Making a calculator in VB 2008

Posted 20 January 2010 - 08:56 AM

View PostCharlieMay, on 20 Jan, 2010 - 07:32 AM, said:

Have you read through this tutorial? It might help

http://www.dreaminco...wtopic32951.htm

I have looked at it over and over I cant understand it still
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Making a calculator in VB 2008

Posted 20 January 2010 - 09:03 AM

OK, after looking through your code you have a lot of unnecessary repetition.

The button_click events are all you really need for what you appear to be doing.

When you click on the multiply button. The button_click event is triggered where you calculate the two numbers by multiplying them together, same goes for add button and subtract button.

You are storing this result in the variable sum so you can display that value in a textbox or append it to the end of a string your are displaying in the result box.

For example

txtResult.Text = cstr(sum)


this would display the result of the calculation in the txtResult textbox.

You could just as easily do the following:
txtResult.Text = cstr(num1) & " X " & cstr(num2) & " = " & cstr(sum)


which would display something like
4 X 5 = 20

This post has been edited by CharlieMay: 20 January 2010 - 09:04 AM

Was This Post Helpful? 0
  • +
  • -

#8 Tiffney83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-January 10

Re: Making a calculator in VB 2008

Posted 20 January 2010 - 09:17 AM

View PostCharlieMay, on 20 Jan, 2010 - 08:03 AM, said:

OK, after looking through your code you have a lot of unnecessary repetition.

The button_click events are all you really need for what you appear to be doing.

When you click on the multiply button. The button_click event is triggered where you calculate the two numbers by multiplying them together, same goes for add button and subtract button.

You are storing this result in the variable sum so you can display that value in a textbox or append it to the end of a string your are displaying in the result box.

For example

txtResult.Text = cstr(sum)


this would display the result of the calculation in the txtResult textbox.

You could just as easily do the following:
txtResult.Text = cstr(num1) & " X " & cstr(num2) & " = " & cstr(sum)


which would display something like
4 X 5 = 20

Thank you For helping me, you have to remember I am new to this not understanding nothing, So I am not understanding how you are explaining it to me, I have zero errors and when I run debug and type a number in it is not letting me, I know you cant give me the code for it but maybe you can explain it to me a more bit down to earth for me to understand, as of now this is what I have
Public Class Form1

	Private Sub txtFirstNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FirstNum.Click
		Dim num1, num2, sum As Double
		num1 = CDbl(FirstNum.Text)
		num2 = CDbl(labelSecondNum.Text)
		sum = num1 + num2
	End Sub

	Private Sub frmAdd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFirstNum.TextChanged
		Dim num1, num2, sum As Double
		num1 = CDbl(labelSecondNum.Text)
		num2 = CDbl(labelSecondNum.Text)
		sum = num1 + num2
	End Sub

	Private Sub txtSecondNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles labelSecondNum.Click
		Dim num1, num2, sum As Double
		num1 = CDbl(FirstNum.Text)
		num2 = CDbl(labelSecondNum.Text)
		sum = num1 + num2
	End Sub

	Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSecondNum.TextChanged
		Dim num1, num2, sum As Double
		num1 = CDbl(FirstNum.Text)
		num2 = CDbl(labelSecondNum.Text)
		sum = num1 + num2
	End Sub

	Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmAdd.Click
		Dim num1, num2, sum As Double
		num1 = CDbl(txtFirstNum.Text)
		num2 = CDbl(txtSecondNum.Text)
		sum = num1 + num2
	End Sub

	Private Sub frmSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmSubtract.Click
		Dim num1, num2, sum As Double
		num1 = CDbl(FirstNum.Text)
		num2 = CDbl(labelSecondNum.Text)
		sum = num1 - num2
	End Sub

	Private Sub frmmultply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmMultply.Click
		Dim num1, num2, sum As Double
		num1 = CDbl(FirstNum.Text)
		num2 = CDbl(labelSecondNum.Text)
		sum = num1 * num2
	End Sub

	Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtResult.TextChanged


	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Making a calculator in VB 2008

Posted 20 January 2010 - 09:34 AM

OK,
For simplicity, lets say you have 3 Textboxes (txtFirstNum, txtSecondNum, txtResult) and 3 buttons (btnAdd, btnSubtract, btnMultiply)

The only place you would need code is inside the button_click events. Lets say you want to multiply the two numbers when you click on multiply button.

Inside that button_click event you would have the code:
	Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
		num1 = CDbl(txtFirstNum.Text)
		num2 = CDbl(txtSecondNum.Text)
		sum = num1 * num2
		txtResult.Text = sum
	End Sub



Now what this does, is takes the 2 numbers entered into the textboxes and prints the result in the txtResult textbox.

Notice that the Dims are missing, you can move them so that you don't have to dim them for each routine.

So your code would look like:
Public Class frmCalculate
Dim num1, num2, sum as Double
	Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
		num1 = CDbl(txtFirstNum.Text)
		num2 = CDbl(txtSecondNum.Text)
		sum = num1 * num2
		txtResult.Text = sum
	End Sub
End Class



You would repeat this for the other two buttons performing their respective calculation.

This of course doesn't check to ensure a number is entered or display anything but the result but it should move you in the right direction.

This post has been edited by CharlieMay: 20 January 2010 - 09:35 AM

Was This Post Helpful? 0
  • +
  • -

#10 Tiffney83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-January 10

Re: Making a calculator in VB 2008

Posted 20 January 2010 - 10:12 AM

Thank you CharlieMay, you have been a real help and I will go and try it the way you are describing
Was This Post Helpful? 0
  • +
  • -

#11 Nerfsqueezer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-January 10

Re: Making a calculator in VB 2008

Posted 21 January 2010 - 02:50 PM

OK I got that Part so what do you do if you want the boxes to clear when a user input a different value in one of the boxes?
Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Making a calculator in VB 2008

Posted 21 January 2010 - 04:05 PM

set them = nothing. You could do it in the textchanged events for each textbox or you could write a routine and pass the textbox that has focus to it so that it doesn't get cleared. The second option would reduce any duplicate code but would require a little more knowledge of sub-routines and passing parameters.
Was This Post Helpful? 0
  • +
  • -

#13 Nerfsqueezer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-January 10

Re: Making a calculator in VB 2008

Posted 21 January 2010 - 06:53 PM

View PostCharlieMay, on 21 Jan, 2010 - 03:05 PM, said:

set them = nothing. You could do it in the textchanged events for each textbox or you could write a routine and pass the textbox that has focus to it so that it doesn't get cleared. The second option would reduce any duplicate code but would require a little more knowledge of sub-routines and passing parameters.


I'm not sure what you mean "set them to nothing". What are the textchanged events? Just to clarify I want the 2 boxes to clear when a new number is put into the first or second number box as to avoid confusion for the user when they attempt to calculate a new problem.

Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#14 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Making a calculator in VB 2008

Posted 21 January 2010 - 07:02 PM

There are numerous ways to clear a text box. A couple of ways are
TextBox1.Text = "" or TextBox1.Text = Nothing

As for the events If you double-click on the textbox in the editor, it will open the code window and by default, will show the sub for TextBox1_TextChanged

This event is triggered every time the text changes in the textbox. To see a list of events for the control you can drop the combobox on the right side of the code window and get a list of events.

I personally would clear the textboxes when one of the 3 buttons is pressed. Once clicked, the result would show the calculation and the result and clear the two textboxes leaving the user ready for the next calculation.

Hope this isn't more confusing.
Was This Post Helpful? 0
  • +
  • -

#15 Nerfsqueezer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-January 10

Re: Making a calculator in VB 2008

Posted 21 January 2010 - 07:31 PM

View PostCharlieMay, on 21 Jan, 2010 - 06:02 PM, said:

There are numerous ways to clear a text box. A couple of ways are
TextBox1.Text = "" or TextBox1.Text = Nothing

As for the events If you double-click on the textbox in the editor, it will open the code window and by default, will show the sub for TextBox1_TextChanged

This event is triggered every time the text changes in the textbox. To see a list of events for the control you can drop the combobox on the right side of the code window and get a list of events.

I personally would clear the textboxes when one of the 3 buttons is pressed. Once clicked, the result would show the calculation and the result and clear the two textboxes leaving the user ready for the next calculation.

Hope this isn't more confusing.


Thx that helped alot. Seems so simple now, almost too simple. All I put in the First Number text box was

[code]

txt.Answer.text = ""

[\code]

And did the same in the Second number box. Could it be that easy?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2