Вы находитесь на странице: 1из 21

Lecture 2: Making Simple Programs

with VB.NET
Outline
 Review:
 Flow chart for project preparation
 Today, we will practice making simple programs
 Program A – Display Time and Date on Demand
 Goal: display the Time and Date,
 On request, by click of a button.
 Program B – Display the Current Time/Date Automatically
 Goal: Add a ‘Timer’ to the program,
 To make it update automatically.
 Creating these programs will provide:
 Practice in using the techniques in Lecture 1
 A basis for making our next pair of programs (Lecture 3).
Flow Chart for Program Preparation
A. Building a Program to Display the
Date and Time on Demand
Step 1: Program Creation
 Open Visual Studio to create a new VB Project.
 Create a Windows Application.
Step 2: Form and Controls Arrangement
 Create a Form with four labels and two buttons.
 Using the tool box, create each as in Lecture 1.
Step 3: Setting Control Properties
Step 4: Writing the Program Code
 Double-click on each button, to add code…
 Using the Code Editor, write code for each button, as shown.
 Double-clicking on each defines a Sub-routine (an ‘event handler’)…
 Which is then encoded by hand to handle a corresponding click event.
Step 5: Program Testing
Flow of Program Execution
 Our program behaves as a very simple “automaton”:
 It initializes once, then enters a ‘waiting’ state…
 Upon input (a <click> event) 
 It wakes, and executes the corresponding code once;
 Then returns to the waiting state…time is not really ‘current’.
B. Building a Program for Automated
Display of the Time/Date
Step 1: Program Creation
 Open Visual Studio to create a new VB Project.
 Create a Windows Application.
Step 2: Form and Controls Arrangement
 Create a Form with four labels and one command button
 Using the tool box, create each as in Lecture 1.
Step 3: Setting Control Properties
Adding a Timer
 Now we add a Timer:
 ‘Drag and Drop’ the timer to the Form from the Toolbox…
 It will then appear beneath the Form
 Set the timer properties using the Properties Window

Enable the timer ( note: the default Interval = 100 ms )
Step 4: Writing the Program Code
 As usual, double-click to add code…using the Code Editor.
 However, code must be written for both Timer1 and Button1.
 Double-clicking on each defines a Sub-routine…
 Which is then encoded by hand to handle the corresponding tick or click .
Step 5: Program Testing
Flow of Program Execution
 Our program now behaves as a synchronous Automaton:
 It initializes once, then (briefly) enters a ‘waiting’ state…
 The Timer periodically (each 0.1 sec) provides an internal ‘wake’ event.
 Waking the automaton, which executes the corresponding code once;
 Our Automaton then returns to the waiting state….until woken again.
Formatting the Displayed Time
 The time display can be formatted differently…
 Using the Format (object, string) function:
 Object = what is to be formatted;
 String = specifies the format: how the object is to be formatted.
 To format a Time, use the following characters:
Customizing the Time Display (cont.)
 Some examples:

 Note the great ease of inserting Japanese characters into a string.


Conclusion
 In this lecture, we have used VisualStudio .NET to construct two
simple Programs:
 A. On-demand Display of the Time/Date
 B. Automated Update/Display of the Time/Date
 And learned how to set and use a Timer.

 Next lecture, we will continue our development with two new


programs:
 An Alarm Clock
 A simple Slot Machine

 With the remainder of the lecture, you should practice:


 Open VB .NET and get familiar with the GUI;
 Try to make the simple program we discussed.
 Test, save, and verify creation of the project files.

Вам также может понравиться