0% found this document useful (0 votes)
11 views

JK VB - Net - 4 Hello World

Uploaded by

curtisandrea242
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

JK VB - Net - 4 Hello World

Uploaded by

curtisandrea242
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

VB.

Net Hello World Example

Before we study basic building blocks of the VB.Net


programming language, let us look a bare minimum VB.Net
program structure so that we can take it as a reference in
upcoming lecturers.
VB.Net Hello World Example
A VB.Net program basically consists of the following parts:
Namespace declaration
A class or Module
One or more procedures
Variables
The Main procedure
Statements & Expressions
Comments
Let us look at a simple code that would print the words "Hello
World" −
Module Module1
'This program will display Hello World
Sub Main()
Console.WriteLine("Hello World")
Console.ReadKey()
End Sub
End Module

When the above code is compiled and executed, it


produces the following result −
Hello, World!
• Let us look various parts of the above program −

• The first line has a Module declaration, the module Module1. VB.Net is
completely object oriented, so every program must contain a module of
a class that contains the data and procedures that your program uses.
• Classes or Modules generally would contain more than one procedure.
Procedures contain the executable code, or in other words, they define
the behavior of the class. A procedure could be any of the following −
– Function
– Sub
– Operator
– Get
– Set
– AddHandler
– RemoveHandler
– RaiseEvent

• The next line ′ This program will be ignored by the compiler and it has
been put to add additional comments in the program.

The next line Sub Main() defines the Main procedure, which is the entry
point for all VB.Net programs (execution starts here). The Main
procedure states what the module or class will do when executed.
• The Main procedure specifies its behavior with the
statement
• Console.WriteLine"Hello World“

• WriteLine is a method of the Console class defined in


the System namespace.
• This statement causes the message "Hello, World!" to be
displayed on the screen.

• The last line Console.ReadKey is for the VS.NET Users.


This will prevent the screen from running and closing
quickly when the program is launched from Visual
Studio .NET.
Compile & Execute VB.Net Program

• If you are using Visual Studio.Net IDE, take the following


steps −
• Start Visual Studio.
• On the menu bar, choose File → New → Project.
• Choose Visual Basic from templates
• Choose Console Application.
• Specify a name and location for your project using the Browse
button, and then choose the OK button.
• The new project appears in Solution Explorer.
• Write code in the Code Editor.
• Click the Run button or the F5 key to run the project. A
Command Prompt window appears that contains the line Hello
World.
VB IDE Components
• Menu Bar: contains all commands needed to run VB
(File, Edit, View etc…).
• Toolbars: quick access to commonly used menu
commands.
• Solution Explorer: displays the project’s components.
• Toolbox: contains icons of the controls that can be
placed on the project’s Forms to create the
application’s interface.
• Properties Window: Shows the property settings of
the selected control (size, caption, color, etc…).
VB IDE Components (Cont’d)
• Form Designer: Main window in which one
can design and edit the application’s user
interface. In this window the user can enter
and edit the application’s code. It displays two
windows: Form and Code window.
• Form Layout: Shows the initial positions of the
forms in the application. It is useful in multiple
forms applications.
• Immediate Window: Debugging tool.
Programming Steps
• Step 1: Customize the windows that the user
sees. I.e. placing controls and components on
the layouts of the project’s Forms.
• Step 2: Decide on the events each control
should recognize.
• Step 3: Coding the event procedures for those
events.
• VB.Net is an object-oriented programming language.
In Object-Oriented Programming methodology, a
program consists of various objects that interact with
each other by means of actions.
• The actions that an object may take are called
methods. Objects of the same kind are said to have
the same type or, more often, are said to be in the
same class.
• When we consider a VB.Net program, it can be
defined as a collection of objects that communicate
via invoking each other's methods. Let us now briefly
look into what do class, object, methods and instance
variables mean.
• Object − Objects have states and behaviors. Example: A
dog has states - color, name, breed as well as behaviors -
wagging, barking, eating, etc. An object is an instance of
a class.
• Class − A class can be defined as a template/blueprint
that describes the behaviors/states that objects of its
type support.
• Methods − A method is basically a behavior. A class can
contain many methods. It is in methods where the logics
are written, data is manipulated and all the actions are
executed.
• Instance Variables − Each object has its unique set of
instance variables. An object's state is created by the
values assigned to these instance variables.
Identifiers
• An identifier is a name used to identify a class,
variable, function, or any other user-defined item.
• The basic rules for naming classes in VB.Net are as
follows −
• A name must begin with a letter that could be
followed by a sequence of letters, digits (0 - 9) or
underscore.
• The first character in an identifier cannot be a digit.
• It must not contain any embedded space or symbol
like ? - +! @ # % ^ & * ( ) [ ] { } . ; : " ' / and \.
• However, an underscore ( _ ) can be used.
• It should not be a reserved keyword.
VB.NET – BASIC CONTROLS
• An object is a type of user interface element
you create on a Visual Basic form by using a
toolbox control. In fact, in Visual Basic, the
form itself is an object.
• Every Visual Basic control consists of three
important elements:
– Properties which describe the object,
– Methods cause an object to do something and
– Events are what happens when an object does
something.
Control Properties
• All the Visual Basic Objects can be moved, resized or
customized by setting their properties.
• A property is a value or characteristic held by a Visual Basic
object, such as Name or Fore Color.
• Properties can be set at design time by using the Properties
window or at run time by using statements in the program code.
• Object. Property = Value
• Where:
– Object is the name of the object you're customizing.
– Property is the characteristic you want to change.
– Value is the new property setting.
Control Properties

• For example,
• Button1.Text = "Hello World“
• You can set any of a control ‘s (object )properties using
Properties Window.
• Most of the properties can be set or read during application
execution.
• You can refer to Microsoft documentation for a complete list
of properties associated with different controls and
restrictions applied to them.
Control Methods

• A method is a procedure created as a member of a class and


they cause an object to do something. Methods are used to
access or manipulate the characteristics of an object or a
variable.
• There are mainly two categories of methods you will use in
your classes:
• If you are using a control such as one of those provided by the
Toolbox, you can call any of its public methods.
• The requirements of such a method depend on the class being
used.
• If none of the existing methods can perform your desired task,
you can add a method to a class.
Control Methods
• For example, the MessageBox control has a method named
Show, which is called in the code
• snippet below:
Public Class Form 1
Private Sub Button1_Click(ByVal sender As System .Object, ByVal e As
System .EventArgs) Handles Button1.Click

MessageBox.Show("Hello, World")
End Sub
End Class
Control Events

• An event is a signal that informs an application that something


important has occurred.
• Forexample, when a user clicks a control on a form, the form can
raise a Click event and call a procedure that handles the event.
There are various types of events associated with a Form like
• click, double click, close, load, resize, etc.
• Following is the default structure of a form Load event handler
subroutine. You can see this code by double clicking the code
which will give you a complete list of the all events associated
with
Control Events
• Form control:
• Private Sub Form 1_Load(sender As Object, e As EventArgs)
Handles MyBase.Load
• 'event handler code goes here
• End Sub
• Here, Handles MyBase.Load indicates that Form1_Load
subroutine handles Load event. Similar way, you can check stub code
for click, double click. If you want to initialize some variables like
properties, etc., then you will keep such code inside Form1_Load
subroutine.
• Here, important point to note is the name of the event handler, which
is by default Form1_Load, but you can change this name based on
your naming convention you use in your application programming.
Basic Controls
• VB.Net provides a huge variety of controls that
help you to create rich user interface.
• Functionalities of all these controls are
defined in the respective control classes.

• The control classes are defined in the


System.Windows.Forms namespace.

• The following table lists some of the


commonly used controls:
lists some of the commonly used controls:
1 Forms
The container for all the controls that make up the user interface.
2 TextBox
It represents a Windows text box control.
3 Label
It represents a standard Windows label.
4 Button
It represents a Windows button control.
5 ListBox
It represents a Windows control to display a list of items.
6 ComboBox
It represents a Windows combo box control.
7 RadioButton
It enables the user to select a single option from a group of choices when
paired with other RadioButton controls.
8 CheckBox
It represents a Windows CheckBox.
lists some of the commonly used controls:
9 PictureBox
It represents a Windows picture box control for displaying an image.
10 ProgressBar
It represents a Windows progress bar control.
11 ScrollBar
It Implements the basic functionality of a scroll bar control.
12 DateTimePicker
It represents a Windows control that allows the user to select a date and a time
and to
display the date and time with a specified format.
13 TreeView
It displays a hierarchical collection of labeled items, each represented by a
TreeNode.
14 ListView
It represents a Windows list view control, which displays a collection of items that
can be
displayed using one of four different views.

You might also like