Here I will shoe you how to create a simple calculator that can add, subtract, divide, multiply and change sign state. Most of the code is commented so it is easy to learn.

For this you will need 19 buttons and one text box (txtNUMBER). Have a look at the picture that is at the bottom on how I have made mine.

first we will declare the variables:

'Declare the global variables to be used throughout the form
Dim mfirst As Single
Dim msecond As Single
Dim manswer As Single
' Declare the global variables for the operators: Add,Sub,Mul and DIV
Dim mbutton As Integer
'Change the sign of the number from + or - or vice versa
' Depending on its state now they show in txtNUMBER text box
Dim Signstate As Boolean

Second we will make it so that the number buttons actually do something so add the code into the your form:

Noticing that all the number buttons are called cmd# (# is the number) as your buttons need to have these names.

Now we will make the math buttons. The first is the add button:

Private Sub cmdADD_Click()
'User slected the add button
mbutton = 1
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)
txtNUMBER = ""
End Sub

Subtract:

Private Sub cmdSUBTRACT_Click()
'User slected the minus button
mbutton = 2
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)
txtNUMBER = ""
End Sub

Multiply:

Private Sub cmdMULTIPLY_Click()
'User slected the multiply button
mbutton = 3
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)
txtNUMBER = ""
End Sub

Divide:

Private Sub cmdDIVIDE_Click()
'User slected the Divide button
mbutton = 4
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)
txtNUMBER = ""
End Sub

Now we will make the equals button work otherwise theres no point to all this.

Now your calculator is complete all you have to do is test it. Since this has a lot of code I have added a text file with the code in it. If you do have any problems then just comment me and I will do what I can to help.

When I am writing the code for each button to add the number i press to the text box it gives me an error that says "Operator '+' is not defined for types 'System.Windows.Forms.TextBox' and 'String'. I'm pretty new to Visual Basic 2008 and I have no clue what to do. Any suggestions to this problem?

When I am writing the code for each button to add the number i press to the text box it gives me an error that says "Operator '+' is not defined for types 'System.Windows.Forms.TextBox' and 'String'. I'm pretty new to Visual Basic 2008 and I have no clue what to do. Any suggestions to this problem?

Instead of using the "+" you could use the "&" operator. That way it will include the text in the text box and the int from the button press. so instead of

If Any One Is Interested I Remade The Whole Code In VB2008 or VB.NET As Its Become Known. I Will Post It Below. It Works Perfectly And I Used All The Same Names As Sam For The Buttons And Variables, cmd0, cmd1 etc.

Hi, why can't you create a control array for this..???
Create a button and give its name as cmd, then copy it and paste it. It will ask you, whether you want to create a control array. Click Yes. Continue pasting till you reached 10 buttons. Then do the below coding:

Here I will shoe you how to create a simple calculator that can add, subtract, divide, multiply and change sign state. Most of the code is commented so it is easy to learn.

For this you will need 19 buttons and one text box (txtNUMBER). Have a look at the picture that is at the bottom on how I have made mine.

first we will declare the variables:

'Declare the global variables to be used throughout the form
Dim mfirst As Single
Dim msecond As Single
Dim manswer As Single
' Declare the global variables for the operators: Add,Sub,Mul and DIV
Dim mbutton As Integer
'Change the sign of the number from + or - or vice versa
' Depending on its state now they show in txtNUMBER text box
Dim Signstate As Boolean

Second we will make it so that the number buttons actually do something so add the code into the your form:

Noticing that all the number buttons are called cmd# (# is the number) as your buttons need to have these names.

Now we will make the math buttons. The first is the add button:

Private Sub cmdADD_Click()
'User slected the add button
mbutton = 1
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)
txtNUMBER = ""
End Sub

Subtract:

Private Sub cmdSUBTRACT_Click()
'User slected the minus button
mbutton = 2
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)
txtNUMBER = ""
End Sub

Multiply:

Private Sub cmdMULTIPLY_Click()
'User slected the multiply button
mbutton = 3
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)
txtNUMBER = ""
End Sub

Divide:

Private Sub cmdDIVIDE_Click()
'User slected the Divide button
mbutton = 4
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)
txtNUMBER = ""
End Sub

Now we will make the equals button work otherwise theres no point to all this.

Now your calculator is complete all you have to do is test it. Since this has a lot of code I have added a text file with the code in it. If you do have any problems then just comment me and I will do what I can to help.

hello.i have complete this code,but it still not work.when i click the all cmd button especially the number,it doesn't show at the text box.why?the only button work are cancel,and exit.is there any problem?i also have checked my declaration but still does not work.please help me..