Task 1.2 Working with Controls

Total Page:16

File Type:pdf, Size:1020Kb

Task 1.2 Working with Controls

Task 1.2 – Working with controls

Before we begin:

 Have you completed task sheet 1.1 (Understanding VB).

Introduction

In this task sheet you will use the following controls: Label and a Command button, a Textbox and List will be discussed next week. These are the most common controls on a Window Form. To get these controls to work for you requires an understanding of their properties, methods and events.

You may remember from creating your Form in task sheet 1.1, you were asked to name controls in the following fashion.

 FrmFirstProgram

 txtLocation

This is due to programming standards requiring us to use correct naming conventions.

Prefixes and Naming Conventions

When we add controls to a Form we must always remember to name the control correctly and to do this we use a convention of prefixes for different controls. The prefix for a label is lbl i.e. lblMessage. Using this convention for all controls make it easier to read code and identify particular controls, however, we must also give the controls meaningful names as well i.e. lblMessage etc. The prefix is always shown in lower case except for forms which always have a upper case first letter to the prefix i.e. FrmFirstProgram. Another tip to aid readability of the code is to capitalise the first letter of every word used in a name i.e. txtStudentsAddress – a textbox for inputting a student’s address (spaces are not allowed).

Page 1 of 13 Brendan Coulson Visual Studio Programming The table below shows the correct way to naming the controls that we will be using during this Unit:

Control Purpose Prefix Label Mainly used for labelling other controls lbl Textbox Used or inputting text or numbers and txt displaying results of computations Button Used for executing various actions btn Form Used as the interface, holding the controls Frm List box Used to list/display and select items in list lst format

Open your Visual Studio Project from task 1.1 by double clicking on the icon shown below:

Page 2 of 13 Brendan Coulson Visual Studio Programming Add a new Form to the project by right clicking on the MyFirstApplication icon located in the solution window and selecting Add followed by New Item.

Select Windows Form and name it FrmHelloWorld.vb

Page 3 of 13 Brendan Coulson Visual Studio Programming You should have a new form created.

Single click and draw a rectangle on the Form where you want the label to be placed.

Try and make your Form look like the one below, try a different font style, colour etc from the label’s properties Window – make sure you give the label a suitable name like “lblMessage”. You can access the label’s properties by selecting the label.

Page 4 of 13 Brendan Coulson Visual Studio Programming Draw a button onto your Form, name it btnChangeMessage.

Change the text of the button to read Change Message.

(Ensure you use the correct naming conventions, if you are unsure ask your lecturer).

Double click on your new button; you should be taken to the code Window.

You will notice that you will be presented with the button’s name followed by the event that will have to take place in order for the code to be executed.

This is the event that will have to occur for Button’s name the code between Private Sub and End Sub to be executed.

The Text property of the label is assigned (=) the new message.

Page 5 of 13 Brendan Coulson Visual Studio Programming Enter in the code shown between the Sub routine as shown above, notice the code in green that appears in the code Window. This text is called ‘Commenting’. This is a brief description written by the programmer to describe what the code / procedure etc is doing or being used for.

Also note how the actual code “lblMessage.Text = “Goodbye World” has been indented - again this is good programming practice. We will be keeping to these standards throughout the unit.

Run your first program

Before you can run your program you will need to change the start-up options to run this form and not the one you previously created.

To do this, right click on the MyFirstApplication icon located in the Solution window, followed by clicking on Properties. – See below:

Page 6 of 13 Brendan Coulson Visual Studio Programming You should now be presented by the following tab:

Change the Startup form to HelloWorld and then save and close this tab window by right clicking on the tab and selecting Close – see below:

Page 7 of 13 Brendan Coulson Visual Studio Programming To execute the program select the run button, shown below or press F5.

Run

Your Form will be activated and should look like the one below.

Try clicking the button, your message should change to: Goodbye World

Page 8 of 13 Brendan Coulson Visual Studio Programming When you selected run, the program was complied (turn into machine code) and debugged (checking for errors/bugs); each line of code was interpreted one line at a time and executed. This will be explained further to you by your lecturer using a break point which is shown below.

A break point is a useful tool, it will enable you to step through (F8) the program to check what is happening and see what variables / controls have been assigned certain values.

Variables – What are Variables? These will be discussed next lesson.

Try to create another button on the form, giving it an appropriate name to change the message back to “Hello World”

Congratulations you have written and executed your program.

Next Task – Creating a Multi Documents Interface

Create a new form (within your project solution) and call it “FrmMain”, change the Text to Main Application.

(Note: Refer to the task of a adding a form from earlier in this tutorial)

Page 9 of 13 Brendan Coulson Visual Studio Programming Once your form is created select the MenuStrip control from the toolbox.

Select and drag this control onto your form. – See below:

Click in the Type Here Box and Type Lesson One.

Page 10 of 13 Brendan Coulson Visual Studio Programming Type the names of the programs that you have created for lesson one as shown below:

Once your form is looking like the one above, double click on the Web Browser menu strip and enter this line of code (FrmFirstProgram.Show()) between Private Sub and End Sub.

When FrmMain is active, this will enable FrmFirstProgram to be displayed.

Page 11 of 13 Brendan Coulson Visual Studio Programming Do exactly the same for the HelloWorld menu strip, but this time you will be presented with the event handler for HelloWorldToolStripMenuItem - see below:

Click Event Handler for Web Click Event Handler for Hello Browser menu Strip World menu Strip

You now need to change the start-up properties like you did before so that FrmMain is the form that is displayed when this application is executed – refer to previous completed task if you can not remember how to do this.

You are required to do this at the end of each lesson- your end result may look similar to the one below:

Run your application and try out the menu strip to see if it works.

DO NOT FORGET TO ATTEMPT THE QUESTIONS - SEE BELOW:

Page 12 of 13 Brendan Coulson Visual Studio Programming Reflective Learning

Why do we name controls in the following way lblMessage?Which controls do the following apply to?

Control Prefix lbl txt btn Frm lst

What other programming standards did you touch upon during this task?

Page 13 of 13 Brendan Coulson Visual Studio Programming

Recommended publications