Class Notes

advertisement
Class 1 (Sep 4)
Windows Applications
Window applications all share some general features
our programs will be easier to use if they resemble other Windows programs
use Word to illustrate:
minimize/maximize/close buttons
menu bar with shortcut and access keys
toolbar
horizontal and vertical scroll bars
dialog box (e.g., New File, Print)
standard controls – open the Print dialog box
Button
Spinner
ComboBox
OptionBox
CheckBox (click on Options)
TabControl (also in Options but only one choice – also see Font Dialog box)
We can create programs with these features without writing any program code
of course, we do need code to make each feature work
History of Visual Basic
BASIC – Beginner’s All-Purpose Symbolic Instruction Code
Developed in the mid 1960s by Kemeny and Kurtz at Dartmouth
Designed to be a language for novices
Became the standard language for early PCs
For Windows to be successful, MS needed a language to easily create Windows apps
Began as a shell language for interface design (Alan Cooper’s “Tripod”)
Renamed “Ruby” it was intended as a part of Windows 3.0 but dropped
Later became known as “Thunder” and is considered the first RAD tool
Bill Gates had apparently promised a BASIC language compiler so that became the base
for the language
VB1 was introduced in May 1991 at Windows World Conference
VB3 allowed database support and that made the language much more popular
VB .Net has moved to an object-oriented version
Find more info at: http://msdn.microsoft.com/vbasic/bday/default.asp
How does Visual Basic differ from C++ and Java?
First, the languages themselves are quite different
different syntax
Java and C++ are object-oriented and VB is too (but only with VB .Net)
As with C++, VB does not force you to do OO programming
1
15-Feb-16
Visual Basic is designed for only one platform – to create Windows-based applications
there is no Mac or Unix version of VB
fortunately, for Microsoft and VB programmers, we live in a Windows world
Visual Basic has only one vendor - Microsoft
there are no competing versions of VB available from other companies
nothing around is even similar (Delphi might be the closest)
Java is in the public domain and changes are managed
Visual Basic for Applications is a closely related language used within Office for macros
also now used by WordPerfect and Visio
latest version is a bit faster and offers better security
other products are adopting it as well
but VBA is more like VB6 than like VB .Net
What will happen with the upcoming VBA is still uncertain
Visual Basic Script is also used for Internet applications
But this is not the same as Visual Basic (even earlier versions)
What is .Net?
The .Net framework is more than just the latest version of Visual Basic
Visual Studio .Net includes C++, C# and VB (a version of Java is to be added later)
Consistent IDE (Integrated Development Environment) across these languages
Different languages can be easily used in a single application
Also supported by versions of APL, COBOL, Fortran, Pascal, Smalltalk, Python
This has some major implications for development
programmers can work in whatever language they prefer or know best
code written in one language can be easily integrated later (i.e., reused)
The .Net architecture is planned to exist on multiple platforms
different hardware and operating systems
this includes other devices such as PDAs and even cell phones
Designed for the Web (topic to be covered in a new course)
Getting Started with Visual Basic
Opening a Visual Basic Project
From the Start menu, click on New Project
Then select Visual Basic Projects (left side) and Windows application (right)
Change the name to something more meaningful (e.g., TestProgram)
Change the location (path)
Show the Visual Basic Integrated Development Environment (IDE)
2
15-Feb-16
Menus (View Toolbar, Standard and Layout are normally checked)
Form
Toolbox
Solution Explorer
Properties
Code (editor)
If any of these features are missing, they can be restored using the View menu
Form (Design Mode)
unlike most older languages, what you see is not a screen for text entry of code
the empty Form on the screen will become the Form the user sees
Forms have Properties (should be displayed in the bottom right hand corner)
Change the BackColor
There are three options – Custom, Web and System
Web and System are set to match the Windows settings of a particular computer
This is great if you want your program to follow the user’s settings
But not good if you want fixed colors
Normally, a BackColor will come from Custom
Add a few controls from the Toolbox to create interface objects
Single-click a control and then use the mouse to draw it on the screen
Double-click and it will appear in the top left corner
Unless you already have another control selected
Running a VB program
To run the program (any of these will work)
From the menu, Debug / Start
F5 shortcut key
The Form should now be displayed in its own window
This may not look like much but a lot has already happened
a Window has been created on the screen with a border
its size and color are determined
it has a TitleBar with an icon and caption (Form1)
it has buttons in the top right corner for minimizing, resizing and closing the Form
and they all work!
Some of this has been created as code for you automatically (you can see the code later)
Other parts are built in to VB and happen in the background
just like most languages calculate square roots
you use a function call and never see the code that makes it work
What happens when you run a Visual Basic .Net program?
3
15-Feb-16
Visual Studio .Net uses a two step compilation process
First step compiles code into Microsoft Intermediate Language (MSIL)
So does C++, C#, etc.
In theory, a C++ and a VB program could produce the same MSIL code
In practice, it is at least fully compatible
The Common Language Runtime (CLR) takes the MSIL code and produces machine code
Machine code is designed for a particular platform (operating system)
The CLR can combine MSIL code from different languages
This permits language interoperability across platforms
What are Objects and Controls (in Visual Basic)?
There are two types of basic objects
Forms (not a Control)
controls that go on Forms (TextBox, Label, etc.) to become objects
Controls are pre-built elements that go into Forms
most look like things you've seen in other Windows programs
you can also build your own controls (or buy them)
this greatly improves programmer productivity
VB programs are created by placing controls on Forms and then adding code so they respond to
different events
The ToolBox shows the commonly used controls
Focus
What does it mean to have the focus?
When a object has the focus, events are interpreted in the context of that object
Text can only be entered into a TextBox if it has the focus
Enter keys can trigger a Button event, if it has the focus
Only one object can have the focus on a Form at any one time
Bring up the Print Screen in Word and tab through the different objects
What are Properties?
Each Control has a set of Properties
properties are attributes of objects, e.g., color, position, name, etc.
Properties can be seen and changed in Design View
but Design View cannot change values while a program is running
Properties can also be read or changed in code (e.g., make a TextBox disappear)
Drag any control on to the Form and see the list of properties (Properties Window)
4
15-Feb-16
What are Methods?
Controls also have methods - things they can do
Method names are usually action verbs (Move, Print, …)
Methods are not available through the Design View, must be coded
e.g., most controls can be moved (e.g., Text1.Move)
To see some possible methods, double-click on a TextBox object
VB should display the code window (with not much in it)
Type the name of the object (e.g., Text1) followed by a period
should get a list of all possible properties and methods
Drag and Move are two methods but most possibilities are properties
note that some properties can only be set in code and not at design time
so all new things in the list are not necessarily methods
select Drag and use F1 to show help (confirms it is a Method)
Methods are very important to object-oriented programming
What is an Event?
An Event is something that happens to an object and it informs the program
e.g., a mouse click, key strokes, etc.
Events are interpreted by whatever object has the focus
if a TextBox has the focus, as keys are typed, characters appear in the TextBox
Controls have more events associated with them than you might expect
double-click again on the TextBox (if you aren’t already in the code window)
the Class Name ComboBox (top left side of code window) will list the TextBox
all controls should be listed
select the TextBox
the Method Name (right-side) ComboBox (on top of the code window) lists all events
If you don’t write code for an event, nothing happens when it occurs
this is normal – even complex programs have code for only a small portion of possible
events
The events for Forms can also be seen
Select Base Class Events in the Class Name list
The Method Name list now contains Form events
What is the difference between an Event and a Method?
a Method is an action you instruct an object to take
an Event is something that occurs
whether any action is taken in response to an event depends on the code
5
15-Feb-16
Download