techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Aug 14, 2008, 10:23 AM   #1
Dark_Webster
200 Posts
 
Join Date: Jul 2007
Location: Portugal
Posts: 335 (0.16/day)
Thanks: 18
Thanked 51 Times in 47 Posts

System Specs

Visual Basic : Build a SIMPLE calculator

Hey all. I thought that this section needed a little help so there it is.
For someone that wants to start programming in Visual Basic, I have a simple program that I'm going to show you.
I will teach, step-by-step how to build a simple calculator.

So here it goes ok?

First, make sure you have at least Visual Studio 2005 (it's the best I've ever experimented and you will learn fast).



Click the New Project button.
To begin with, you need to create a Visual Basic application with an empty project, like shown on this image.
The name of the project is at your criteria.



Then, when the project is created, you should click with the right mouse button on the bold letters on the Solution Explorer,I mean the name of the project. Then go to Add, Windows Form. Now, it should appear the same window that appears on the image. Then again, the name of the form is at your criteria.



Then double-click on the form you have just created and your window should look like this. If the toolbox doesn't appear, then go to View,Toolbox or press Ctrl+Alt+X.



Then in the toolbox, look for Textbox. Click on it and draw two textboxes on the form. Again, the size is at your will.
I will not change the name of the objects to keep things simple. Then look for label and draw one.
Now click on the label you've just created. In the block Properties just below Solution Explorer, change AutoSize to False and clear the Label1 text in Text. Then it should be like on the image, except for position and size of objects.

Now go to the toolbox and look for Button. Draw the first one, go to properties and in the Text value write Add.
Draw the second one and in Text value write Subtract, draw the third and in Text value write Multiply. Finally, draw the fourth and in Texh value write Divide. Oh I forgot, after doing all that, click on the label that you've introduced on the form and look in Properties for BorderStyle. Change it to Fixed3D.

Now that we have it all, we can now start to write the source code.



Now, double-click on the Add button. It should appear a window like this. This window is the coder part of the program.
It's where you write the source code of the program, more precisly, the Add button you've created on the form. Warning: Do not confuse Button1_Click with the Add button. Button1 is the name of the object to the program, Add is just a string to know what the button does.

Then put this on the code:
Code:
Label1.Text = Val(TextBox1) + Val(TextBox2)
This will add the value on Textbox1 with Textbox2 and show it on Label1. Now click on the design tab and double click on the subtract button and put the same code, you only need to change the signal to minus. Do the same procedure to Multiply and change it to the right signal. Now go click on the Divide button.

This is the one that needs a different code, because it's more suspectible to errors like dividing 0 by 0.
Put this code:
Code:
        
        Try
            Label1.Text = Val(TextBox1.Text) / Val(TextBox2.Text)
        Catch ex As Exception

        End Try
This will end the errors on dividing zero by zero or dividing without anything on the textboxes.



The code should be like this (depending how are your tastes at formatting text).
Ignore the warnings, the program still works perfectly. If you have followed all the steps, now try out the program.
Click on the green play and try it .


I know that this may look too easy, but I wanted to show to people that programming is not so boring. I hope you like it and if someone has corrections do do, please step foward.
__________________
“bet it still sounds like an adolescent sewing machine on steroids.” -WhiteLotus

Lappy: HP 6720s - Intel C2D T2390, 2GB DDR2, 250GB Harddrive, Intel GME965, DVD Rewriter, Card Reader, Vista 32 Home Premium SP1


^^ - Stefanels did this!

Last edited by Dark_Webster; Aug 14, 2008 at 11:11 AM.
Dark_Webster is offline  
Reply With Quote
The Following User Says Thank You to Dark_Webster For This Useful Post:
Old Aug 14, 2008, 04:30 PM   #2
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

What's the point of val()?
DanTheBanjoman is offline  
Reply With Quote
Old Aug 14, 2008, 04:55 PM   #3
SathyaBhat
5 Posts
 
Join Date: Aug 2008
Location: Chennai / Mangalore, India
Posts: 22 (0.01/day)
Thanks: 3
Thanked 8 Times in 3 Posts
Send a message via Yahoo to SathyaBhat

System Specs

Quote:
Originally Posted by DanTheBanjoman View Post
What's the point of val()?
well the text box holds the values in string format, val() does type conversion from string to integer (number) so that the math operations can be done properly.
SathyaBhat is offline  
Reply With Quote
Old Aug 14, 2008, 05:27 PM   #4
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

Quote:
Originally Posted by SathyaBhat View Post
well the text box holds the values in string format, val() does type conversion from string to integer (number) so that the math operations can be done properly.
Val() doesn't convert to string, it takes a number from a string. You can still enter all kinds of pointless things in the box. Use an input mask and the whole issue is solved.
DanTheBanjoman is offline  
Reply With Quote
Old Aug 14, 2008, 06:25 PM   #5
Oliver_FF
200 Posts
 
Oliver_FF's Avatar
 
Join Date: Oct 2006
Posts: 478 (0.20/day)
Thanks: 23
Thanked 66 Times in 53 Posts

System Specs

Quote:
Originally Posted by DanTheBanjoman View Post
Val() doesn't convert to string, it takes a number from a string. You can still enter all kinds of pointless things in the box. Use an input mask and the whole issue is solved.
SathyaBhat is correct, val(String) will type conversion from string to integer. It's vital because without it, the + will be String concatenation and 3 + 4 will suddenly result in 34.

Dan is also correct that you can still enter all kinds of non-integers into the box. I suspect that val() with throw an exception if you do this - just catch it and set like "Math Error" as the result or something...

Oh, and good effort Dark_Webster Some suggestions for some fun other related stuff:
1. Factorise quadratic equations... 3x^2 + 2x +6 = 0
2. Solve simultaneous equations 3x + 4y = 7, x + y = 2
Oliver_FF is offline  
Reply With Quote
Old Aug 14, 2008, 06:30 PM   #6
SathyaBhat
5 Posts
 
Join Date: Aug 2008
Location: Chennai / Mangalore, India
Posts: 22 (0.01/day)
Thanks: 3
Thanked 8 Times in 3 Posts
Send a message via Yahoo to SathyaBhat

System Specs

Correct. but it seems it picks up the numbers only if the string starts with a number. If the string is like test123 then val() returns 0

If the string is 123test231 then val() returns only 123.
SathyaBhat is offline  
Reply With Quote
Old Aug 14, 2008, 06:49 PM   #7
Oliver_FF
200 Posts
 
Oliver_FF's Avatar
 
Join Date: Oct 2006
Posts: 478 (0.20/day)
Thanks: 23
Thanked 66 Times in 53 Posts

System Specs

I'm suprised that it even returns anything, maybe i'm just too used to C/C++/C#/Java where it's mainly up to the programmer to make sure everything is used perfectly...
Oliver_FF is offline  
Reply With Quote
Old Aug 14, 2008, 06:52 PM   #8
chron
500 Posts
 
Join Date: May 2006
Posts: 533 (0.21/day)
Thanks: 46
Thanked 35 Times in 30 Posts

in the textbox's keypress method insert:

If e.KeyChar <> vbBack Then
e.Handled = IIf(IsNumeric(e.KeyChar), False, True)
End If

This will take away the user's ability to insert anything other than numbers. If you're concerned about pasting info in using the right click menu, you can always insert a blank context menu and set the textbox's context menu property to point to the empty context menu.

Using Val() is good because it's simple, but you can also declare an integer variable and set it using a convert.to function. For instance:

dim x as integer = convert.toInt64(textbox1.text)

then of course instead of using textbox1.text in your coding, you can just point to x

happy coding
chron is offline  
Reply With Quote
Old Aug 14, 2008, 07:16 PM   #9
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

Quote:
Originally Posted by chron View Post
in the textbox's keypress method insert:

If e.KeyChar <> vbBack Then
e.Handled = IIf(IsNumeric(e.KeyChar), False, True)
End If

This will take away the user's ability to insert anything other than numbers. If you're concerned about pasting info in using the right click menu, you can always insert a blank context menu and set the textbox's context menu property to point to the empty context menu.

Using Val() is good because it's simple, but you can also declare an integer variable and set it using a convert.to function. For instance:

dim x as integer = convert.toInt64(textbox1.text)

then of course instead of using textbox1.text in your coding, you can just point to x

happy coding
Why write code when you can simply use an input mask? Only allow numeric input and all is solved.
DanTheBanjoman is offline  
Reply With Quote
Old Aug 14, 2008, 08:30 PM   #10
chron
500 Posts
 
Join Date: May 2006
Posts: 533 (0.21/day)
Thanks: 46
Thanked 35 Times in 30 Posts

Quote:
Originally Posted by DanTheBanjoman View Post
Why write code when you can simply use an input mask? Only allow numeric input and all is solved.
I mean, either way is cool I guess lol. I normally use masked textboxes for things like phone number, zip, date, and ssn's. I suppose it depends on how many decimal places you want your calculator to be capable of...
chron is offline  
Reply With Quote
Old Aug 14, 2008, 08:36 PM   #11
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

Quote:
Originally Posted by chron View Post
I mean, either way is cool I guess lol. I normally use masked textboxes for things like phone number, zip, date, and ssn's. I suppose it depends on how many decimal places you want your calculator to be capable of...
Good point, decimals can change things.

I guess my SQL teacher wasted my way of thinking. He used to bitch about me solving things frontend that could be done on the backend. And he always said anything that can be solved in the backend should be solved there. Which actually is true in most cases.
DanTheBanjoman is offline  
Reply With Quote
Old Aug 14, 2008, 08:41 PM   #12
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

Perhaps something fun, how about we all make our own calculator in our fav language and publish the source for eachother to learn? I guess I can construct something tomorrow.
It might draw some interest of people eager to learn but not knowing where to start. With that in mind I would say comments in code will be a requirement.
DanTheBanjoman is offline  
Reply With Quote
Old Aug 15, 2008, 09:51 AM   #13
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

I just wasted 10min making v1 of my calculator. It does basic things so far.

http://dan.h0sted.org/Calculator.exe for the program.
http://dan.h0sted.org/Calculator.rar for the source, it's made vb.net 2003.

I didn't extensively test it, it just seemed to work I'm pretty sure it'll crash somehow because I didn't think of things.
DanTheBanjoman is offline  
Reply With Quote
Old Aug 15, 2008, 07:01 PM   #14
Dark_Webster
200 Posts
 
Join Date: Jul 2007
Location: Portugal
Posts: 335 (0.16/day)
Thanks: 18
Thanked 51 Times in 47 Posts

System Specs

Oh just one thing.

When using masked textboxes, how can I make the cursor appear on the very left of it when I click on it? Because if I click on the middle of the textbox, it will start on the place that I clicked.
__________________
“bet it still sounds like an adolescent sewing machine on steroids.” -WhiteLotus

Lappy: HP 6720s - Intel C2D T2390, 2GB DDR2, 250GB Harddrive, Intel GME965, DVD Rewriter, Card Reader, Vista 32 Home Premium SP1


^^ - Stefanels did this!
Dark_Webster is offline  
Reply With Quote
Old Aug 16, 2008, 10:36 AM   #15
Dark_Webster
200 Posts
 
Join Date: Jul 2007
Location: Portugal
Posts: 335 (0.16/day)
Thanks: 18
Thanked 51 Times in 47 Posts

System Specs

And, I need to know also how to show a messagebox when an exception is catched...
__________________
“bet it still sounds like an adolescent sewing machine on steroids.” -WhiteLotus

Lappy: HP 6720s - Intel C2D T2390, 2GB DDR2, 250GB Harddrive, Intel GME965, DVD Rewriter, Card Reader, Vista 32 Home Premium SP1


^^ - Stefanels did this!
Dark_Webster is offline  
Reply With Quote
Old Aug 16, 2008, 10:40 AM   #16
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

Quote:
Originally Posted by Dark_Webster View Post
Oh just one thing.

When using masked textboxes, how can I make the cursor appear on the very left of it when I click on it? Because if I click on the middle of the textbox, it will start on the place that I clicked.
There should be a property to align text.

Quote:
Originally Posted by Dark_Webster View Post
And, I need to know also how to show a messagebox when an exception is catched...
Use try/catch you can catch exceptions, just type "try" and vs will add the rest for you. It'll explain itself. Use msgbox("Omg the world exploded") to show a messagebox.
DanTheBanjoman is offline  
Reply With Quote
Old Aug 16, 2008, 10:50 AM   #17
Dark_Webster
200 Posts
 
Join Date: Jul 2007
Location: Portugal
Posts: 335 (0.16/day)
Thanks: 18
Thanked 51 Times in 47 Posts

System Specs

Try
Sinal.Text = "/"
BarraProgresso.Value = 0
Resultado.Text = Val(Valor1.Text) / Val(Valor2.Text)
BarraProgresso.Value = 100
StatusCalculadora.Text = ("You've just divided something...")
Catch ex As Exception
MsgBox("You're smart xD")
End Try

Is the messagebox in the right place?

If it is, when I do an exception it doesn't appear
__________________
“bet it still sounds like an adolescent sewing machine on steroids.” -WhiteLotus

Lappy: HP 6720s - Intel C2D T2390, 2GB DDR2, 250GB Harddrive, Intel GME965, DVD Rewriter, Card Reader, Vista 32 Home Premium SP1


^^ - Stefanels did this!
Dark_Webster is offline  
Reply With Quote
Old Aug 16, 2008, 10:53 AM   #18
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

Quote:
Originally Posted by Dark_Webster View Post
Try
Sinal.Text = "/"
BarraProgresso.Value = 0
Resultado.Text = Val(Valor1.Text) / Val(Valor2.Text)
BarraProgresso.Value = 100
StatusCalculadora.Text = ("You've just divided something...")
Catch ex As Exception
MsgBox("You're smart xD")
End Try

Is the messagebox in the right place?

If it is, when I do an exception it doesn't appear
But if you remove try/catch it does crash?
DanTheBanjoman is offline  
Reply With Quote
Old Aug 16, 2008, 10:55 AM   #19
Dark_Webster
200 Posts
 
Join Date: Jul 2007
Location: Portugal
Posts: 335 (0.16/day)
Thanks: 18
Thanked 51 Times in 47 Posts

System Specs

Quote:
Originally Posted by DanTheBanjoman View Post
But if you remove try/catch it does crash?
It does not... it only crashes if i divide with the textboxes empty... and try removes that error, but I wanted to show that messagebox when a exception ocurrs.
__________________
“bet it still sounds like an adolescent sewing machine on steroids.” -WhiteLotus

Lappy: HP 6720s - Intel C2D T2390, 2GB DDR2, 250GB Harddrive, Intel GME965, DVD Rewriter, Card Reader, Vista 32 Home Premium SP1


^^ - Stefanels did this!
Dark_Webster is offline  
Reply With Quote
Old Aug 16, 2008, 12:07 PM   #20
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

And it doesn't show the textbox when you leave the divide textbox empty?
DanTheBanjoman is offline  
Reply With Quote
Old Aug 16, 2008, 12:29 PM   #21
Dark_Webster
200 Posts
 
Join Date: Jul 2007
Location: Portugal
Posts: 335 (0.16/day)
Thanks: 18
Thanked 51 Times in 47 Posts

System Specs

Quote:
Originally Posted by DanTheBanjoman View Post
And it doesn't show the textbox when you leave the divide textbox empty?
No, it does not.
__________________
“bet it still sounds like an adolescent sewing machine on steroids.” -WhiteLotus

Lappy: HP 6720s - Intel C2D T2390, 2GB DDR2, 250GB Harddrive, Intel GME965, DVD Rewriter, Card Reader, Vista 32 Home Premium SP1


^^ - Stefanels did this!
Dark_Webster is offline  
Reply With Quote
Old Aug 16, 2008, 01:02 PM   #22
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

Look in my sample, it has some try/catch in it. See if there is anything different.
DanTheBanjoman is offline  
Reply With Quote
Old Aug 16, 2008, 02:14 PM   #23
Dark_Webster
200 Posts
 
Join Date: Jul 2007
Location: Portugal
Posts: 335 (0.16/day)
Thanks: 18
Thanked 51 Times in 47 Posts

System Specs

No it is the same, except the cases xD.
__________________
“bet it still sounds like an adolescent sewing machine on steroids.” -WhiteLotus

Lappy: HP 6720s - Intel C2D T2390, 2GB DDR2, 250GB Harddrive, Intel GME965, DVD Rewriter, Card Reader, Vista 32 Home Premium SP1


^^ - Stefanels did this!
Dark_Webster is offline  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
New Build Thread bruins004 System Builder's Advice 159 Mar 5, 2013 10:53 AM
Overclocking is Easy! Get Results! Kursah Overclocking & Cooling 131 May 12, 2010 12:14 AM
Show SLi Visual Indicator DaMulta NVIDIA 11 Mar 28, 2009 01:28 PM
First Build Advice, Basic! kyle2020 System Builder's Advice 94 Sep 29, 2008 08:22 PM
Visual Basic HELP dcf-joe Programming & Webmastering 6 Mar 5, 2008 01:36 PM


All times are GMT. The time now is 01:04 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts