Using Variables in your NET Code

Started by sukishan, Jul 11, 2009, 06:12 PM

Previous topic - Next topic


How to Use Variables in VB NET


Name: txtVariables
Font: MS Sans Serif, Bold, 10
Text Delete the default text "Text1" and leave it blank


Name: lblTransfer
BackColor: A colour of your choice
Text: Label Caption
Font: MS Sans Serif, Bold, 10


Name: btnTransfer
Text: Transfer

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, _
ByVal e As System.EventArgs) _
Handles btnTransfer.Click

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

Your code window should now look something like this:

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!

Dim LabelContents As String
Here is where we set up a variable called LabelContents. Because it will be holding text, we've used the variable type As String.

LabelContents = lblTransfer.Text
Here is where we put something into our empty variable. We changed the Name property of our Label from the default Label1 to lblTransfer. A Label has lots of properties you can manipulate. One of those properties is the Text property. After you typed the word "lblTransfer" and then typed a full stop, you probably saw a drop down box appear. Inside the box is a list of all the properties and Methods that a Label has. We wanted to manipulate the Text property of our label so we selected the word Text after the full stop. So we were saying "Access the value of the Text property of the label called lblTransfer, and put this value into the variable called LabelContents." Because our Text was ""Label Caption", the variable LabelContents now holds the text "Label Caption."

txtVariables.Text = LabelContents
Finally, we want to transfer whatever is in the variable LabelContents to the Textbox. Our Textbox is called txtVariables. Again, after typing the full stop the drop down box would appear, showing you a list of all the properties that a Textbox has. The one we're interested in is the Text Property. So we're saying, "Take whatever text is in the variable LabelContents, and transfer it to the Text property of the Textbox called txtVariables.
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

TextBoxContents = txtVariables.Text

lblTransfer.Text = TextBoxContents

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.
A good beginning makes a good ending


by using variable we can easily in tthis code
this should be always in .net code
this can many variable in to your net code

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:
Shortcuts: ALT+S post or ALT+P preview