|
Free
computer Tutorials
|
![]() |
home |
Stay
at Home and Learn
|
||||||
Using Variables in your NET Code |
|||||||
|
How to Use Variables in VB NET
In this next section, we're going to learn how to transfer the contents of one textbox to another textbox. We'll also learn to transfer the text from a label to a textbox, and whatever was in the textbox we'll transfer it to a label. This will get us a little more practise with variables, and how to use them. Ok, start a new Visual basic project. You should know how to do this by now, and what the design environment looks like. But you should have a plain grey Form on your screen. By default it will be called Form1. Make sure the Form is selected (has it got the white squares around it?), and the 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. 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 Label Name: lblTransfer Button Name: btnTransfer The height of your controls is entirely up to you. If you double click your Button to bring up the code window, you will see that the first line of the code no longer says Button1_Click (etc). The first line should say this Private Sub btnTransfer_Click(ByVal
sender As System.Object, _ End Sub The reason it has changed is because you changed the Name property of the Button. The button now has the Name btnTransfer. If you wanted to, you could change the Name property back to Button1. Then when you double clicked the button, the code window would pop up and the first line would be Button1_Click(etc ). What we're going to do now is to transfer the Text on the label ("Label Caption") to our empty textbox. And all with the click of a button. As you'll see, there isn't much code. Type the following into your code window: Dim LabelContents As String LabelContents = lblTransfer.Text txtVariables.Text = LabelContents Before you Run your programme, you need to change the Start-Up form. So right click on the name of your project, and select Properties from the menu, as in the image below:
Alternatively, click the Project menu. From the list, select Properties at the bottom (it will be prefaced by the name you gave to your project - Transfer Information in our case.) When you get the Property Pages dialogue box, change the Start up object to frmVariables:
Click OK to exit the dialogue box. Now Run your programme and test it out. When you click on the "Transfer" button, you should see that the contents of the label will be inserted into the textbox. But let's break the code down and see what's going on.
No more reading these lessons online - get the eBook here!
And with three lines of code we can transfer text from a label to a Textbox. But can we do it the other way round? Can we transfer whatever is in a Textbox to a Label? Well, sure we can. Add another button to your form. Change its Name property from Button1 to cmdTransferToLabel, and change the Caption property to "Transfer To Label". Again, there's just three lines of code. So double click your new button to bring up the code window. Then type in the following code: Dim TextBoxContents
As String Now, see if you can work out how it works. It's the same thing as the first three lines of code: set up a variable, transfer the Text property of the Textbox to the variable, transfer the variable to the Text property of the Label. Run your programme and test it out. Type something into the Textbox, and then click the "Transfer To Label" button. ExerciseA 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) . But the process is exactly the same as the two bits of code above - you should only need 3 lines of code for this exercise.
In the next part, we'll start a Calculator project. This will get you some more practical experience of working with variables.
<-- Back One Page Move on to the next part --> |