first of all this is not all my code. i took i tut off another site and made it better
i also fixed some things (it was a bad tut). secondly this isn't a tut how how to hack but instead something to get you started. you should learn the following
*simple api (very simple)
* fundamentals of vb's gui
*fundamentals of math using vb
*you should also just simply get used to it
*how to debug code
o and your more than likely going to need to rename all the buttons if you don't add them in the same order as me (i didn't put the buttons in any real order so your wasting time to try)
first thing is gui formating now this can be done any way you like but your going to need to be something like this (i made mine look like the windows calc)
now that you have that lets move on to code. first off we need to declare some globle varibles. the reason we do this is so all of the program can use
the same code. so by nature its hard to completely enplane now so just add
this to the top
Code:
[PHP]Public Class Form1
Private valHolder1 As Double
Private valHolder2 As Double
Private tmpValue As Double
Private hasDecimal As Boolean
Private inputStatus As Boolean
Private clearText As Boolean
Private calcFunc As String
Private Declare Function Beepapi Lib "kernel32" Alias "Beep" (ByVal dwFrequency As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
[/PHP]
next we want to have some way to deal with errors. i like to use noise. to do this we need to have a section off the program that delegates what sound to make. now you can do this with a sound file or make things much simpler and
use windows API this is the reason for two of those globle varibles
i talked about earlier seen here
Code:
[PHP]Private Declare Function Beepapi Lib "kernel32" Alias "Beep" (ByVal dwFrequency As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
[/PHP]
to utilize this we would do use something along the lines of the fallowing
Code:
[PHP]Private Sub Playsound(ByVal sTune As String)
Select Case sTune
Case "Error"
Beep()
Sleep(200)
Beep()[/PHP]
this when called on will make a nice little Beep Beep.
now we need to handle all of are calculator's operations and sutch things like
+, -, /, *, x^,ect...
we will use the same logic we used when we made the sub that beeps.
it would look something like this
Code:
[PHP]Private Sub CalculateTotals()
valHolder2 = CType(TextBox1.Text, Double)
Select Case calcFunc
Case "Add"
valHolder1 = valHolder1 + valHolder2
Case "Subtract"
valHolder1 = valHolder1 - valHolder2
Case "Divide"
valHolder1 = valHolder1 / valHolder2
Case "Multiply"
valHolder1 = valHolder1 * valHolder2
Case "PowerOf"
valHolder1 = System.Math.Pow(valHolder1, valHolder2)
Case "1/X"
valHolder1 = 1 / valHolder1
End Select
TextBox1.Text = CType(valHolder1, String)
inputStatus = False
End Sub[/PHP]
now what each case dose is allow you to go of on to a different line of code almost like a tournament tree except in reverse
now we need to add all of those operations. there all exactly the same expet you change out "add" for what ever part you need. the exception to this is the fraction button but will get to that in a bit for now heres the code for "add".
Code:
[PHP]Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
'Make sure out input box has a value
If TextBox1.Text.Length <> 0 Then
'Check the value of our function flag
If calcFunc = String.Empty Then 'Flag is empty
'Assign the value in our input
'box to our holder
valHolder1 = CType(TextBox1.Text, Double)
'Empty the input box
TextBox1.Text = String.Empty
Else 'Flag isnt empty
'Call our calculate totals method
CalculateTotals()
End If
'Assign a value to our calc function flag
calcFunc = "Add"
'Toggle the decimal flag
hasDecimal = False
inputStatus = False
End If
End Sub[/PHP]
now see all of the green things that have a ' before them those are notes so
you know what each line of code dose feel free to add you own.
now look at the 'toggle decimal flag part. those are a key part for most of my if statements they help to full proof the program.
now for the fraction button its all the same except for a little extra code
Code:
[PHP]Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
valHolder1 = CType(TextBox1.Text, Double)
CalculateTotals()
calcFunc = "1/X"
CalculateTotals()
calcFunc = String.Empty[/PHP]
now i now this looks like less code but its not its just missing the notes. see the second CalculateTotals() that and what follows it is the same logic we will use for are equal button. lets see what are equal button's code looks like.
Code:
[PHP]Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
If TextBox1.Text.Length <> 0 AndAlso valHolder1 <> 0 Then
CalculateTotals()
calcFunc = String.Empty
hasDecimal = False
inputStatus = True
End If
End Sub[/PHP]
now the only difference from the second part of the "1/x" (and this probably bad) is that i don't have true false statements (i haven't bug tested this very well so fell free to change this in yours if it helps)
now we need numbers and this is really simple we simply use the text of the button its self so all code is the same just simply change the name to match the correct button. o and this isn't completely true the zero button is a bit different, but same basic concept. heres the code for button number one
Code:
[PHP]Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If inputStatus Then
TextBox1.Text += Button1.Text
Else
TextBox1.Text = Button1.Text
inputStatus = True
End If
End Sub
[/PHP]
and heres the code for button zero
Code:
[PHP]Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If inputStatus Then
If TextBox1.Text.Length >= 1 Then
TextBox1.Text += Button10.Text
End If
End If
End Sub[/PHP]
ive been saying "next we need this" a lot but actruly you don't have to do any off this in this order.
i not very good at explaining certain(and i'm tired) things so for this button the (backspace) button so i'm not going to, but heres the code
Code:
[PHP]Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
'BackSpace
'Declare locals needed
Dim str As String
Dim loc As Integer
'Make sure the text length is > 1
If TextBox1.Text.Length > 0 Then
'Get the next to last character
str = TextBox1.Text.Chars(TextBox1.Text.Length - 1)
'Check if its a decimal
If str = "." Then
'If it is toggle the hasDecimal flag
hasDecimal = False
End If
'Get the length of the string
loc = TextBox1.Text.Length
'Remove the last character, incrementing by 1
TextBox1.Text = TextBox1.Text.Remove(loc - 1, 1)
End If
End Sub
[/PHP]
i my code next comes the decimal button. this code is completely my own. the tut i got the base screwed up bad and he programs for a living (he was up at five in the morning).
now for this next bit i want you to pay close attention to the if statements and the "Error" statement they prevent a lot of bugs. its like it goes though a security check and it has to meet all the criteria before it can pass.
Code:
[PHP]Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.Click
If Not TextBox1.Tex*****ntains(".") Then
If Not inputStatus Then
TextBox1.Text += Button10.Text
TextBox1.Text += Button22.Text
hasDecimal = True
inputStatus = True
ElseIf inputStatus Then
If TextBox1.Text.Length > 0 Then
TextBox1.Text += Button22.Text
hasDecimal = True
ElseIf TextBox1.Text.Length < 1 Then
TextBox1.Text += Button22.Text
hasDecimal = True
End If
End If
Else : Playsound("Error")
End If
[/PHP]
once again there is lot of bugs prevented by these if statements you should play around with them some to learn more about them
o if you have tried you program by now you probably have relized your sqrt function isnt working thats because i made a mistake in what i told but heres
the code to fix it.
Code:
[PHP]Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click
'sqrt
If TextBox1.Text.Length <> 0 Then
tmpValue = CType(TextBox1.Text, Double)
'Perform the square root
tmpValue = System.Math.Sqrt(tmpValue)
'Display the results in the input box
TextBox1.Text = CType(tmpValue, String)
'Clear the decimal flag
hasDecimal = False
End If
End Sub[/PHP]
(lol im so tired right now i almost coverd the equal button again)
now for the clear buttons. all they do is set the variables to 0 or false. C is clear all and CE is clear entry. here is CE
Code:
[PHP] Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
'clear entry
TextBox1.Text = String.Empty
inputStatus = False
hasDecimal = False
End Sub[/PHP]
and here is C
Code:
[PHP]Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
'clear
TextBox1.Text = String.Empty
valHolder1 = 0
valHolder2 = 0
calcFunc = String.Empty
inputStatus = False
hasDecimal = False
End Sub
[/PHP]
now for a cool little button the +/- button. now some of you are probably thinking you should use a toggle button well i couldn't find one so this is the code you get it dose the same thing. if this change to that, if that change to this(are you catching on to the similarity between English and vb). now there is one impotent difference, my(its yous once you make it) program says if nothing than "BEEP"(lol). heres the code.
Code:
[PHP]Private Sub Button21_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Click
If inputStatus Then
If TextBox1.Text.Length > 0 Then
If TextBox1.Text = -TextBox1.Text Then
TextBox1.Text = +TextBox1.Text
ElseIf TextBox1.Text = +TextBox1.Text Then
TextBox1.Text = -TextBox1.Text
End If
Else : Playsound("Error")
End If
Else : Playsound("Error")
End If
End Sub[/PHP]
try making you own version i pretty sure there a bunch i of ways to do this i just didn't think of them.
now for the memory buttons. for those of you who are lazy(me) hide a label for those of you are not declare a globle variable and use it where i usel the label.
this is for MC (memory clear)
Code:
[PHP]Private Sub Button24_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button24.Click
Label2.Text = 0
TextBox2.Text = ""
End Sub[/PHP]
this for MR (i don't know what it stands for but it displays the number in your memory)
Code:
[PHP]Private Sub Button25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button25.Click
TextBox1.Text = Label2.Text
End Sub[/PHP]
this is for M+ and MS (i cant tell the difference if there is one but in this program there the same so if you know the difference grate use it and post)
Code:
[PHP]Private Sub Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button26.Click
Label2.Text = TextBox1.Text
TextBox2.Text = "M"
End Sub
[/PHP]
o and you should put a little un-editable to display the "m" in the corner here look at mine
notice the "M" of to the side in the box.
now remember you going to need to debug all of this to fit your buttons
but other than that thats it you have calculator plz thank me if you learned something.
so... hope you enjoy, credits: me (special thanks /as always xD/ to IHelper
Lmao! Nice TUT and good job giving credits. I got a simple way for calc and has all functions. No code needed aswell hehe.
wow... i made this TUT hard xD php php php php php -.-
more will come
Good Job
You want respect
Earn it.
/b/oss (03-20-2010)
xD haha dude
xD lol Slovenia!!!