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

Structure of a Visual Basic Program

A Visual Basic program is built up from standard building blocks. A solution comprises one
or more projects. A project in turn can contain one or more assemblies. Each assembly is
compiled from one or more source files. A source file provides the definition and
implementation of classes, structures, modules, and interfaces, which ultimately contain all
your code.s.

File-Level Programming Elements


When you start a project or file and open the code editor, you see some code already in place
and in the correct order. Any code that you write should follow the following sequence:

1. Option statements
2. Imports statements
3. Namespace statements and namespace-level elements

If you enter statements in a different order, compilation errors can result.

A program can also contain conditional compilation statements. You can intersperse these in
the source file among the statements of the preceding sequence.

Option Statements

Option statements establish ground rules for subsequent code, helping prevent syntax and
logic errors. The Option Explicit Statement ensures that all variables are declared and spelled
correctly, which reduces debugging time. The Option Strict Statement helps to minimize
logic errors and data loss that can occur when you work between variables of different data
types. The Option Compare Statement specifies the way strings are compared to each other,
based on either their Binary or Text values.

Imports Statements

You can include an Imports Statement (.NET Namespace and Type) to import names defined
outside your project. An Imports statement allows your code to refer to classes and other
types defined within the imported namespace, without having to qualify them. You can use as
many Imports statements as appropriate. For more information, see References and the
Imports Statement.

Namespace Statements

Namespaces help you organize and classify your programming elements for ease of grouping
and accessing. You use the Namespace Statement to classify the following statements within
a particular namespace. For more information, see Namespaces in Visual Basic.

Conditional Compilation Statements


Conditional compilation statements can appear almost anywhere in your source file. They
cause parts of your code to be included or excluded at compile time depending on certain
conditions. You can also use them for debugging your application, because conditional code
runs in debugging mode only. For more information, see Conditional Compilation.

Namespace-Level Programming Elements


Classes, structures, and modules contain all the code in your source file. They are
namespace-level elements, which can appear within a namespace or at the source file level.
They hold the declarations of all other programming elements. Interfaces, which define
element signatures but provide no implementation, also appear at module level. For more
information on the module-level elements, see the following:

 Class Statement
 Structure Statement
 Module Statement
 Interface Statement

Data elements at namespace level are enumerations and delegates.

Module-Level Programming Elements


Procedures, operators, properties, and events are the only programming elements that can
hold executable code (statements that perform actions at run time). They are the module-level
elements of your program. For more information on the procedure-level elements, see the
following:

 Function Statement
 Sub Statement
 Declare Statement
 Operator Statement
 Property Statement
 Event Statement

Data elements at module level are variables, constants, enumerations, and delegates.

Procedure-Level Programming Elements


Most of the contents of procedure-level elements are executable statements, which constitute
the run-time code of your program. All executable code must be in some procedure
(Function, Sub, Operator, Get, Set, AddHandler, RemoveHandler, RaiseEvent). For more
information, see Statements.

Data elements at procedure level are limited to local variables and constants.

The Main Procedure


The Main procedure is the first code to run when your application has been loaded. Main
serves as the starting point and overall control for your application. There are four varieties of
Main:

 Sub Main()
 Sub Main(ByVal cmdArgs() As String)
 Function Main() As Integer
 Function Main(ByVal cmdArgs() As String) As Integer

The most common variety of this procedure is Sub Main(). For more information, see Main
Procedure in Visual Basic.

VB.Net - Decision Making


Decision making structures require that the programmer specify one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.

Following is the general form of a typical decision making structure found in most of the
programming languages −

VB.Net provides the following types of decision making statements. Click the following links
to check their details.
Statement Description

An If...Then statement consists of a boolean expression


If ... Then statement
followed by one or more statements.
An If...Then statement can be followed by an optional Else
If...Then...Else statement statement, which executes when the boolean expression is
false.
You can use one If or Else if statement inside another If or Else if
nested If statements statement(s).

A Select Case statement allows a variable to be tested for


Select Case statement
equality against a list of values.
You can use one select case statement inside another select
nested Select Case statements
case statement(s).

iVB.Net - Statements
A statement is a complete instruction in Visual Basic programs. It may contain keywords,
operators, variables, literal values, constants and expressions.

Statements could be categorized as −

 Declaration statements − these are the statements where you name a variable,
constant, or procedure, and can also specify a data type.
 Executable statements − these are the statements, which initiate actions. These
statements can call a method or function, loop or branch through blocks of code or
assign values or expression to a variable or constant. In the last case, it is called an
Assignment statement.

Declaration Statements
The declaration statements are used to name and define procedures, variables, properties,
arrays, and constants. When you declare a programming element, you can also define its data
type, access level, and scope.

The programming elements you may declare include variables, constants, enumerations,
classes, structures, modules, interfaces, procedures, procedure parameters, function returns,
external procedure references, operators, properties, events, and delegates.

Executable Statements
An executable statement performs an action. Statements calling a procedure, branching to
another place in the code, looping through several statements, or evaluating an expression are
executable statements. An assignment statement is a special case of an executable statement.
VB.Net - Loops
There may be a situation when you need to execute a block of code several number of times.
In general, statements are executed sequentially: The first statement in a function is executed
first, followed by the second, and so on.

Programming languages provide various control structures that allow for more complicated
execution paths.

A loop statement allows us to execute a statement or group of statements multiple times and
following is the general form of a loop statement in most of the programming languages −

VB.Net provides following types of loops to handle looping requirements. Click the
following links to check their details.

Loop Type Description

It repeats the enclosed block of statements while a Boolean


Do Loop condition is True or until the condition becomes True. It could be
terminated at any time with the Exit Do statement.
It repeats a group of statements a specified number of times and
For...Next a loop index counts the number of loop iterations as the loop
executes.
It repeats a group of statements for each element in a collection. This
loop is used for accessing and manipulating all elements in an array or
For Each...Next
a VB.Net collection.

While... End While It executes a series of statements as long as a given condition is


True.
It is not exactly a looping construct. It executes a series of
With... End With
statements that repeatedly refer to a single object or structure.
You can use one or more loops inside any another While, For or
Nested loops
Do loop.

Loop Control Statements


Loop control statements change execution from its normal sequence. When execution leaves
a scope, all automatic objects that were created in that scope are destroyed.

VB.Net provides the following control statements. Click the following links to check their
details.

Control Statement Description

Terminates the loop or select case statement and transfers


Exit statement execution to the statement immediately following the loop or
select case.
Causes the loop to skip the remainder of its body and
Continue statement
immediately retest its condition prior to reiterating.
Transfers control to the labeled statement. Though it is not
GoTo statement
advised to use GoTo statement in your program.

VB.Net - Strings
In VB.Net, you can use strings as array of characters, however, more common practice is to
use the String keyword to declare a string variable. The string keyword is an alias for the
System.String class.

Creating a String Object


You can create string object using one of the following methods −

 By assigning a string literal to a String variable


 By using a String class constructor
 By using the string concatenation operator (+)
 By retrieving a property or calling a method that returns a string
 By calling a formatting method to convert a value or object to its string representation

Properties of the String Class


The String class has the following two properties −
Sr.No Property Name & Description
Chars
1
Gets the Char object at a specified position in the current String object.
Length
2
Gets the number of characters in the current String object.

VB.Net - Constants
The constants refer to fixed values that the program may not alter during its execution. These
fixed values are also called literals.

Constants can be of any of the basic data types like an integer constant, a floating constant, a
character constant, or a string literal. There are also enumeration constants as well.

The constants are treated just like regular variables except that their values cannot be
modified after their definition.

An enumeration is a set of named integer constants.

Declaring Constants
In VB.Net, constants are declared using the Const statement. The Const statement is used at
module, class, structure, procedure, or block level for use in place of literal values.

VB.Net - Variables
A variable is nothing but a name given to a storage area that our programs can manipulate.
Each variable in VB.Net has a specific type, which determines the size and layout of the
variable's memory; the range of values that can be stored within that memory; and the set of
operations that can be applied to the variable.

We have already discussed various data types. The basic value types provided in VB.Net can
be categorized as −

Type Example
SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong and
Integral types
Char
Floating point types Single and Double
Decimal types Decimal
Boolean types True or False values, as assigned
Date types Date
Multiple-Document Interface (MDI)
Applications
Multiple-document interface (MDI) applications enable you to display multiple documents at the
same time, with each document displayed in its own window. MDI applications often have a
Window menu item with submenus for switching between windows or documents.

How to: Create MDI Parent Forms


The foundation of a Multiple-Document Interface (MDI) application is the MDI parent form.
This is the form that contains the MDI child windows, which are the sub-windows wherein
the user interacts with the MDI application. Creating an MDI parent form is easy, both in the
Windows Forms Designer and programmatically.

To create an MDI parent form at design time

1. Create a Windows Application project.


2. In the Properties window, set the IsMdiContainer property to true.

This shows the form as an MDI container for child windows.

3. From the Toolbox, drag a MenuStrip control to the form. Create a top-level menu
item with the Text property set to &File with submenu items called &New and
&Close. Also create a top-level menu item called &Window.

The first menu will create and hide menu items at run time, and the second menu will
keep track of the open MDI child windows. At this point, you have created an MDI
parent window.

4. Press F5 to run the application. For information about creating MDI child windows
that operate within the MDI parent form

How to: Create MDI Child Forms


MDI child forms are an essential element of Multiple-Document Interface (MDI)
Applications, as these forms are the center of user interaction.

In the following procedure, you will create MDI child form that displays a RichTextBox
control, similar to most word-processing applications. Substituting the
System.Windows.Forms control with other controls, such as the DataGridView control, or a
mixture of controls enables you to create MDI child windows (and, by extension, MDI
applications) with diverse possibilities.

Note
The dialog boxes and menu commands you see might differ from those described in Help
depending on your active settings or edition. To change your settings, choose Import and
Export Settings on the Tools menu. For more information, see Personalize the Visual Studio
IDE.

To create MDI child forms

1. Create a new Windows Forms project. In the Properties Windows for the form, set
its IsMdiContainer property to true, and its WindowsState property to Maximized.

This designates the form as an MDI container for child windows.

2. From the Toolbox, drag a MenuStrip control to the form. Set its Text property to
File.
3. Click the ellipses (…) next to the Items property, and click Add to add two child tool
strip menu items. Set the Text property for these items to New and Window.
4. In Solution Explorer, right-click the project, point to Add, and then select Add New
Item.
5. In the Add New Item dialog box, select Windows Form (in Visual Basic or in Visual
C#) or Windows Forms Application (.NET) (in Visual C++) from the Templates
pane. In the Name box, name the form Form2. Click the Open button to add the form
This form will be the template for your MDI child forms.

The Windows Forms Designer opens, displaying Form2.

6. From the Toolbox, drag a RichTextBox control to the form.


7. In the Properties window, set the Anchor property to Top, Left and the Dock
property to Fill.

This causes the RichTextBox control to completely fill the area of the MDI child
form, even when the form is resized.

8. Double click the New menu item to create a Click event handler for it.
9. Insert code similar to the following to create a new MDI child form when the user
clicks the New menu item.

10. In the drop-down list at the top of the Properties window, select the menu strip that
corresponds to the File menu strip and set the MdiWindowListItem property to the Window
ToolStripMenuItem.

This will enable the Window menu to maintain a list of open MDI child windows with a
check mark next to the active child window.

11. Press F5 to run the application. By selecting New from the File menu, you can create
new MDI child forms, which are kept track of in the Window menu item.

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