Download Fundamentals of Programming in Visual Basic: A Beginner's Guide (Part 1) and more Study notes Engineering in PDF only on Docsity!
Fundamentals of
Programming in Visual Basic
– Part 1
Robert Burtch
Surveying Engineering Department
Ferris State University
VISUAL BASIC OBJECTS
- VB programs display Windows style
screen called a form
- Contains boxes and buttons called controls
- Forms and controls called objects
VISUAL BASIC OBJECTS
- To start – click on Microsoft Visual Studio.NET option - Opens Visual Studio start page
START PROJECT
- VB.Net also known as application, solution or project - Each project saved as series of files and subfolders in it own folder
- Start project:
- click on File|New|Project
- Alternatively, click New Project icon
INITIAL VB SCREEN
- Menu bar
- Displays commands used with VB – File, Edit, Project, Format, Debug, etc.
- Like other windows programs, but some specific to VB
- Toolbar
- Collection of icons that carry out standard operations
- Little information rectangle that pops up called a tooltip
- Main Area
- Holds Windows Form Designer
- Large stippled Form Window, or Form, becomes Window on execution
- Solution Explorer Window
- Sometimes used to open programs stored on hard drive
- Properties Window
- Used to change how objects look and read
- Toolbox
- Controls that can be placed on Form
TEXT BOX CAPABILITIES
- Double click text box icon
- Opens rectangle with sizing handles in center of form - If handles are displayed – object is said to be selected
- Rectangle can be resized by moving mouse to one of the handles
- Rectangle can be moved by holding down the left mouse key anywhere within the object
CREATING TEXT BOX
approach
- Click on text box icon
- Move mouse to any place in form
- Hold down left mouse button and drag a diagonal to create rectangle
Textbox Cursor
TEXT BOX
- Alphabetic tab display • Categorized tab display
To access Properties window, click on Properties window icon in Toolbar or click on the Properties window or press F
TEXT BOX
- Highlight Font property with single click
of mouse
- Click on ellipsis (…) to open Font dialog
box
PROGRAM EXECUTION
- To execute program, click on Start icon - Example of executed form - Text displayed in single line to right – hides text to left
TEXT BOX
- In Properties window, turn on Multiline option
- Result is that words wrap
- Can make Text box read only:
- Properties|Read Only Property|True
TEXT BOX
- Can create multiple text boxes in the
form
- One with cursor is the active box and this is called the focusfocus
COMMAND BUTTON
- Command button look at execution - Button appears to move in and out - Will add code to button - Can underline parts of command button text by adding ampersand to text - Here, “P” is called the access key – pressing Alt+P activates program like clicking on command button
LABELS
- Double click on label icon
- Places label at center of form
Label Cursor
LABELS
- Alignment can be changed by double clicking Alignment property in Properties window - Executing program does nothing to label - Just sits there - User cannot change what a label displays
LISTBOX
- Click ListBox icon
- Click and drag
location of listbox
on Form
HELP WALKTHROUGH
- Dynamic help
- Displays list of topics appropriate for what you are working on
- To Invoke: Alt | H | H
- Help Index
- Alphabetized list of all help topics
- To Invoke: Alt | H | I
FONTS
- Default Microsoft Sans Serif
FONTS
- Courier New
- Fixed-width font
- Useful to arrange columns - Wingdings - Assorted small pictures and symbols
AUTOHIDE
Pushpin vertical – AutoHide disabled
- Pushpin horizontal – AutoHide enabled
Allows you to make more room for Main area of screen by minimizing tool windows
VISUAL BASIC EVENTS
- Event
- Action by user such as clicking a control or pressing a key
- Programmer writes code to do something when event occurs
- 3 steps in creating a VB program
- Create interface – generate, position, size objects
- Set properties – configure appearance of objects
- Write code that executes an event
VB.NET EVENTS
- Properties of controls changed using following form controlName.property = setting
- controlName = name of control
- Property = one of control’s properties
- Setting = any valid setting for control property
- Examples
- txtBox.ForeColor=Color.Red
- Changes characters in text box to red
- txtBox.Text = “Hello”
- Words Hello displayed in text box
- btnButton.Visible=True
EVENT PROCEDURE
- Statements executed when even occurs
written in block of code called event
procedure
- First line of Event Procedure has form
(sometimes called Procedure-Declaration
Statement)
Private Sub objectName_event (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles objectName.event
- Often simplified as Private Sub ojbectName_event(…) Handles objectName.event statements End Sub
KEYWORDS
- Words like “Private”, “ByVal”, “As”,
“Handles” and “End” – keywords or
reserved words
- VB.NET automatically capitalizes first letters of keywords and displays it in blue
EVENT PROCEDURE
WALKTHROUGH
- Code window
- Right click anywhere on Main area and select View Code from pop-up Page tabs between Code window and Form Design window
Method Name Box
Class Name Box
EVENT PROCEDURE
WALKTHROUGH
- Rectangular area corresponds to
collapsed block of code
- Hover cursor to see information about it
EVENT PROCEDURE
WALKTHROUGH
- To enter code, double click on control
- Program will automatically take you to Code window and will enter the first and last line for the event procedure - First line: Private Sub … - Last line: End Sub
- Will write code between these two lines
- Alternatively, click on Class Name box
and select control
EVENT PROCEDURE WALKTHROUGH
- Class Name Box
- Then select desired option in Method
Name Box