Professional Documents
Culture Documents
Programming in VB NET PDF
Programming in VB NET PDF
NET
Rohit Birla
RohitKumarBirla@gmail.com
RohitBirla_rb@yahoo.com
Your Success, Our Strength TM
Rohit Birla
RohitKumarBirla@gmail.com
RohitBirla_rb@yahoo.com
2
Your Success, Our Strength TM
Preface
It gives me great pleasure in presenting this book. Programming in VB.NET is
written with several goals in mind. It is designed to cover all essential and basic
information you will need to start your programming in VB.NET.
I hope this book will help you to start your programming in .NET.
In spite of all efforts some errors might be there. I shall be grateful to the
readers if the same are brought to my notice. Suggestions and comments for
further improvements of the book will be gratefully acknowledged.
Rohit Birla
RohitKumarBirla@gmail.com
RohitBirla_rb@yahoo.com
3
Your Success, Our Strength TM
4
Your Success, Our Strength TM
Dedicate to
5
Your Success, Our Strength TM
6
Your Success, Our Strength TM
Contents:
PREFACE ................................................................................................................................................................ 3
CONTENTS: ............................................................................................................................................................ 7
LAYER 1: ....................................................................................................................................................................18
LAYER 2: ....................................................................................................................................................................18
LAYER 3: ....................................................................................................................................................................18
MANAGE CODE ...........................................................................................................................................................18
GARBAGE COLLECTION PROCESS .....................................................................................................................................18
NAMESPACES: .............................................................................................................................................................19
ASSEMBLY: .................................................................................................................................................................19
EXPLANATION: .............................................................................................................................................................20
7
Your Success, Our Strength TM
LOOPING.............................................................................................................................................................. 26
Assignment: ...................................................................................................................................................................... 26
INHERITANCE ....................................................................................................................................................... 35
EXPLANATION: .............................................................................................................................................................41
EXAMPLE: ...................................................................................................................................................................41
INTERFACE ........................................................................................................................................................... 42
8
Your Success, Our Strength TM
EXPLANATION: .............................................................................................................................................................42
ABSTRACT CLASS VS. INTERFACE ......................................................................................................................................42
EXAMPLE1 ..................................................................................................................................................................42
EXAMPLE2 IMPLEMENTING MULTIPLE INTERFACES ..............................................................................................................43
EXAMPLE3 INHERITING INTERFACES .................................................................................................................................44
EXAMPLE ....................................................................................................................................................................45
ENUMERATION .................................................................................................................................................... 45
EXAMPLE: ...................................................................................................................................................................46
CREATING DIRECTORY....................................................................................................................................................52
COPY ONE FILE TO ANOTHER ...........................................................................................................................................52
GETTING DRIVE INFORMATION........................................................................................................................................52
9
Your Success, Our Strength TM
STACK ........................................................................................................................................................................56
QUEUE .......................................................................................................................................................................57
TRY-CATCH .................................................................................................................................................................60
TRY THROW CATCH .................................................................................................................................................60
TRY THROW MULTIPLE CATCH ..................................................................................................................................61
EXCEPTION CLASS.........................................................................................................................................................62
USING EXCEPTION CLASS................................................................................................................................................62
TRY THROW CATCH FINALLY ...................................................................................................................................63
NESTED TRY ................................................................................................................................................................64
NESTED TRY EXAMPLE ................................................................................................................................................64
EXAMPLES OF VARIOUS EXCEPTIONS.................................................................................................................................65
USER DEFINED EXCEPTION ..........................................................................................................................................65
USER DEFINED EXCEPTION EXAMPLE .............................................................................................................................66
Assignment ....................................................................................................................................................................... 66
LABEL ................................................................................................................................................................... 73
TEXTBOX .............................................................................................................................................................. 74
FORM................................................................................................................................................................... 75
10
Your Success, Our Strength TM
BUTTON ............................................................................................................................................................... 76
CHECKBOX ........................................................................................................................................................... 79
TRACKBAR ........................................................................................................................................................... 81
VSROLLBAR .......................................................................................................................................................... 82
EXAMPLE: ...................................................................................................................................................................82
Assignment ....................................................................................................................................................................... 82
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
COMBOBOX ......................................................................................................................................................... 91
EXAMPLE: ...................................................................................................................................................................91
11
Your Success, Our Strength TM
COLORDIALOG ..................................................................................................................................................... 93
EXAMPLE: ...................................................................................................................................................................93
FONTDIALOG ....................................................................................................................................................... 94
EXAMPLE: ...................................................................................................................................................................94
FOLDERBROWSERDIALOG .................................................................................................................................... 95
EXAMPLE: ...................................................................................................................................................................95
OPENFILEDIALOG ................................................................................................................................................. 96
EXAMPLE: ...................................................................................................................................................................96
ADOBEPDF ........................................................................................................................................................... 97
EXAMPLE: ...................................................................................................................................................................97
NUMERICUPDOWN .............................................................................................................................................. 98
EXAMPLE: ...................................................................................................................................................................98
MONTHCALENDAR............................................................................................................................................... 99
DATETIMEPICKER ................................................................................................................................................. 99
EXAMPLE: ...................................................................................................................................................................99
EXAMPLE: .................................................................................................................................................................101
CONFIGURE LISTBOX FOR MULTIPLE SELECTIONS ...............................................................................................................102
EXAMPLE: .................................................................................................................................................................104
CONTEXTMENUSTRIP......................................................................................................................................... 107
12
Your Success, Our Strength TM
MEDIAPLAYER.................................................................................................................................................... 111
EXAMPLE: .................................................................................................................................................................115
NOTE: ......................................................................................................................................................................121
NOTIFYICON....................................................................................................................................................... 122
Assignment: .................................................................................................................................................................... 124
ADO.NET ................................................................................................................................................................125
BASIC STEPS TO FOLLOW:.............................................................................................................................................125
DISPLAYING ALL RECORDS AT A TIME USING DATA ADAPTER AND DATA SET ...........................................................................126
SEARCHING USING DATA ADAPTER AND DATA SET ............................................................................................................127
READING ONE VALUE AT A TIME USING DATA ADAPTER AND DATA SET .................................................................................128
READING ONE VALUE AT A TIME USING COMMAND AND DATA READER ..................................................................................129
SEARCHING USING COMMAND: .....................................................................................................................................130
SEARCHING USING COMMAND AGAIN: ............................................................................................................................131
INSERTION INTO TABLE USING COMMAND:......................................................................................................................132
DELETION USING COMMAND: .......................................................................................................................................132
UPDATE USING COMMAND: .........................................................................................................................................132
13
Your Success, Our Strength TM
.NET History
.NET History
.NET 1.0 2002
.NET 1.1 2003
.NET 2.0 2005
.NET 3.0 2007
.NET 3.5 2008
.NET 4.0 2010
.NET 2010 is the latest version of .NET. We will be using the same in our programs.
14
Your Success, Our Strength TM
Types of Applications
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
.NET Architecture
17
Your Success, Our Strength TM
.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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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.
21
Your Success, Our Strength TM
Boolean
Byte
Char
DateTime
Decimal
Double
Int16
Int32
Int64
SByte
Single
TimeSpan
Write the following code in your Console Application code window
22
Your Success, Our Strength TM
Key Points:
23
Your Success, Our Strength TM
Key Points:
24
Your Success, Our Strength TM
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
25
Your Success, Our Strength TM
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
Your Success, Our Strength TM
27
Your Success, Our Strength TM
but redim will delete all the data stored in array, to preserve that we use preserve keyword
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
Your Success, Our Strength TM
Using Select-Case
Function overloading
29
Your Success, Our Strength TM
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
30
Your Success, Our Strength TM
Creating constructor
31
Your Success, Our Strength TM
Overloading constructor
32
Your Success, Our Strength TM
Defining Properties
Key Points:
33
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
Inheritance Example
36
Your Success, Our Strength TM
Example
Main1
37
Your Success, Our Strength TM
Main2
38
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
Interface
Explanation:
~ An alternative to abstract classes
~ less formality as compared to abstract classes
Example1
42
Your Success, Our Strength TM
Example2
Implementing multiple interfaces
43
Your Success, Our Strength TM
Example3
Inheriting interfaces
44
Your Success, Our Strength TM
Example
Enumeration
Key points:
45
Your Success, Our Strength TM
Example:
46
Your Success, Our Strength TM
Understanding console
Creating Namespace
47
Your Success, Our Strength TM
1. Go to project menu and choose <your application> properties. For me, its consoleapplication1
properties
2. Choose debug tab from left pane and in command line textbox, write your arguments
48
Your Success, Our Strength TM
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
Your Success, Our Strength TM
50
Your Success, Our Strength TM
51
Your Success, Our Strength TM
Creating directory
52
Your Success, Our Strength TM
Understanding System.Environment
53
Your Success, Our Strength TM
Understanding My.Computer
Checking various keys like caps lock, num lock, shift etc
Getting OS platform
Getting culture
File System
54
Your Success, Our Strength TM
Assignment
Try out various function of My.Computer.FileSystem at your own.
55
Your Success, Our Strength TM
Using System.Collections
Stack
56
Your Success, Our Strength TM
Queue
57
Your Success, Our Strength TM
Assignment
There are lot many functions of strings. Try them at your own.
58
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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.
Try-Catch
60
Your Success, Our Strength TM
61
Your Success, Our Strength TM
Exception Class
- Its base class of all other exception classes
- Used basically to handle those exception which programmer either forget to handle
or programmer was not able to classify
- Also used to define user defined exceptions
62
Your Success, Our Strength TM
63
Your Success, Our Strength TM
Nested Try
In case inner try was not able to handle the exception, it will search catch in outer try.
64
Your Success, Our Strength TM
65
Your Success, Our Strength TM
Assignment
Try some examples on exceptions at your own
66
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
Change text properties from properties menu. I am here not changing name properties of controls; you
can change them as per your convenient.
70
Your Success, Our Strength TM
1. Form
2. Button
3. CheckBox
4. CheckListBox
5. ColorDialog
6. Combo
7. ContextMenu
8. DataGrid (at a later stage)
9. DateTimePicker
10. DomainUpDown
11. ErrorProvider
12. FontDialog
13. GroupBox
14. HScrollBar
15. Label
16. LinkLabel
17. ListBox
18. ListView
19. MaskedTextBox
20. MenuStrip
21. MonthCalendar
22. NotifyIcon
23. NumericUpDown
24. OpenFileDialog
25. Pannel
26. PictureBox
27. ProgressBar
28. RadioButton
29. RichTextBox
30. SaveFileDialog
31. StatusStrip
32. TabControl
33. TextBox
34. Timer
35. ToolTip
36. ToolStrip
37. TrackBar
38. VScrollBar
71
Your Success, Our Strength TM
39. MediaPlayer
40. WebBrowser
41. AdobePdf
42. TableLayout
43. FlowLayout
44. FolderBrowserDialog
45. PrintDialog
46. PrintForm
47. Animation Control
48. Drawing Graphics
49. MessageBox
50. InputBox
These controls are described in an order to make the reader comfortable and to make learning more
interesting.
72
Your Success, Our Strength TM
Label
Properties window:
1. AutoSize : True ( Label will automatically adjust its size )
2. BackColor : to change its background color
3. BorderStyle : single or 3D
4. ContextMenuStrip : To change right click menu; we will look in detail about this option
5. Cursor : you can change mouse cursor which should appear on this label
6. Dock : Where and how this label should appear
7. FlatStyle : Looks of label
8. Font
9. ForeColor : text color of label
10. Image: Image which will appear on label
11. ImageAlign : Left / Right / Center
12. RightToLeft : If yes, text will appear from right to left
13. Text : text to be appear on label
Events:
73
Your Success, Our Strength TM
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.
Events:
74
Your Success, Our Strength TM
Form
Properties window:
1. BackgroundImage
2. BackgroundImageLayout
3. ControlBox = False ( minimize, maximize and close button will not appear)
4. FormBorderStyle ( Try all these options and see effects)
5. MaximizeBox = False (Maximize button will get disable on form)
6. MinimizeBox
7. Opacity (to set transparency of Form, Try using with 80% and 50%, Default is 100%)
8. ShowInTaskbar = False (When you execute your form, your form will not appear in Taskbar)
9. Text = to change title bar text of form
10. 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
Your Success, Our Strength TM
Button
Properties window:
Properties are similar to textbox and label, try them at your own.
Events:
76
Your Success, Our Strength TM
2. Program it as
3. Now execute it
77
Your Success, Our Strength TM
Again Confusion!!
1. Design same form again
2. Program it as
78
Your Success, Our Strength TM
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
Your Success, Our Strength TM
Result:
80
Your Success, Our Strength TM
TrackBar
Properties Window:
1. Minimum and Maximum value
2. RightToLeft
3. Orientation : Horizontal or Vertical
Events:
Example:
81
Your Success, Our Strength TM
VSrollBar
Example:
Assignment
Try HScrollBar at your own.
82
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
85
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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.
87
Your Success, Our Strength TM
RadioButton
Example:
Design your form as:
88
Your Success, Our Strength TM
A Big Confusion!!!
Design your form as:
And now execute your form and try to select one option from both groups!!!
89
Your Success, Our Strength TM
Confusion Solved
Drag GroupBox from toolbox and design your form as:
90
Your Success, Our Strength TM
ComboBox
Example:
Design your form as:
And execute it, choose one day from combo box and click submit.
91
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
FontDialog
Example:
Drag font dialog on your form and design your form with a button and textbox and program it as:
94
Your Success, Our Strength TM
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.
95
Your Success, Our Strength TM
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.
96
Your Success, Our Strength TM
AdobePdf
Example:
Drag AdobePdf, OpenFileDialog and a button on your form and program it as:
97
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
ListBox
Example:
Simply drag a ListBox and a button on your form and program it as:
101
Your Success, Our Strength TM
And now we have to retrieve all selected items, so we will program button_click as:
102
Your Success, Our Strength TM
103
Your Success, Our Strength TM
CheckListBox
Example:
Drag a CheckListBox and a button on form and program it as:
104
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
MediaPlayer
Drag MediaPlayer, 3 buttons and an OpenFileDialog on your form and design your form as:
111
Your Success, Our Strength TM
Media Player has lots of properties. Try out all those properties.
112
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
117
Your Success, Our Strength TM
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!
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
Your Success, Our Strength TM
119
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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:
121
Your Success, Our Strength TM
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
Your Success, Our Strength TM
Note: Dont forget to set its icon property from property menu.
123
Your Success, Our Strength TM
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
Your Success, Our Strength TM
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
Your Success, Our Strength TM
Displaying all records at a time using Data Adapter and Data Set
1. Open connection
2. Use Data Adapter to read from connection
3. Use Data Set to store data
4. And then display data from Data Set
Open windows application and drag DataGrid on it and program your form as:
126
Your Success, Our Strength TM
127
Your Success, Our Strength TM
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
Your Success, Our Strength TM
129
Your Success, Our Strength TM
130
Your Success, Our Strength TM
131
Your Success, Our Strength TM
Now, I leave up to you, how you will use these commands in your project.
132
Your Success, Our Strength TM
MillenniumYear
Serial: 29062010172738
133