Save and test that the display is cleared when you enter a number and click on one of the arithmetic buttons. Repeat this process for the other 3 buttons, remembering to change the operation to 2, 3 and 4 for the different arithmetical operations. The remaining statements perform the tasks listed in the bullets. The IF statement checks that the label is not empty. Set the operation to 1 (our code for Add).Store the fact that a number was entered.Check that there is a number in the display.The code window appears with the cursor blinking in the place we need to type. Go back to the form design and double click on the Add button. Go to the Code window by clicking View, Code on the menu and enter the declarations you see below. This will be a true/false value so we will use a Boolean variable. This lets us know that we should do a calculation when the equals button is clicked. This number will be a double in case the user entered a decimal.įinally, we need to store the fact that a first number has been entered. It's normal for the display to be cleared ready for the second button. We will also need to store the number that was in the display when they clicked a button. When the user clicks on one of the arithmetic buttons we will need to store which one they have pressed, we will store a whole number to represent this with the numbers being, We are going to have to declare some variables on the form to make our calculator work like a standard calculator. Make sure you sort out the Text so that your buttons look like the ones in the screenshot. Copy and paste one of your digit buttons and add four buttons to the form. Now we need to add the buttons to do the arithmetic. We can hide this by clicking on the minus sign to the left of the procedure name. Save and test that there is now a limit on the number of digits that can be entered.įor now, we have finished with the code we need for the digit buttons. We can do this by adding an IF statement to the code we have just written. It would be sensible to limit the number of digits that can be entered to say, 10. Step 8Ĭalculators can't work with super huge numbers and ours will be no exception. Save and test your program to make sure that all of the buttons behave as they should. We need one line of code, lblDisplay.Text = lblDisplay.Text + sender.Text The cursor will be blinking away in the place we are going to need to write our code. Look for the event named click and double click in the empty box to the right of it.Ī code window should appear with something like you see in the screenshot. We want to look at events - so click on the lightning symbol. Start by selecting all of your buttons - you can do this by drawing a selection rectangle around them all. Clicking on a button will add the number that is on the button into the display. It makes quite a difference to how easy your application will be to use if you lay the buttons out neatly as you see in the screenshot. Then you should have something like this,Īt this point you have a sense of how large the form needs to be and can tidy up the size. Don't forget to sort out their names and text. Change the font, size and colour until you are happy.Ĭopy and paste your 7 button until you have buttons for all of the digits 0-9. Change its name to btnSeven and its text to 7. Start by adding one of the number buttons, say the 7 button. Our next job is to add the number buttons. You should be able to do this by looking in the properties window when you have the label selected on the form. You will also need to add a border to the label, set its background colour to white, remove the text and make the text right aligned. Then look for the property called Autosize. Start by looking in the properties window and choosing a font and size you like. Notice when you first make it, however large you try to make it, it will shrink to the size it needs to be to display the text Label1. The user can't enter text directly in here so the best thing for us to use is a label. The large box at the top is to display the numbers we enter and the answer to any calculations we make. There is a button for each of the digits from 0-9, one for the decimal point, one for equals and a button for each of the arithmetical operations (+-*/) that we want to do. Most of the things in the sketch are buttons. Step 2Ĭhange a few properties on the form to make it look more like a calculator program - change the Text to Calculator and change the FormBorderStyle to FixedToolWindow.Ī quick sketch helps us think about the features we should see in a calculator program - we'll start with the simplest things. From now on you can just click the Save All button to ensure you have saved your work. Launch Visual Basic and create a new Windows Forms Application called Calculator.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |