Vous êtes sur la page 1sur 8

How to Create a Basic Calculator in Visual Basic.NET - VBasic.NET | V...

http://www.vbasic.net/how-to-create-a-basic-calculator-in-visual-basic-n...

VBasic.NET | Visual Basic Tutorials, Tips, and Code Examples Search Home Search Contact Us About Us VB Resources VB Tutorials

How to Create a Basic Calculator in Visual Basic.NET


See more tutorials in Syntax. This post has No Comments. 2

First I want to say thank you for taking the time to read this tutorial. If your a beginner this will help you a lot in the basics of visual basic and also create a fun little application. In this tutorial we will be creating a calculator in visual basic. For this calculator we will use radio buttons, text boxes and buttons to create a fully functional calculator. Hopefully this will intrigue you into making other fun little applications, for me, the better you make your projects the better I feel mentally and physically. 1. Open Microsoft Visual Studio. 2. Click File > New > Project. 3. In installed templates, click on Other Languages > Visual Basic > Windows. Click on Windows Forms Application, name the file Calculator in Visual Basic and click OK. You will see a blank form like this:

1 of 8

12/4/2013 12:32 PM

How to Create a Basic Calculator in Visual Basic.NET - VBasic.NET | V...

http://www.vbasic.net/how-to-create-a-basic-calculator-in-visual-basic-n...

We will add radio buttons, text boxes and buttons to the form to create the design of the calculator. Keep in mind you can design your calculator the way you want, it does not have to be identical to mine, the functionality will be the same regardless of the calculators design. 4. In the Toolbox panel, click and drag a RadioButton onto the form. Copy the radio button and paste it onto the form three more times, until there is a total of four radio buttons on the form. 5. In the properties window of RadioButton1, change its text to Add and change its name to addRb. In the properties window of RadioButton2, change its text to Subtract and change its name to subtractRb. In the properties window of RadioButton3, change its text to Multiply and change its name to multiplyRb. In the properties window of RadioButton4, change its text to Divide and change its name to divideRb. 6. In the Toolbox panel, click and drag a TextBox onto the form. Copy the text box and paste it onto the form. You should have a total of two text boxes on the form. In the properties window of TextBox1, rename it userInput1TextBox. In the properties window to TextBox2, rename it userInput2TextBox. 7. In the Toolbox panel, click and drag a Button control onto the form. Copy the button and paste it onto the form. You should have a total of two buttons on the form. In the properties window of the Button1, change its text to Calculate and its name to calculateBtn. In the properties window of Button2, change its text to Clear and its name to clearBtn. 8.In the Toolbox panel, click and drag a Label onto the form. Copy the label and paste it onto the form. You should have a total of two labels on the form. In the properties window of Label1, change its name to symbolLbl and delete its text so it is blank. In the properties window of Label2, change its name to answerLbl and delete its text so it is blank. We will use symbolLbl for the operation symbol, so place it between the text boxes where it makes most sense. The answerLbl will be used to output the answer, so place it where it makes most sense. This is what my design looks like, there is one label in between the two text boxes and one to the right of the calculate button. Remember that you can design it any way youd like.

2 of 8

12/4/2013 12:32 PM

How to Create a Basic Calculator in Visual Basic.NET - VBasic.NET | V...

http://www.vbasic.net/how-to-create-a-basic-calculator-in-visual-basic-n...

This is where we actually do the coding that does the calculating. We will make great use of the If Else statement to make sure which radio button is checked and to put code in accordingly. 9. Double click on the Calculate button to take you to Form1.vb. You will see a calculateBtn_Click method, inside of this method is where we will enter the code. Although this is a basic calculator, I took the liberty to have a message box appear when a number is not entered or no operation is selected. Also we all know that when a zero is involved when multiplying or dividing, the answer will always be zero. In the case that a denominator is zero Ive programmed the application to read Cannot Divide by Zero because you cannot. The rest of the code is just calculating the user inputs depending on which radio button is selected.

3 of 8

12/4/2013 12:32 PM

How to Create a Basic Calculator in Visual Basic.NET - VBasic.NET | V...

http://www.vbasic.net/how-to-create-a-basic-calculator-in-visual-basic-n...

Now that we have the calculating part of the coding done, we will work on the clear button. The function of this button will be to clear what is inside the text boxes, the labels and the operation selection. 10. So lets start by going to Form1.vb [Design] and double click on the Clear button. We will basically set the text values of the text boxes and labels to null while also checking for which radio button is selected and changing it to false.

Now that we have the clear button finished, we still have to code the symbolLbl to change symbols according to which operation is selected. 11. Switch to Form1.vb [Design] and double click on the Add radio button. We want to tell the application to change symbolLbl to the correct operation symbol each time a radio button is selected. So to do that we will set the test of symbolLbl to + and the text of answerLbl to (nothing). We will do the same for the other radio buttons, just change the symbolLbl according to which operation is selected.

4 of 8

12/4/2013 12:32 PM

How to Create a Basic Calculator in Visual Basic.NET - VBasic.NET | V...

http://www.vbasic.net/how-to-create-a-basic-calculator-in-visual-basic-n...

So when all the coding is done you should have the same code that is in the code block:

5 of 8

12/4/2013 12:32 PM

How to Create a Basic Calculator in Visual Basic.NET - VBasic.NET | V...

http://www.vbasic.net/how-to-create-a-basic-calculator-in-visual-basic-n...

Run the application. Test the application and try to break it. It should be fully functional. Try entering only one number and dont select an operation to see if the message boxes appear each time. Use the clear button to clear everything from the calculator and use the calculate button to calculate the and output the correct answer. Everything works. This was a fun experiment, you learned a little bit about If Else statements but also about radio buttons, text boxes and buttons and you have a calculator to use whenever your in need of one!

6 of 8

12/4/2013 12:32 PM

How to Create a Basic Calculator in Visual Basic.NET - VBasic.NET | V...

http://www.vbasic.net/how-to-create-a-basic-calculator-in-visual-basic-n...

Download Source Files

Leave A Response
Name (required) Mail (will not be published) (required) Website

Subscribe To Our Feed Follow Us On Twitter

Navigation
Categories Pages Controls Error Handling Files Miscellaneous Network Syntax Windows Forms

7 of 8

12/4/2013 12:32 PM

How to Create a Basic Calculator in Visual Basic.NET - VBasic.NET | V...

http://www.vbasic.net/how-to-create-a-basic-calculator-in-visual-basic-n...

About Us Contact Us Search VB Resources VB Tutorials

Recent
Tutorials Comments Create a Master Page in Visual Basic.NET How to Create a PlaceHolder in Visual Basic.NET How to Create a Panel in Visual Basic.NET How to Use the Localize Control in Visual Basic.NET How to Add Items to a ListBox in Visual Basic.NET

Browse
About Us Contact Us Search VB Resources VB Tutorials

About Us
Here at VBasic.net our goal is to provide you with easy to understand and simple to implement tutorials for all things Visual Basic. Learn More About Us

Resources
.NET 4.0 Tutorials and Code Examples AJAX Tutorials and Code Examples ASP.NET 4.0 Tutorials and Code Examples Database Tutorials and Examples Programming Tutorials and Code Examples Web Development Tutorials and Articles 2013 All Rights Reserved

8 of 8

12/4/2013 12:32 PM

Vous aimerez peut-être aussi