Documente Academic
Documente Profesional
Documente Cultură
Programming in VB.NET
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
Contents:
PREFACE ................................................................................................................................................................ 3
ABOUT THE AUTHOR: ............................................................................................................................................ 4
ABOUT THE BOOK: ................................................................................................................................................. 6
CONTENTS: ............................................................................................................................................................ 7
.NET HISTORY ...................................................................................................................................................... 14
.NET HISTORY .............................................................................................................................................................14
APPLICATIONS THAT CAN BE DEVELOPED UNDER .NET ........................................................................................ 14
VISUAL STUDIO INTEGRATED DEVELOPED ENVIRONMENT .................................................................................. 14
WHY TO USE VS IDE ....................................................................................................................................................14
TYPES OF APPLICATIONS ...................................................................................................................................... 15
SINGLE TIER APPLICATIONS: ............................................................................................................................................15
TWO TIER APPLICATIONS: ..............................................................................................................................................15
FAT CLIENT: .......................................................................................................................................................15
FAT SERVER: .......................................................................................................................................................15
DIVIDING BUSINESS SERVICES BETWEEN USER AND DATA SERVICES: ................................................................................15
LIMITATIONS OF TWO TIER APPLICATIONS: .........................................................................................................................16
THREE TIER APPLICATIONS:.............................................................................................................................................16
N TIER APPLICATIONS: ................................................................................................................................................ 16
WHY TO USE .NET ................................................................................................................................................ 17
ADVANTAGES OF USING .NET FRAMEWORK ......................................................................................................................17
.NET ARCHITECTURE ............................................................................................................................................ 17
LAYER 1: ....................................................................................................................................................................18
LAYER 2: ....................................................................................................................................................................18
LAYER 3: ....................................................................................................................................................................18
MANAGE CODE ...........................................................................................................................................................18
GARBAGE COLLECTION PROCESS .....................................................................................................................................18
NAMESPACES: .............................................................................................................................................................19
ASSEMBLY: .................................................................................................................................................................19
MANAGE CODE EXECUTION PROCESS .................................................................................................................. 20
EXPLANATION: .............................................................................................................................................................20
FIRST VB.NET PROGRAM...................................................................................................................................... 21
HELLO WORLD AGAIN!!! ...............................................................................................................................................21
SECOND VB.NET PROGRAM ................................................................................................................................. 23
THIRD PROGRAM (USING STRUCTURES) .............................................................................................................. 24
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
INHERITANCE ....................................................................................................................................................... 35
CODE REUSABILITY ........................................................................................................................................................35
TYPES OF INHERITANCE ..................................................................................................................................................35
VARIABLE SCOPE ..........................................................................................................................................................35
HOW TO INHERIT..........................................................................................................................................................35
RUN TIME POLYMORPHISM ................................................................................................................................. 37
OBJECT SLICING, RUN TIME BINDING, LATE BINDING...........................................................................................................37
EXAMPLE ....................................................................................................................................................................37
MAIN1 ......................................................................................................................................................................37
MAIN2 ......................................................................................................................................................................38
RUN TIME POLYMORPHISM MAIN3 .................................................................................................................................38
Assignment: ...................................................................................................................................................................... 39
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
EXPLANATION: .............................................................................................................................................................42
ABSTRACT CLASS VS. INTERFACE ......................................................................................................................................42
EXAMPLE1 ..................................................................................................................................................................42
EXAMPLE2 IMPLEMENTING MULTIPLE INTERFACES ..............................................................................................................43
EXAMPLE3 INHERITING INTERFACES .................................................................................................................................44
NON INHERITABLE CLASS ..................................................................................................................................... 45
EXAMPLE ....................................................................................................................................................................45
ENUMERATION .................................................................................................................................................... 45
OVERRIDING TOSTRING () FUNCTION .................................................................................................................. 46
EXAMPLE: ...................................................................................................................................................................46
UNDERSTANDING CONSOLE ................................................................................................................................ 47
CREATING NAMESPACE ....................................................................................................................................... 47
COMMAND LINE ARGUMENTS............................................................................................................................. 48
PASSING COMMAND LINE ARGUMENTS .............................................................................................................................48
Assignment ....................................................................................................................................................................... 49
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
10
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
TIMER .................................................................................................................................................................. 83
PROPERTY WINDOW:....................................................................................................................................................83
EXAMPLE: ...................................................................................................................................................................83
LINKLABEL ............................................................................................................................................................ 84
EXAMPLE: ...................................................................................................................................................................84
PROGRESSBAR ..................................................................................................................................................... 85
EXAMPLE: ...................................................................................................................................................................85
WEBBROWSER ..................................................................................................................................................... 86
EXAMPLE: ...................................................................................................................................................................86
MASKEDTEXTBOX ................................................................................................................................................ 87
RADIOBUTTON .................................................................................................................................................... 88
EXAMPLE: ...................................................................................................................................................................88
A BIG CONFUSION!!! ............................................................................................................................................ 89
CONFUSION SOLVED ............................................................................................................................................ 90
COMBOBOX ......................................................................................................................................................... 91
EXAMPLE: ...................................................................................................................................................................91
STILL, CONFUSION!!! ............................................................................................................................................ 92
11
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
12
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
13
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
.NET History
.NET History
.NET 1.0
.NET 1.1
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
2002
2003
2005
2007
2008
2010
.NET 2010 is the latest version of .NET. We will be using the same in our programs.
Console Applications
Windows Applications
Web Applications
Component Libraries
Windows Custom Control
Web Custom Control
Web Service
Windows Service
And lots more
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
10
Types of Applications
Single tier Applications:
A single executable file handles all functions relating to user, business and data service layer.
Such an application also called monolithic application. Ex: Very early COBOL applications, console
applications (DOS mode applications), In Dbase and FoxPro; user interface is with Database.
Fat Client:
Business services layer combine with user services layer. Client executes presentation
logic and enforces business rules. Server stores data and process transactions. Fat client model is use
when server is overloaded with transaction processing activities and not available to process business
logic.
Fat Server:
Business services layer combine with data services layer. As business services stored on
server most of processing take place on server.
15
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
11
n Tier Applications:
An n tier architecture/ application use multiple server handling business services. This
architecture provides various advantages over other architectures. Ex: extensibility, maintainability and
scalability. One of most important requirements of such application is the ability to interchange info
across platform and to benefit from the functionality provided by other application. In other words,
there should be increased interoperability and integration between various applications. A website may
register its consumers and provides services to them; however it may not be capable of interacting with
other websites that providing complementary services to customers. For performing such tasks;
applications needs to provide cross platform application integration.
16
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
12
.NET Architecture
17
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
13
.NET framework exists as a layer between .NET applications and underlying Operating System. The
above figure shows components of .NET framework.
Layer 1:
Layer 1 Consists of web forms, windows forms and console applications that constitute to the
presentation layer of an application. Web forms are used in web applications where as windows forms
are used in windows based applications for providing interacting user interface. In addition, you can
create character based console application that can be executed from command line.
Layer 2:
One of the most important features of .NET Framework class library is that it can be used in a
consistent manner across multiple languages. This means, you can use same set of classes for
performing a specific task in VB.NET as well as in C++.NET. The .NET Framework class libraries contain
namespaces which are contained within assemblies.
Layer 3:
The common language runtime provides functionality such as exception handling, security,
debugging, and version control to any language that target it. This means that common language
runtime can host a variety of languages and offer a common set of tools across these languages ensuring
interoperability between codes. Ex: the runtime can execute programs written in any language such as
VB.NET, C#.NET and C++.NET with manage extension.
Manage Code
-
Code that is developed with a language compiler that target CLR called manage code
Code that is developed without considering the conventions and requirements of runtime
called unmanaged code. Unmanaged code runs in CLR with limited debugging and without
garbage collection process.
18
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
14
Namespaces:
Namespaces helps you to create logical groups of related classes and interfaces that can be used
by any language targeting .NET framework. Namespaces allow you to organize your classes so that they
can be easily accessed in other application. Namespaces can also be used to avoid any naming conflicts
between classes that have same name. Ex: you can use two classes with same name in an application
provided they belong to different namespaces. You can access classes belonging to a namespace by
simply using namespace name followed by dot (.) and then class name.
Ex: System.console => here System is namespace and console is class name
Assembly:
Assembly is a single deployable unit that contains all information about implementation of
classes, structure and interfaces. An assembly stores all information about itself and this information
called Meta data and includes the name and version number of assembly, security information and
information about dependencies, list of files that constitute assembly. All the application developed
using .NET framework made up of assembly. Namespaces are also stored in assemblies. Assemblies and
metadata provide the CLR with the information required for execution of an application. Assembly
provides this information to CLR while application is being executed. Assembly also plays an important
role in deployment and version control.
19
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
15
Explanation:
When you compile a program develop in a language that targets the CLR, instead of compiling
the source code into machine level code, the compiler translate it to Microsoft Intermediate Language
(MSIL) or Intermediate Language (IL). No matter which language has been used to develop the
application, it always gets translated into IL; this ensures language interoperability. Compiler also
produces Meta data about the program/ application during process of compilation. Meta data contains
description of program such as classes and interfaces, version of components stored in program. IL and
metadata linked in an assembly.
When you execute .exe/ .dll file, the IL and other information from base class library is sent to
class loader. Class loader loads code in memory.
Before code can be executed, .NET framework needs to convert the intermediate language into
native or CPU specific code. JIT compiler translates the code from IL to managed native code. During
20
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
16
process of compilation, JIT compiles only code that is required. During execution, instead of compiling
complete code, when an un-compiled method is invoked during execution, JIT compiler converts IL for
that method into native code. This process saves the time and memory required to convert complete IL
into native code.
During JIT compilation, code is also check for Type Safety. Type Safety ensures that objects are
always accessed in a compatible way.
While executing code, a security check is performed to ensure that code has appropriate
permission for accessing available resources.
Open Visual Studio 2010 and click on New Project; a dialog box will open, choose language VB and
from application choose Console Application.
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
17
Boolean
Byte
Char
DateTime
Decimal
Double
Int16
Int32
Int64
SByte
Single
TimeSpan
Write the following code in your Console Application code window
22
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
18
Key Points:
We can use System.Console.Write
System namespace is included in our programs, so it is optional to write System
Convert.int32 is used to convert string to integer type
Every read line is string by default, we have to convert it to specific type, we want to use.
23
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
19
Key Points:
24
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
20
Using if-else
Key Points:
Option Explicit ON => it will force you to declare each variable before it is being used
Option Explicit OFF
Option Strict ON => to stop automatic conversion like from int to long
Option Strict OFF
ByVal => to pass a parameter by value
ByRef => to pass a parameter by reference
We can comment a line by pressing in beginning of a line.
Boolean Operators:
o AND
o OR
o NOT
o XOR
o ANDALSO
o ORELSE
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
21
Looping
Assignment:
1. Print table of a number using for loop, do-while loop, do-until loop
2. Read a number from console and check whether it is prime or not
3. Ask two numbers from user and check which number is greater using conditional operator
26
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
22
27
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
23
Assignment
1. Addition of 2 matrix of order 2X2
2. Initially stores names of 5 students, then change array to hold 10 names and print 10 names
using for-each loop
3. Define a function factorial which returns factorial of a number
4. Make a subroutine which swaps two integers
5. Make a subroutine which swaps two integers without using third variable
6. Print following
1
12
123
1234
28
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
24
Using Select-Case
Function overloading
29
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
25
Assignment
1. Overload area() function to calculate area of circle and rectangle.
2. Overload sum() function to sum 2 integers, 2 doubles and 2 strings
3. Overload volume() function to calculate volume of cube, cylinder and cuboid
Defining Classes
Making a simple class
30
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
26
Creating constructor
31
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
27
Overloading constructor
32
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
28
Defining Properties
Key Points:
Properties can be readonly or writeonly also
Write readonly or writeonly keyword before property keyword while defining properties.
33
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
29
Operator Overloading
Assignment
1. Overload +,-,*,/ in complex class
a. You can use simple multiplication and division
2. Overload + and in matrix class of order 3 X 3
34
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
30
Inheritance
Code reusability
Time saving, cost saving
Types of inheritance
Single Level Inheritance
Multi level inheritance
Hierarchical Inheritance
Variable Scope
Private => accessible in member functions only
Protected => accessible in derived class also
Public => accessible outside the class also
How to inherit
35
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
31
Inheritance Example
36
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
32
Example
Main1
37
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
33
Main2
38
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
34
Assignment:
1. Assume that a bank maintains two kinds of accounts for customers, one called as saving account
and other as current account. The savings account provides compound interest and withdrawal
facilities but no cheque book facility. The current account provides cheque book facility but no
interest. Current account holders should also maintain a minimum balance and if the balance
falls below this level, a service charge is imposed.
Create a class account that stores customer name, account number and type of account. From
this derive the classes cur_acct and sav_acct to make them more specific to their requirements.
Include necessary member functions in order to achieve the following tasks:
a. Accept deposit from a customer and update the balance.
b. Display the balance
c. Compute and deposit interest
d. Permit withdrawal and update the balance
e. Check for the minimum balance, impose penalty, necessary, and update the balance
Dont use any constructors. Use member functions to initialize the class members.
2. Specify all the classes as shown in figure and define functions to create the database and retrieve
individual information as and when required
39
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
35
3. Create a base class called shape. Use this class to shore two double type values that could be
used to computer the area of figures. Derive two specific classes called triangle and rectangle
from the base shape. Add to the base class, a member function get_data() to initialize base class
data members and another member function display_area() to computer and display the area of
figures. Override display_area() in derive classes to suit their requireents.
Using these three classes, design a program that will accept dimensions of a triangle or a
rectangle interactively, and display the area.
Remember two values given as input will be treated as lengths of two sides in case of rectangles
and as base and height in case of triangles, and used as follows:
Area of rectangle = x * y
Area of triangle = *x*y
4. Extend the above program to display area of circles. This requires addition of a new derived class
circle that computes the area of a circle. Remember, for a circle we need only one value, its
radius, but the get_data() function in the base class requires two values to be passed. (Hint:
Make the second argument of get_data() function as a default/ optional one with zero value)
40
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
36
Abstract Classes
Explanation:
Abstract classes are used to create template for other classes.
We cant create object of abstract classes.
In VB.NET abstract classes are declared by the keyword mustinherit
As we cant create object of abstract class, so there is no use of defining any function, so we declare
functions/ subroutine with the keyword mustoverride
Example:
41
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
37
Interface
Explanation:
~ An alternative to abstract classes
~ less formality as compared to abstract classes
Example1
42
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
38
Example2
Implementing multiple interfaces
43
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
39
Example3
Inheriting interfaces
44
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
40
Example
Enumeration
Key points:
1. Default value is 0 for first name in enumeration
2. Rest all will get incremented value.
45
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
41
Example:
46
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
42
Understanding console
Creating Namespace
47
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
43
48
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
44
Assignment
1. Make a vehicle class, overload properties for registration number and model number. Also
override tostring () function in vehicle class
2. Use command line arguments to perform +, - , *, /. For example, user will provide first number
then operator and then second number like
a. Consoleapplication1.exe 10 + 20
3. Define a test class in a namespace sample. Define input and output function for the class test
and use them in main.
4. Make an enumeration month. User will enter a numeric month, use select case to check the
month name and print number of days for the same.
49
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
45
50
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
46
51
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
47
52
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
48
Understanding System.Environment
Getting User Name
Getting Current Directory
Getting System Directory
Getting Folder Path
Getting Operating System Version
Checking whether it is 64 bit OS or not
53
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
49
Understanding My.Computer
Getting GMT time
Checking various keys like caps lock, num lock, shift etc
Checking whether mouse is left handed or right handed
Getting computer name
Checking total physical memory
Checking total virtual memory
Checking available physical memory
Checking screen information, resolution etc
Getting OS platform
Getting culture
File System
54
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
50
Assignment
Try out various function of My.Computer.FileSystem at your own.
55
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
51
Using System.Collections
Stack
56
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
52
Queue
57
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
53
Assignment
There are lot many functions of strings. Try them at your own.
58
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
54
DateTime functions
Assignment:
1. Write a program to copy one file to another
2. Write a program which read from 2 files and write into third file
3. Write a program which read form one file and copy all even lines to second file and odd lines
to third file
4. Write a program which read input lines from users and save them in a file on desktop and
then read those lines from file and display it to user
Hint: use getfolderpath() to get path of desktop
5. Write a program for parsing using stack
6. Write a program which converts infix to postfix and evaluate the postfix expression
7. Write a program which converts infix to prefix and evaluate the prefix expression
59
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
55
Exception Handling
I am not covering theory part as every book has long stories on what are exceptions. Here we are
directly going to look at procedure to handle exceptions.
1.
2.
3.
4.
Try-Catch
60
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
56
61
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
57
Exception Class
-
62
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
58
63
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
59
Nested Try
In case inner try was not able to handle the exception, it will search catch in outer try.
64
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
60
65
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
61
Assignment
Try some examples on exceptions at your own
66
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
62
Windows Applications
I expect you are comfortable with Microsoft Visual Basic 6.0. Incase you dont have knowledge about
Visual Basic 6.0, I will suggest you to first go thru with that.
Here, we will be referring directly to the some common properties and some common events of
controls.
67
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
63
Remember:
In .NET, each control is having text property instead of text and caption property and the most
important line, everything is based on namespaces, classes and object is mother of all.
68
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
64
Remember:
Me refers to current form. In VB.NET, you cant use current form name in current form class, so
we will be use Me if we want to refer to current class.
6. To execute the application, press ctrl + F5 or simply press play button in toolbar
69
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
65
Change text properties from properties menu. I am here not changing name properties of controls; you
can change them as per your convenient.
And write button1_click event
70
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
66
Form
Button
CheckBox
CheckListBox
ColorDialog
Combo
ContextMenu
DataGrid (at a later stage)
DateTimePicker
DomainUpDown
ErrorProvider
FontDialog
GroupBox
HScrollBar
Label
LinkLabel
ListBox
ListView
MaskedTextBox
MenuStrip
MonthCalendar
NotifyIcon
NumericUpDown
OpenFileDialog
Pannel
PictureBox
ProgressBar
RadioButton
RichTextBox
SaveFileDialog
StatusStrip
TabControl
TextBox
Timer
ToolTip
ToolStrip
TrackBar
VScrollBar
71
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
67
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
MediaPlayer
WebBrowser
AdobePdf
TableLayout
FlowLayout
FolderBrowserDialog
PrintDialog
PrintForm
Animation Control
Drawing Graphics
MessageBox
InputBox
These controls are described in an order to make the reader comfortable and to make learning more
interesting.
72
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
68
Label
Properties window:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
Events:
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
69
TextBox
Properties window:
Same as of label control, some new properties are shown here
1. Multiline = true ( you can write multiple lines in textbox; by default, textbox is single line)
2. Password char = * ( now the text appearing in textbox will be marked as * ; you can use any
character)
3. ReadOnly = True ( now user can read the text of textbox, he cant modify it)
4. ScrollBar = None / Horizontal / Vertical / Both
5. WordWrap = True
6. TabStop = True ( Using Tab key, you can focus on this control )
7. TabIndex, to set tab preference, when tab should appear on it.
I will recommend you to try all properties of properties window.
Events:
74
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
70
Form
Properties window:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
BackgroundImage
BackgroundImageLayout
ControlBox = False ( minimize, maximize and close button will not appear)
FormBorderStyle ( Try all these options and see effects)
MaximizeBox = False (Maximize button will get disable on form)
MinimizeBox
Opacity (to set transparency of Form, Try using with 80% and 50%, Default is 100%)
ShowInTaskbar = False (When you execute your form, your form will not appear in Taskbar)
Text = to change title bar text of form
TransparencyKey = white ( which color of form you want to be transparent, if I have selected
white, then the white color, wherever on form , will be transparent)
Events:
75
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
71
Button
Properties window:
Properties are similar to textbox and label, try them at your own.
Events:
76
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
72
2. Program it as
3. Now execute it
77
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
73
Again Confusion!!
1. Design same form again
2. Program it as
78
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
74
CheckBox
Properties window:
1. Checked: (Initially, when form is loaded, this checkbox will be checked or not)
2. ThreeState : True ( Then checkbox can have three states : Unchecked, Checked or intermediate )
Events:
Example:
Design your form as:
Program it as:
79
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
75
Result:
80
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
76
TrackBar
Properties Window:
1. Minimum and Maximum value
2. RightToLeft
3. Orientation : Horizontal or Vertical
Events:
Example:
81
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
77
VSrollBar
Example:
Assignment
Try HScrollBar at your own.
82
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
78
Timer
Simply drag the timer on your form. When you will drag, it will appear on the bottom bar IDE.
Property Window:
1. Enable: True ( to enable the timer, by default; its false)
2. Interval : duration after which timer will tick (1000 means 1 sec)
Example:
83
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
79
LinkLabel
It will appear as a link on form. You can do the normal working on its click event or you can browse
some website as it looks states.
Example:
84
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
80
ProgressBar
Example:
Simply drag a progressbar on your form. Change its minimum, maximum and style properties (if you
want)
Place a timer on your form. Change its interval to 1000 and enable to true
And program your timer as below and execute your program.
85
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
81
WebBrowser
Example:
Drag WebBrowser on your form. By Default it is docked in form. Click on Top Right Arrow appearing on
WebBrowser Control and choose undock
Initially, we are designing it with buttons, later you can create toolbar or menu bar and also you can use
images to change its Design.
86
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
82
Thats all we have to do and Thanks to Microsoft and our WebBrowser is ready!!!
MaskedTextBox
Use to specify a Mask/ Format.
Change its mask property and try it at your own. Its similar to TextBox except its mask property.
Try this at your own.
87
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
83
RadioButton
Example:
Design your form as:
88
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
84
A Big Confusion!!!
Design your form as:
And now execute your form and try to select one option from both groups!!!
89
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
85
Confusion Solved
Drag GroupBox from toolbox and design your form as:
90
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
86
ComboBox
Example:
Design your form as:
And execute it, choose one day from combo box and click submit.
91
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
87
Still, Confusion!!!
In above program, check carefully, user is able to change the text of ComboBox. User can edit the text of
combo box.
Confusion Solved:
Select the combo box and from properties menu, change its DropDownStyle to DropDownList.
92
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
88
ColorDialog
Example:
Simply drag color dialog on your form and program your form as:
And execute your form and click on form and change color
93
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
89
FontDialog
Example:
Drag font dialog on your form and design your form with a button and textbox and program it as:
94
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
90
FolderBrowserDialog
Example:
Drag FolderBrowserDialog and a button on form and program it as:
Using FolderBrowserDialog, you can ask user to select folder, where he wants to save files, and you can
get the path using FolderBrowserDialog.
Note: before showing this dialog, write
FolderBrowserDialog1.ShowNewFolderButton = False
95
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
91
OpenFileDialog
Example:
Drag OpenFileDialog and a button on form and program it as:
Now you know the file handling; so instead of displaying filename in msgbox, you can read its contents
and show in textbox or you can perform some operations. Basic idea is to show you how to use
OpenFileDialog.
Note: Try SaveFileDialog at your own.
96
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
92
AdobePdf
Example:
Drag AdobePdf, OpenFileDialog and a button on your form and program it as:
97
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
93
NumericUpDown
Example:
Drag NumericUpDown and a button on form. You can change minimum and maximum value of
NumericUpDown from properties menu. Program your button as:
98
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
94
MonthCalendar
Try this at your own. Try out all its properties. Its generally used to display calendar on form.
DateTimePicker
Using this control, you can get the value (Date) which user has selected.
Example:
Design your form as:
99
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
95
MessageBox
Till now, we are using Msgbox (), but this is a function of VB. Now we will be discussing MessageBox.
Variants of MessageBox:
InputBox
Its used to take a value from user. It will return a string; you have to convert it to specific type before
using.
Variants of InputBox:
100
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
96
ListBox
Example:
Simply drag a ListBox and a button on your form and program it as:
101
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
97
And now we have to retrieve all selected items, so we will program button_click as:
102
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
98
103
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
99
CheckListBox
Example:
Drag a CheckListBox and a button on form and program it as:
104
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
100
MenuStrip
Drag the MenuStrip on form. You can directly edit the menus you want to create.
Use if you want to create separator between menus. And you can assign ALT keys as shortcut by
writing & before that character.
105
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
101
You can assign the shortcut keys by choosing that particular item and choose shortcut key from its
properties menu. For example: here I want to assign ctrl + s for my save menu
To program it, simply double click that menu item, which you want to program.
106
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
102
ContextMenuStrip
Its is similar to MenuStrip except that it doesnt appear as normal menu bar, you have to assign
ContextMenu to some control, where user will right click to see the menu. Here, I am assigning
ContextMenu to TextBox as:
107
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
103
ErrorProvider:
Drag a textbox, a button and an error provider on form. Suppose we want to show an error message to
user, if textbox. Text is blank. We will write as:
108
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
104
RichTextBox
Have you noticed the difference between notepad and WordPad? In WordPad, you can paste pictures;
you can change background and foreground color of selected text, however in notepad, all this is not
possible. So, the alternate to TextBox is RichTextBox.
Drag it to your form. By default it is multiline. Lets look at some of its properties:
Assignment:
Make a Notepad Application using TextBox.
Enhance your notepad using RichTextBox.
109
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
105
FlowLayout
Used to draw controls. Like a Panel or GroupBox. Draw a FlowLayout on your form and place your
controls inside it. Controls will appear automatically in flowing locations. Try this at your own.
FlowLayout Example:
TableLayout
You will find using FlowLayout is quite difficult because we cant place controls at desired locations. One
alternative is TableLayout. You can place your controls in cells; you can insert some rows and columns
and can insert controls in cells. Try this control at your own.
TableLayout Example:
110
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
106
MediaPlayer
Drag MediaPlayer, 3 buttons and an OpenFileDialog on your form and design your form as:
111
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
107
Media Player has lots of properties. Try out all those properties.
112
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
108
ToolTip
Drag a ToolTip and a button on form. Suppose we want to show ToolTip on this button. Select button
and from its properties window, choose ToolTip on ToolTip1 and set it to any value.
And now select ToolTip control which you have dragged on form and from its properties menu, you can
set its icon, isBalloon and ToolTipTitle.
113
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
109
And now execute your form and move your mouse over Button.
PictureBox
Drag a PictureBox on your form and click on arrow appearing on top right corner; change SizeMode
property to different options and check their effects. You can change image of PictureBox in two ways as
shown below. Use any one method at a time.
StatusStrip
Drag a StatusStrip on Form; it will appear at the bottom of Form. You can add label, ProgressBar and
Dropdown by clicking on the add new icon appearing on StatusStrip.
114
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
110
Example:
Here, on status label, I want to show the number of characters in RichTextBox.
And now execute your form, type something in RichTextBox and check status label.
115
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
111
ToolStrip
To create Toolbar. Drag ToolStrip on Form. By Default, it will appear on top corner. You can change its
back color, background image etc (Check its properties window). Here, I am setting back color to
transparent.
There is also an add new button appearing on ToolStrip, you can add new items by clicking on it.
Suppose we are adding a Button, by default it will appear as image, right click on image button and
change its display style like image and text. Now you can change its image by choosing set image option
from context menu and its text can be changed from properties menu. Here I have added two buttons:
You can program them by simply double clicking on them. In ToolStrip you can Buttons, Dropdown,
Label, Separators, Progress Bar, and TextBox.
116
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
112
117
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
113
TabControl
Drag a TabControl on your form. By default, only two tabs will be shown, you can add or remove tabs by
choosing TabPages from its properties menu. You can add your controls in it and program them. Now I
leave it to you, how creative you are!
Here are some properties which will help you:
Some Hints:
You can create MultiTab WebBrowser or you can create your notepad and tabbed menu to it.
Printing:
The simplest way, I found is- generate HTML report or open your document thru WebBrowser and user
will automatically do the page setup, print preview and print automaticallyWe dont have to do all that
stuff.
118
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
114
119
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
115
Animation:
We dont have animation control present in our toolbox; we have to add it explicitly. Right click the
toolbox and click choose items and from the dialog box, choose com components and from that choose
Microsoft Animation Control, and click ok.
It will add animation control to your Toolbox; now drag Animation Control to your form.
120
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
116
Animation Control can play .avi files, search for .avi files available on your PC. Generally, you have seen,
while copying files, animation plays on form. We will do something similar. Program your button as:
Note:
Bundle of images and animation comes with VisualStudio2010. You can found them in similar path of
the path shown below:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary\1033\
Extract files and use them.
121
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
117
Graphics:
Here, we have created a line and a rectangle; there are many functions available for graphics, Again I
leave it on you, how creative you are!
NotifyIcon
It appears in System Tray.
Drag NotifyIcon control to your form, select it and change its properties (icon, text and title).
122
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
118
Note: Dont forget to set its icon property from property menu.
And now on button_click write
123
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
119
Assignment:
1. Design a Code Module for C++ or Java, like you have Visual Studio for .NET, Similar to this, Design
for C++ or Java, so that user dont have to write so much, Everything Drag and Drop and in
background, code will appear automatically and user can view, edit or save it.
124
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
120
Database Connectivity
ADO.NET
Here, I have tried to simplify the Database connectivity. The following figure explains how to connect
with database. We are showing here the connectivity with MS-Access, so that you can practice at home.
In the similar way, you can connect to SQL Server. MS-Access is not generally used because it can handle
only 35 clients at a time while SQL Server and Oracle can handle a large number of clients.
125
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
121
Displaying all records at a time using Data Adapter and Data Set
1.
2.
3.
4.
Open connection
Use Data Adapter to read from connection
Use Data Set to store data
And then display data from Data Set
Open windows application and drag DataGrid on it and program your form as:
126
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
122
127
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
123
Reading one value at a time using Data Adapter and Data Set
Design a form with just a Label on it and program it as:
128
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
124
129
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
125
130
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
126
131
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499
127
Now, I leave up to you, how you will use these commands in your project.
Further, You can also add Exception handling in this.
132
Manjalpur-3290290,Sayajigunj-3290291,Racecourse-3249499