LAB - 02 - Writing Simple Visual Applications
LAB - 02 - Writing Simple Visual Applications
Learning Outcomes
This Labsheet encompasses of activity 2A,2B, 2C and 2D
Activity Outcome:
Theory/ Topics
The first thing to note about visual applications is that they are event-driven. This means that
the application appears to do nothing until "something happens" (ie an event occurs) then a
piece of code is executed before the application appears to go back to doing nothing.
Different pieces of code are executed for different events. An event can be something like a
button click, the mouse pointer entering or leaving a control, a key up or down, etc. Thus at
first sight a visual application seems to be a collection of separate little bits of code.
DFP4013 Visual Programming
Activity 2A
Activity Outcome: Creating an interface to add two numbers.
Procedure :
This is a very simple application. The only time we want anything to happen is when the
button is clicked. In other words - the only event that needs to be handled is the button click.
Thus all the code for this application will be in the button click event handler.
Double click on the button to open its click event handler as shown:
Try entering this single line of code inside the event handler:
Run the application and try it out with a simple sum (use integer values). Do you get the
expected answer? What you actually get is:
It's actually concatenating the two numbers rather than adding them! The "+" operator is
overloaded, ie it does different things depending on the types of its operands (the operands
of the "+" operator are the values that come before and after it). If the operands are of any
arithmetic type then the "+" operator will perform an addition on them. However if the
operands are strings (the .Text property is a string) it performs a concatenation. Note the
concatenation operator is "&" but "+" works as well. It's probably best to use "&" to show
explicitly that you mean concatenate rather than add. This may help when doing code
maintenance.
So how do we make the application ADD the two numbers? Replace the line of code from
above with the three below:
In line 1 a local variable of type Integer is declared and initialised with the value of the Text
property of the first TextBox. The Text property is a string but is automatically converted to a
integer on the assignement.
Line 3 uses the "+" operator with two integer parameters hence it performs an add. The sum
is then assigned to the Text property of the sum TextBox. The sum is automatically
converted from an integer to a string on the assignment. The following shows this working:
The code we have just written isn't idiot-proof. It assumes the user will enter integer values
into the first two TextBoxes (a big assumption); what if she doesn't?
You will often see a naming convention for variables where int Integer
a type specific prefix is put on the variable name. In the lng long
above example the local variables were integers so the obj Object
prefix "int" was added to indicate this. Beware, using such sng Single
a prefix is for documentation only and doesn't affect the str String
type of a variable. A list of the common prefix's used is var Variant
shown. This convention is less rigorously applied than that
for the names of controls.
Activity 2B
Activity Outcome: Creating an interface to calculate the circumference and area of a circle
Procedure :
Some very simple code for the button click event handler is shown:
The first line takes the (hopefully) numeric string in the radius TextBox and automatically
converts it to the floating-point type double. The next two lines do the calculations for the
circumference and the area respectively. In both these lines a value of type double is
automatically converted to a string and displayed in the TextBoxes. The answers shown are
correct but are shown to too many decimal places to be easily read. You will see how to
format a string to a specific number of decimal places when we look at the String class in
detail.
Sequence of Statements
Within a piece of code (eg an event handler) the individual statements are normally
executed in the order that they are listed. This is the default flow of control within a program.
To change this there are also "control structures" for decision (selection) and for repetition
(iteration). These are special statements that alter the flow of control. We will look at these in
detail in the following two labs. This next example shows how the sequence of statements is
important.
DFP4013 Visual Programming
Activity 2C
Activity Outcome: Creating an interface to enter a sum of money and the minimum
combination of coins to make up the sum is calculated.
Here's a simple application where the user enters a sum of money and the minimum
combination of coins to make up the sum is calculated:
Procedure :
Start a new project and design the user interface, naming the button cmdCoins, the TextBox
txtIn, and the labels lbl1 through to lbl100.
All the code for this application is in the Button Click event handler (line numbers have been
added to aid the description that follows, these are not part of the code):
Line 1 declares a local variable of Integer type and initialises it to the value in the TextBox
multiplied by 100. The value in the TextBox is a string which is automatically converted to a
floating-point value (assuming the user enters a number with a decimal point), multiplied by
100, then rounded to the nearest integer when assigned to intChange. Thus at this point the
variable intChange contains the number of pence to be given out as coins.
DFP4013 Visual Programming
The task now is to work out the minimum combination of coins to give out this change. The
strategy to do this is to divide the change by the value of the highest available coin, then
what remains by the next highest, then the next, and so on.
Line 2 does an integer division of the change by 100 (the highest available coin). This will
yield the number of £1 coins to be given out. This value is written to the appropriate label
concatenated with some explanatory text.
Line 3 works out how much change is outstanding after the £1 coins have been given out. It
does that using the Mod operator which returns the remainder of an integer division.
Lines 4 & 5 work in a similar way to lines 2 & 3 but for 50p coins. Similarly for the rest of the
code. Note that the value in intChange goes down as the numbers of coins of each value are
given out. The important thing to note about this piece of code is that the order of the
statements matters!
Do a "paper and pencil" execution of this code. Choose a value for the user to enter
(something other than £3.46). Go through the code above pencilling in the value of the
variable change as each statement is executed in sequence.
Put the code above in the button click event handler and check that the values you worked
out above are calculated correctly. This isn't the only way of implementing a change
machine application; most applications can be done in many different ways.
DFP4013 Visual Programming
Activity 2D
Activity Outcome: Creating an interface and apply the usage of tooltips.
Procedure :
Tooltips are a feature of some Windows applications (eg Visual Studio .NET) where a little
bit of text ( ie the Tooltip) is displayed when the mouse pointer hovers over something
significant. In our VB .NET applications we have the facility to create ToolTips for when the
mouse pointer hovers over a control. Take the Change Machine application as an example:
To create ToolTips for many controls on a form we need just one ToolTip
control (find it in the Toolbox). The ToolTip control is non-visual (ie can't be
seen at run-time), so place it anywhere on the form and it will appear at the
bottom of the form designer.
After seeing the tooltip work, check out the effect of changing
the IsBalloon property on the ttpCoins to True.