Computer Programming

Computer Programming » A Generic Computer Program » Behind the Scenes Programming

Behind the Scenes Programming

Visual Studio has multiple tabs, the design tab and the code tab. We have spent our time so far in the design tab. We will explore the code tab next.

The code is part of our project. The name of our application is WindowsApplication1. The name of our window is Form1. Even though we changed the name that is displayed, we did not change the name of the thing, just what is displayed. We changed the text property not the name property.

For now, we will call the class a means to name something, so the Form1 name is important as when we type in our code which we will get to soon, we shall see how that name is important.

And remember that same name, Form1, is part of our application, so it is all linked together. The good news is that this is not something to be too concerned about as if we change the name from Form1 to something else, all the places that say Form1 will be automatically changed for us. Thanks Visual Basic! Otherwise, it would be quite a nightmare to keep track of it ourselves.

Double click on the button and the code to handle what happens when the button is clicked will be our focus.

Notice the bold Button1 at the top of the properties sub-window, which helps us to remember that the name of this element is Button1.

Select the textbox.

Notice that the name of the textbox is TextBox1.

Double click on the button to bring up the code that controls the behavior of the button.

Put the cursor after the line that has Private Sub and before the line End Sub. We will put our code here. The location of the code is important.

We will enter code that will change the text property of the textbox. TextBox1.Text = "Everywhere that Mary went the lamb was sure to go". The name of the textbox is TextBox1. Since we are changing the Text property, we use that name of the property. The equal sign is used as a means to do assignment, which is the programming term for changing the value of a property. The new value is on the right hand side of the equal sign, and the property to get the new value is on the left hand side.

Now run the program. The original text that was put into the text box via the property window in the design tab is shown when the program initially runs.

Click the button, and the text in the text box will change because of our assignment statement that changes the value of the property. If you click a second time, the text remains the same even though what is actually happening is that the assignment statement is run again, but since it is replacing the text with the exact same text, there is no visible change.

We can change in the code the properties that we changed in the designer. Again, we use the assignement with the equal sign. The property to change is on the left side, and the new value is on the right. The property is actually the name of the element with a period and then the name of the property. We can change the multiline property and the readonly property of the text box.

All done.

⇐ Prev Chapter
Adding a button

Next Chapter ⇒
Saving the executable program

Computer Programming for Everyday People
Michael Bigrigg

Amazon eBook

Powered by w3.css