Hello and welcome to my YouTube channel. Today I am going to show you how to make a calculator in Visual Basic 6. So first I will show you the demonstration of this calculator application that I have created earlier. Then we will start making this calculator from scratch step by step. So all you need you have a visual basic 6 installed on your system and then we will proceed to make this calculator step by step. Now open Microsoft Visual Basic and start with the new project.
Here you have to click on standard exe and click on open button. Now here you have to first change the caption of this form. So I'm just clicking on this caption property here. I'm changing the caption.
To calculator. So this caption name calculator Will be displayed here. Now I am adding a text box control On this forum.
So this text box Control will act as a display for your calculator So I am changing the properties. Text property as Blank and the name property will be display So as you know in calculator there are total 10 digits from 0 to 9. For every digit I need a command button. For my own convenience I am going to create a control array for all these digits.
So how we can create this control array? Just click on command button and just paste it on the form. As you see there is a command button with the name command Command 1. So I am going to change the name of this particular command button to digits and the caption will be zero. Right now again I am just click on this particular command button and click on copy and again paste here.
Now you can see here a warning you already have a control named digits do you want to create a control array? Now I am going to click on yes. Once you click on yes another command button will appear here.
This command button has also the same name and the caption is also same because in the control array, control array is simply a group of related control that share the same event handler. We will discuss it later. First we will add total 10 control buttons on this forum by just clicking on copy and paste. Now we have created a control array of command button For 10 digits. Now I am just going to change the caption of Every command button.
So just clicking on the first command button I am changing the caption to 9. Same way next is 8, 1 and last one is the 0. Now I am going to add total 8 command buttons. So I have added total 8 command buttons on the form. Now I am going to change the property of every command button that I have placed.
So first click on the command 1 button and change its property. First name property will be clear button clear btn. Ok. And caption will be C. C stands for clear screen.
Next is command 2. The name will be %btn and Caption is %symbol. Command 3 here I am going to write divide btn And command caption is divide symbol. Next command 4 I am changing it to Multi multiply btn And caption is small x Next is name property is minus button Minus btn and caption is Minus symbol.
Next is Command 6 it is equal button Equal btn and caption is equal Symbol. Command 8 is Plus btn and Caption is plus symbol and command 7 is dot btn And caption is Dot symbol. Now I am going to change the Font and size property of all the command buttons So just click on the command button and go To the properties windows and here you will find font You have to select any font That you want to apply.
I am going to use stencil I am going to use stencil and size I am going to select Is 36, now I am going to change the height and width of this particular command Button, I am going to set height equal to 1000 and Width equal to 1000, ok Now apply same setting to all these command buttons After applying the settings and aligning every control Your form will look like this. Now I am going to stretch My clear button too just like this. Also I am going to stretch the command button equal.
I am going to change The caption of multiply button. So this is the caption of multiply Button. This symbol is not looks good.
So I am changing It with the symbol that I have searched from the Google So I am pasting here So you can also search for multiply symbol on Google and copy it and paste it here. I'm going to change the caption of divide symbol as well. So I am just pasting the divide symbol here.
Your calculator application look like this but it is not functional yet. So I am going to write the code to make it functional. So first we will open the code window.
So code window will be here in the project explorer. You have to click on it double click and declare Two variables before that writing here option Explicit option explicit then declare two variables Variable variable One comma variable two as Double okay and dim operation as string. So this is our three variables.
Two variables are declared as double. The variable one will store the value that you enter before clicking the any operator and this variable two will store the value after you click the plus operator. So next is OPRN is just to specify the operation. What kind of operation you want to perform on these two variables. Now we will get the caption value from this control array and display on this particular text box.
But before that I am just want to show you the text box property. Here the name property is display. So I am going to initialize the caption value of control array into the text box.
For this I am just again double click on control array and here I am going to write here display dot text. Equal to display dot text plus digits. This is the name of the control array digits index dot Caption now close this window and run your application here. You can see nine eight seven six five four three two one and zero all these values are Now displaying on the text box control. The number displayed in the text box are very small.
So first I am going to change the property of the text box. That is a font property. Just click on text box and go to the font.
This is the font. I am going to change it to select the font stencil and size equal to 36. Again run the application. Now number appears in large size. Write the code for clear button. So just simply double click on the clear button and write the code display dot text equal to blank.
It means if there is anything written in the text box when you click on clear button everything will be removed. Now test it again. I am writing 98 and when you click on C everything will be removed. So next is percentage button.
just double click on percentage button and write the following code var1 equal to value and here you have to write display one dot text close it and again here here you have to specify opr n equal to percentage symbol so this is the code for percentage symbol same way double click on the divide symbol and write the following code. I am just simply copying these two lines from here Ctrl C and paste in the divide button event and here you have to specify the divide symbol and display dot txt equal to blank. In this code any value that is displayed in the text box is initialized in the variable one and This particular symbol specify the operation and the initialization of value in the variable then the display text will be blank automatically. Next is multiply button.
Double click on multiply button and I am going to just copy the divide button code. These three lines I am just copying and paste in the multiply button event and just simply change the multiply symbol here. There is no need to change anything in this code.
Close. Now I am going to click on minus button and same here. I am going to paste the same code here again and here I am mentioning the minus symbol for minus button event.
Then I am going to click on plus button and again here I am going to paste the same code and click on plus button. After that I am going to write the code for dot button. Double click on the dot button and write the following code display.txt equal to display.txt plus double quote and dot symbol.
It means it will append the dot symbol with the value that is written in the text box. Now I am going to write the code for equal button. So double click on equal button.
And write the following code var variable2 Equal to val display dot txt And now I am going to use the select case to Select the operation I am writing here select case Oprn and now here I am going to write here case Plus and display dot txt equal to var1 plus var2. I am just copy this particular case ctrl C and here paste again here paste again paste and paste so second is minus minus Next is divide. So value will be divided.
Now next is multiply. The value of variable 1 and variable 2 will be multiplied. And last one is the percentage.
We are just simply write variable 1 into 1 upon 100. Then end the select case. Now I am going to run this application to test whether it is running correctly Or not. Here I am going to write 65 clear So now I am going to perform addition Now I am going to add one more 34 I am going to minus 2 32 I am going to multiply 7 that is equal to 224 and now divide by 5 that is 44.8 so you can perform the calculation with decimal point as well using this calculator now i'm going to change the back color of all these command button so first you have to select all these command buttons and go to the properties here is the back color here you have to select the window background and Style property here you have to select graphical one so you can see the color of every command button has been changed Now I am going to change the back color of the form So I am going to select any of the color form here You can select any of the color form your choice and now the color has been changed now I am going to add one shape control. Now.
I am going to set the properties So back color is transparent, border color I'm selecting active title and border style is one solid, fill color is go to system in active title bar and fill style is solid. And now again I'm going to add one more shape control here and apply the same settings again. Your form will look like this.
So now I'm going to run this application. Now I am doing last two settings in this calculator. First is I am changing the back color of the clear and equal symbol.
So first click on C and then I am going to go to the back color and I am set it to any color and now click on equal symbol and apply other color and click on this text box and make it it right justify so every calculation appear from the right side now this is our complete calculator application now you can simply make this application by following this video step by step so thanks for watching this video please like and subscribe to my youtube channel thank you