1 Advanced Computer Programming Lab Calculator Project
2 How to use Variables in VB.NET Learn how to transfer the contents from one textbox to another Learn how to transfer the text from a label to a textbox
3 Lets Start!! Start a new Visual basic project. You should have a plain grey Form on your screen. By default it will be called Form1. Click the Name property in the Properties window. Change the Name of the form to frmVariables. Set the Text property of the Form to "Transferring information". You can choose any background colour you like for the form, or leave it on the default.
4 Change Control Properties Put the following controls on the Form, and change their properties to the one's specified below (NOTE: lbl is short for label) Textbox Name: txtVariables Font: MS Sans Serif, Bold, 10 Text Delete the default text "Text1" and leave it blank Label Name: lblTransfer BackColor: A colour of your choice Text: Label Caption Font: MS Sans Serif, Bold, 10 Button Name: btnTransfer Text: Transfer to Textbox
5 Coding…. (Transfer text from a label to an empty textbox) Double Click on the button to bring up the code window. You will write your code in this window Declaring Variables Dim LabelContents As String Assigning a value to the variables LabelContents = lblTransfer.txt The next line of code simply puts what ever text is written in the label into the textbox txtVariables.txt = LabelContents
6 Output
7 Coding…(Transfer text from the textbox to a label) Now we want to transfer the text in the textbox to a label Add a new button to your form and change its text property to “Transfer to Label” The code for this is similar to what we have done above Declaring the variables Dim TextboxContents As String Assigning a value to the variable TextboxContents = txtVariables.txt The next line of code simply puts what ever text is written in the label into the textbox lblTransfer.txt = TexboxContents
8 Exercise A button also has a Text Property. Write code to transfer the Text property of a button to the Textbox. It's probably better for this exercise to create a new Button. Set its Name property to whatever you like. And give its Text Property a new value (The Text property will be Button1 by default).
9 Calculator Project Create a new project called Calculator The following controls need to be added to the form Buttons Name: btn Plus a Number (btnOne, btnTwo, btnThree, etc) Text: A number from 0 to 9. A different one for each button, obviously Font: MS Sans Serif, Bold, 14 Textbox Name: txtDisplay Font: MS Sans Serif, Bold, 14 Text: Erase the default, Textbox1, and leave it blank
10 Command Buttons Plus Button Name: cmdPlus Font: MS Sans Serif, Bold, 14 Text: + Equals Button Name: cmdEquals Font: MS Sans Serif, Bold, 14 Text: = Clear Button Name: cmdClear Font: MS Sans Serif, Bold, 14 Text: Clear
11 Calculator
12 What Shall Happen? So if you wanted to add 5 + 9, you would click first on the 5. A 5 would appear in the textbox. Then you would click the + symbol. The 5 would disappear from the textbox. Next, click on the 9. The number 9 would appear in the textbox. Finally, click on the = symbol. The 9 would disappear from the textbox, and the answer to our sum would replace it. We would then click the Clear button to clear the display.
13 Calculator Code in VB.NET Declare two variables named total1 and total2 These variables shall store the numbers which have to be added together We shall declare the two variables at the top of the code below the line that begins “Public Class Form1” So after declaring the code will look like; The Single in the above code means “Single-Precision” For scientific calculations use “Double”
14 Code for 0-9 Buttons The Buttons with the Text 0 to 9 only need to do one thing when the button is clicked - have their Text Properties transferred to the Textbox. You've already wrote code to do that. So double click the 0 Button and enter the following code: Private Sub btnZero_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnZero.Click txtDisplay.Text = btnZero.Text End Sub This code will transfer the Text Property of a Button called btnZero to the Text Property of a Textbox called txtDisplay. There is a problem with this code?????
15 Code for 0-9 Buttons Problem in the code: What happens when you transfer the number 2 to the Textbox, and then click the number 3? The number 2 will disappear, to be replaced by the number 3. if you actually wanted the number 23 in the Textbox. With this code, you could have either the number 2 in the Textbox or the number 3, but not both! Solution: What we need is a way to get whatever is in the Textbox to stay where it is, and not disappear on us when we click a different number. For this the code will transform into something like: txtDisplay.Text = txtDisplay.Text & btnZero.Text Note: This code has to be written for all of the 10 buttons
16 Coding for the Plus Button When we click the Plus Button, we want two things to happen –Store the number that is currently in the textbox –Clear the textbox To store the number that is currently in the textbox we have already defined a variable which does just that i.e. Dim total1 As integer So in order to store the number in the textbox our code will look like this; total1 = txtDisplay.Text
17 Coding for the Plus Button The above code will only put the value in the textbox in the variable named total1. But if we want to add three or four numbers together like … If we don't keep a running total, there's no way for our program to remember what has gone before, it will just erase whatever is in total1 and then start again. We can do something similar if we want to retain a value that is already in a variable variable1 = variable1 + 1 The "= variable1 + 1" part just says "Remember what is in the variable variable1, and then add 1 to it.
18 Coding for the Plus Button Using the code above we can write our code for the plus button as; total1 = total1 + Val (txtDisplay.Text) The Val( ) part just makes sure that a number in a textbox is kept as a number, and not as text So now we are saying that “The variable total1 contains whatever is in total1 added to the number in the textbox. Clear the Textbox To clear the textbox just write the code as; txtDisplay.clear()
19 Whole Code for Plus Button Private Sub btnPlus_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnPlus.Click total1 = total1 + Val(txtDisplay.Text) txtDisplay.Clear() End Sub
20 Exercise Write the code for the equals button