- Visual Basic Simple Calculator Code
- Visual Basic Tip Calculator
- Visual Basic .NET Language - Creating A Simple Calculator To ...
- How To Make A Calculator In Visual Basic 6.0
Once you have installed Visual Studio from start a new project. Select 'Windows Forms Application' from Visual Basic Tab. You can rename it here if you need to. Once you click 'OK', you will see this window: Click on the 'Toolbox' tab on the left. The toolbar has 'auto-hide' option enabled by default. The simplest way to see what Managed Extensibility Framework can do is to build a simple MEF application. In this example, you build a very simple calculator named SimpleCalculator. The goal of SimpleCalculator is to create a console application that accepts basic arithmetic commands, in the form '5+3' or '6-2', and returns the correct answers.
Building a calculator program is one of the best ways to train one’s mind when it comes to creating an algorithm and forming logic. Though a calculator sounds easy to create, it might not that as simple to create as you think even the most basic one that compose only of the MDAS (Multiplication, Division, Addition and Subtraction) operations. Obviously, these operations can be processed by human brain easily but teaching a program to think that way is another thing.
This tutorial shows how to develop a simple calculator application in C that runs in a console window using Visual Studio 2017. Time to Complete. A simple calculator application written in C that performs math operations on two inputs and outputs the result to. Today i am going to build a simple Calculator application using C# in Visual studio 2010 /2012.I will create a setup file(.exe file)of this application in visual studio 2010/2012.But i Will show you 'How to create setup file in visual studio 2012 'only.Because I have already explained 'How to create setup file in visual studio ' in our previous tutorial. If you're using Visual Basic Express 2005 Beta or Visual Studio 2005 Beta, you can do this to add this class and run in a project: - Start VB Express / Visual Studio. File New Project Select Windows Application. Project Add Class Rename the name to Calculator.vb. Replace the content of Calculator.vb with the sample code above.
In this tutorial, we are going to create a basic calculator application in Microsoft Visual C# that performs the four basic mathematical operations.
Step 1: Create a New Project
First things first, create a new C# project by going to File > New and choose Windows Form Application template in order for you to create an application with a Windows Forms user interface. Change the project name into “Basic Calculator” to easily find the project later on though you can name it whatever you want. Click OK!
After creating the project, you will now have the basic Windows form in your screen. When you double click the form, it will open the program window and it should already have the basic code such as the using directive, namespace declaration.
Step 2: Declaring Variables
Though variable is not required to perform the functions of a calculator, we are still going to declare for the sake of good practice. Declare the following variables after the public partial class Form1 : Form
- operation: Save the operation to be used in the form of “+”, “-“, “*” and “/”
- firstOperand: Save the first number
- secondOperand: Save the second numbe
- answer: Save the calculated result
- clear: Determine what kind of clear function to be used. “S” for single digit and “A” for all digits.
Step 3: Build the Calculator Interface
You can now add the controls necessary to create the calculator interface using the Toolbox located in the left side of the screen. In this case, we are going to use Button and TextBox.
Add the following:
- A TextBox where you can enter numbers and show result
- Buttons that will serve as keypad containing the following:
- Numbers from 0-9
- Decimal point
- Clear function
- Four operations to be used (+. -, *, /)
- Equal button using the equals sign.
To rename a control, go to the property window in the right side and use a naming convention like below (or create your own):
- Button Number 0: btnNum0
- Button Number 1: btnNum1
- Button Number 2: btnNum2
- Button Number 3: btnNum3
- Button Number 4: btnNum4
- Button Number 5: btnNum5
- Button Number 6: btnNum6
- Button Number 7: btnNum7
- Button Number 8: btnNum8
- Button Number 9: btnNum9
- Button Decimal Point: btnDecimal
- Button Equal: btnEqual
- Button Clear Function: btnClear
- Button Addition: btnAdd
- Button Subtraction: btnSubtract
- Button Multiplication: btnMultiply
- Button Division: btnDivide
- Result Textbox: txtInput
Step 4: Program the Number Buttons (0-9) and Decimal Point
Like a typical calculator application, we would like to display the number in the textbox based from whatever button is clicked by the user. To display a value in a textbox control, we will use the
text property of the textbox. The syntax for using the said property is something like this:
There is, however, a problem if you are going to use the syntax above. Every time the the button is clicked, it will always be a single digit and the number that will appear in the textbox is the last clicked button. To fix this problem, use the syntax below instead:
Double click the Button 0 to create a
btnNum0_click event handler and add the above code between the curly braces. After adding the code, it will look like this:
Visual Basic Simple Calculator Code
The above code simply gets the initial value of the textbox first, add another digit based from whatever number button is clicked and finally display the result in the textbox. Since the value of the textbox is a String, it will only concatenate the numbers.Tip: You can also simplify the code by using a “+=” operator. The code will look like this: txtInput.Text += “0”.
Repeat the above step for all the numerical input event handlers (number buttons) as well as the decimal button event handler. Change the “0” value from the code and replace it depending which button handler you are in. Your code will now look like this:
Step 5: Program the Clear Function
The clear function will perform two different forms. The first form is to remove a single digit from a group of numbers. This second form is to remove all digits in the textbox which is normally performed after getting the result. The variable clear will determine which among the forms will be performed by the button.
- Line 3: Since the default value of clear variable is “S”, it will only remove a single digit form the right every time the button is clicked.
- Line 7: This will be activated only when the value of the variable is changed into “A” which will be coded in the equal button (after getting the answer).
Step 6: Program the Mathematical Operations
For the buttons designated to the mathematical operations, a series of actions will be needed to perform to make sure that the calculator will work as expected. This is where the importance of algorithm (step-by step procedure) will be seen. The said actions are as follows:
- Save the value of the textbox into a variable.
- Clear the textbox to give way to the next operand.
- Save the operation to be used.
Double click the Button + to create a
btnAdd_click event handler and add the code like below:
- Line 2: Save the value of txtInput textbox into firstOperand variable
- Line 3: Place an empty value in the textbox to give way to the next operand
- Line 4: Save the operation into operation variable to determine what operation to be used
Visual Basic Tip Calculator
Repeat the above step for all the mathematical operations event handlers (+, -, *, and / buttons). Change the “+” value and replace it depending which operation button you are in.
Your code will now look like this:
Step 7: Program the Equal Button
Just like the buttons intended for mathematical operations, the equal button requires a series of action as well. The said actions are as follows:
- Save the value of the textbox into a variable.
- Convert the two operands into integer.
- Determine which operation will be used and perform it.
- Convert the answer to string and display it in the textbox.
Double click the Button = to create a
btnEqual_click event handler and add the code like below:
- Line 2: Save the value of txtInput textbox into secondOperand variable
- Line 3: Place an empty value in the textbox to give way to the next operand.
- Lines 4 and 5: Convert the value of the two operands from String to Double so that it can be used in mathematical operations.
- Lines 7, 13, 19 and 25: Determine which operation to be used based from the value of the variable operation.
- Lines 8, 14, 20 and 26: Perform the operations to the two operands.
- Lines 9, 15, 21 and 27: Convert the answer to String so that it can be displayed in the textbox.
- Lines 10, 16, 22 and 28: Display the answer into the textbox.
- Line 30: Activate the clear function for all the digits.
After successfully creating the C# calculator program, it is expected that you have gained a basic understanding of how algorithm works as well as how to create a simple program in Visual C#.
As a challenge, you can add more buttons and mathematical formulas and convert this basic program into scientific calculator. You can also add design to make the calculator more appealing.
Download Basic Microsoft Visual C# Calculator
If you want to experiment with the C# code of this basic calculator, you can download this free Visual C# calculator project file below.
This month's example illustrates one of the reasons for using VBA over other options for the development of a particular class of applications inside AutoCAD. That is, it can be implemented with fewer lines of code involved than any other choice to date. VBA also stands out as a programming tool in the area of interactive input systems for AutoCAD. Programs can be quickly written to accept user input and then convert that input into graphical objects in AutoCAD.
Visual Basic .NET Language - Creating A Simple Calculator To ...
First, let's describe the application to be solved. The program we'll be developing this month is a simplified input calculator. This program will accept geometric data along with optional information to be drawn along with the points. It is expected to handle data coming in relative to a variable point. Output options include placing a label at a given point and drawing a line from the reference point to the new point. A running copy of the Input dialog box. The controls (buttons, list, text boxes and so on) in the example dialog box include labels for the text fields, five edit boxes for the input fields, a list box for selection of reference base points and three command buttons. Operator input consists of a name and either a distance/angle or x, y displacement. The options are then to accept the data point or to accept and draw a line from the relative point to the new point. Pressing the Exit button will terminate the program and return control to the AutoCAD system with all the drawing updates applied.
We've greatly simplified the demands on the programmer for this application, but it is easy to grow the program example into a powerful tool for reducing survey field notes or similar data into usable graphics. The program was modeled after a primitive Coordinate Geometry (COGO) program which essentially is a 'game' of connect-the-dots used by surveyors and civil engineers to start a drawing based on information gathered in the field. The idea is to be able to input points relative to previously defined points such as one would see coming from surveying instruments. The entry of point data in our example program will support both xy displacement and polar coordinate methods. Points can be named to coincide with survey note data, and when named, the text will appear at the datum point when the program is run.
The process involved in developing a program of this nature is relatively straightforward. Start the VBA editor with the command VBAIDE at the AutoCAD prompt and proceed directly into the definition of a new form. To define a new form, start with an empty editor, then select the Insert Pulldown menu and then the User Form option. Forms are dialog boxes. A blank form appears as an empty dialog box with a grid displayed over the top of it. We build the dialog box by selecting various controls and inserting them into the dialog box work area. When you started the form editor, a toolbar should have appeared containing various controls that can be inserted into the empty form. If you don't see that, select the View, Form options inside the VBA editor. Select the List box control (to learn what each icon does, just hold the pointer over the icon for a few seconds a tool tip help dialog will appear). After selecting the List control, move the pointer to the empty form and locate the corners of the list box. Next, locate the Properties window and change the (Name) item to be Refs. That is all there is to laying out a dialog box in VBA. Pick a control, move it into the dialog box at the position desired, then name it something you can work with in the properties tile.
Repeating the basic steps from above, insert the text boxes, labels and command buttons needed to run the program. Note that the names of the objects have been modified from the defaults created by dragging the controls into the form and then changing the property value for the (Name) of the object. Creating and editing dialog boxes is very easy in Visual Basic, but it does take a little practice to get comfortable, and once you do, you will find the development of new dialog boxes to be very quick.
After the basic dialog box has been defined, the code can be created in segments. One of the nicest features of using Basic and other interpretive language environments is that code can be developed and tested in modules without creating a large front-end to drive it. When developing this program, I started with the global declarations section and then proceeded into basic input handling. As I discovered that I needed another global variable in the input form, I simply added it to the appropriate section of code. To work in the Code View window, select the View pulldown menu and then the Code option. A full-screen text editor is presented for this form in the global declarations section of the program. Visual Basic will attempt to start things off for you by placing you in a subroutine definition for the control last referenced in the dialog editor for the form itself. Use the pulldown options at the top of the text editor to select the subroutine you want to edit or create. To create a subroutine not on the list, simply type the SUB keyword followed by the new name and press [Enter]. The END SUB will automatically appear, and you're into the middle of defining a new subroutine.
Listing 1 shows the global declarations you enter with the subroutine called when the form is initiated. To enter the global variables, select the General category in the left pulldown list and then select the Declarations option in the right pulldown. Then start typing.
Also found in Listing 1 is the subroutine UserForm_Initialize, which is called when the macro first invokes when the dialog box is started. The initialization function sets up the global variables defined in the General Declaration section. The variables in the General Declaration section of the form are local only to this form and will be reinitialized each time you restart the program. But that's a different subject that we might have to cover in the future.
Much of the required code for the basic input handling is seen in Listing 2, which actually contains several subroutines. The subroutines demonstrate how easy it is to respond to dialog-box input in VBA. The Value property of the object is simply obtained, converted to the appropriate data type via one of many BASIC conversion routines and then stored into a variable. It doesn't get much easier than that! The properties of an object tell us virtually everything we need to know about the status and details involved. For edit boxes, the data is retrieved using the Value property; however, do note that the _Change subroutine will be called each time the data field changes. Thus, you should keep field validity checking to a minimum.
The subroutine Refs_Click is the call back function for the Reference names list box control called Refs. When the user selects one of the members from the list, the Refs_Click subroutine is called. The property ListIndex tells us where in the list the user selected. This value is a variant that can be equated to an integer for access into an array. Note that we could have stored all the data in the control and parsed the data point from the text strings. This would have been more efficient in terms of storage used; however, taking the alternative approach would have required more coding, and that was a trade off we were willing to make.
Listing 3 contains the code for the two subroutines that act as call-back functions for the two command buttons for accepting or saving the data input. When the button labeled Accept is selected, the point is added to the references list, a point object is created at the specified location and if a reference name has been provided, it is output at the same point. The other command button, Accept and Draw, performs all of the functions of the Accept button but also draws a line from the reference point to the new point. Both of these functions call numerous subroutines defined in Listings 4 and 5 to accomplish their tasks.
The One of them is Model Space, which can be selected by typing enough characters to uniquely select it in the list and then pressing [Tab]. Next, pick the object you want to add, such as AddLine, and once again press [Tab] to continue on. The required parameters are then shown one by one. Generally, I just type in a variable name similar to that shown, define it and then fill it in before the call to the method to add the new object. This is extremely fast and easy to use once you get used to it, which doesn't take very long. Plus, it's a fast and easy way to learn how the interface works.
The last step required is to define a module containing a macro that is a public function (not private) that can be called by the user to start the dialog box interface. Listing 6 contains this short macro description, which simply starts the dialog box using the SHOW method of Visual Basic. That's all there is to it. Now, the operator can select the macro from the list presented in the dialog box for VBARUN after the macro set has been loaded.
If you already know BASIC or Visual Basic, it will not take long to get comfortable with VBA inside AutoCAD. It is possible to create very powerful applications using just the VBA supplied with AutoCAD R14 even though not all the features of the professional version of Visual Basic are found in VBA. The key to success is not only in learning how to best utilize this tool, but also in selecting the proper type of application for it. This article demonstrates that one suitable area for Visual Basic is in anything that involves the use of a dialog box for preparing input from one format to one accepted by AutoCAD. Visual Basic excels at that sort of activity.
How To Make A Calculator In Visual Basic 6.0
Here's a tip. Rather than keyboarding all of the VBA code we provided with these examples, please download the DVB file. Until next time, keep on programmin'!