16431625-Visual-Basic-6

download 16431625-Visual-Basic-6

of 35

Transcript of 16431625-Visual-Basic-6

  • 7/24/2019 16431625-Visual-Basic-6

    1/35

    VISUAL BASIC 6..A HAND BOOK

    Visual Basic 6 ..Programming with Visual Stuio 6

    !his articl" tr# to h"l$ %"ginn"rs o& 'Visual Basic' an

    tr# to "li("r a com$l"t" VB6 Bi%l". I us" sim$l"st

    m"tho to "scri%" "ach s"ction. Also inclu" som"

    a(anc" $rogramming tutorial too.I w"lcom" all

    t#$" o& sugg"stions &rom all si"s to ma)" this %oo)

    outstaning. I thin) %oth t"ach"rs an stu"nts will

    acc"$t this articl" with &ull min. I also w"lcom"

    thos" who woul li)" co*authoring with m"..

    Cont"nts

    An introduction to Visual Basic

    Basic Features of Visual Basic

    IDE Features of Visual Basic

    Declaring and using Variables

    Work with variables

    Control structures in Visual Basic

    Event driven programming under Windows latform

    Comments!Documentation

    "nderstanding and creating Visual Basic ro#ect$

    Advance Visual Basic rogramming

    An introduction to Visual Basic

    Microsoft Windows Operating is the worlds largest using computer operatingsystem today. Because of its user friendly features and its ability to customize itbecame No 1.

    Microsoft Visual studio is the programming solution is a power full tool fordeeloping new pac!ages under Windows platform.

    Visual studio "roide many language under this pac!age# Visual Basic was thesimplest among them. Visual Basic as the e$tension of B%&'( language# whichwas support only ()' interface *+i!e (ommand Mode# ,isc Operating&ystem-.his interface has changed into a new interface called /)'. 't has many

    http://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#An_introduction_to_Visual_Basichttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Basic_Features_of_Visual_Basichttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#IDE_Features_of_Visual_Basichttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Declaring_and_using_Variableshttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Work_with_variableshttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Control_structures_in_Visual_Basichttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Event_driven_programming_under_Windows_Platformhttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Comments(2F)Documentationhttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Understanding_and_creating(C2)(A0)Visual(C2)(A0)Basic_Project(2E)http://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Advance_Visual_Basic_Programminghttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#An_introduction_to_Visual_Basichttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Basic_Features_of_Visual_Basichttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#IDE_Features_of_Visual_Basichttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Declaring_and_using_Variableshttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Work_with_variableshttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Control_structures_in_Visual_Basichttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Event_driven_programming_under_Windows_Platformhttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Comments(2F)Documentationhttp://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Understanding_and_creating(C2)(A0)Visual(C2)(A0)Basic_Project(2E)http://knol.google.com/k/manoj-ap/visual-basic-6a-hand-book/3bx1ymwndxj4/13#Advance_Visual_Basic_Programming
  • 7/24/2019 16431625-Visual-Basic-6

    2/35

    adantages oer ()' and allows users to deelop smart applications using e$tbo$es# list bo$es etc. Visual Basic is a powerful programming system that allowsus to create application that fully ma!es use of facilities of /raphical )serinterface */)'-.

    Visual Basic programming system is a window based productiity tool. 'tsupports an eent ,rien programming model rather than conentionalprocedure oriented model. 't proides a rapid application enironment# a rich setof easy to use debugging tools.

    How it works

    In traditional applications% the application itself controls which portion of code e&ecute

    and in what se'uence$ E&ecution starts with the first line of the code and follows apredefined path through the application% calling procedure as needed$

    Visual Basic applications are eent drien. 'n an eent drien application# thecode doesnt follow a predefined path# it e$ecute different code sections inresponse to eents.

    0ents can be triggered by the users actions by messages from the system orother applications or eent from the application itself. he seuence of theseeents determines the seuence in which the code e$ecutes# thus the paththrough the applications code differs each time the program runs.

    With most languages# if you ma!e a mista!e in composing your code the error iscaught by the compiler when you start to compile your application. 2ou must

    then find and fi$ the errors and begin the compile cycle again# repeating theprocess for each error you found. 'n Visual Basic VB interprets your code as enterit# catching and highlighting most synta$ or spelling error on the fly. 't much li!esan e$perts eye oer your shoulder as moe through the codes.

    'n addition to this adanced error handling techniues# Visual Basic alsopartially compile the code as it is entered. When you are ready to rub theprogram# it ta!es only small amount of time to finish the compilation. 'f thecompiler find s and error# it is highlighted and you can ma!e correction andcontinue running the program# in short you need not go through the compilationcycle again.

  • 7/24/2019 16431625-Visual-Basic-6

    3/35

    Visual Basic IDE

    Basic Features of Visual Basic

    Visual Basic supports following features

    1. Data Access Features3 this allows programmers to deelop database front endapplications and serer side components for most popular database formats including M&&4+ and other ,atabases.

    5. Active X Technologies:that allows programmers to use the functionality proidedby other applications# such as M& Office 6Word# 0$cel etc- and other windowsapplications. 2ou can een automate applications and ob7ects created using theprofessional or 0nterprise editions of Visual Basic.

    8. Internet capabilitiesma!e it easy to proide access to documents and applicationsacross internet serer applications.

    9. our finished application is a true e!ecutable*.e$e- file that use a isual BasicVirtual Machine that you can freely distribute.

    ID+ ,"atur"s o& Visual Basic

    Visual Basic posses a 'ntegrated ,eelopment 0nironment 6',0: in whichusers can deelop# run and debug their applications. Visual basic is a ;front end

    which is used for design a screen 6(alled form: and also writing codes for thespecific controls.

    here are different type applications we can deelop with isual basic enterpriseedition. hese types are referred as "ro7ects< in isual basic we create pro7ectswhich include different types of modules and many forms. % good pro7ect mayhae many forms as it can hae. =ollowing are the ma7or type of pro7ects we canhae in Visual Basic

    http://knol.google.com/k/-/-/3bx1ymwndxj4/ej5c9k/vbide.bmp
  • 7/24/2019 16431625-Visual-Basic-6

    4/35

    -. Stanar +"/ A standard E(E pro#ect is a t)pical application in which we can

    use the database manipulation$

    "# Active $X$% Active D&&: hese types of pro7ect are aailable with theprofessional edition. %ctie > components are basic code building components

    that dont hae a isible interface and that can add special functionality to ourapplications.

    '# Active X (ontrol:)sing this type of pro7ect we can create our own VisualBasic controls.

    )# Active X Docu*ent $X$% Active X Docu*ent $X$: %ctie >,ocuments are in essence Visual Basic applications that can run in theenironment of container that supports hyper lin!ing# such as internet e$plorer.

    +# Data ,ro-ect: it is identical to the &tandard 0>0 pro7ect type# but it

    automatically adds the controls that are used in accessing database to the toolbo$. i also adds the database %ctie > ,esigner to the pro7ect e$plorer window.%ctie > ,esigners are Visual tools for accessing and manipulating databases andgenerating reports.

    .# DHT/& Applications: )sing this pro7ect user can be deeloping webpages that can be displayed in the browsers window on the client computer.

    0# II1 Applications:Visual Basic allows user to build application that run onthe web serer and interact with clients oer the internet with the internetinformation serer.

    2# Add3in: We can create our own add?in for the VB ',0. %dd?ins is thespecial commands we can add to Visual Basic menus. %dd?ins helps us to e$tendthe VB ',0.

    *ew ro#ect Dialog Bo&

    VB ID+ 0inows an Com$on"nts

    From the N"w Pro1"ct winow we can select one t)pe of pro#ect +We alread)

    mentioned different t)pes of VB ro#ects we can create,$ -he window have three tabs

    namel)% *ew% E&isting% .ecent$ -he *ew Window Displa) the options to start *ewro#ect$ -he items in this window is Standard EXE, Active X EXE, Active X DLL , Active

    X Control, VB Application Wizard, AddIn, Active X Document EXE, Active X Document

    DLL. -he ne&t tab shows /ptions to open e&isting pro#ects and the last tabs show help)ou to find recentl) worked pro#ect or the recent created programs$

  • 7/24/2019 16431625-Visual-Basic-6

    5/35

    B) 0electing /ur First 0tandard E(E ro#ect we can move to the window as shown

    below$ -his is the main IDE window of Visual Basic$ 1ou can see the form in the design

    section% in which )ou can design )our application b) using the items of tool bo&$

    !ool Bo/

    Items of tool bo& are used to design the application interface$ -hese items are called as

    Controls$ Controls are basicall) $/C( files$ 1ou can add additional controls to tool bo&

    using ro#ect 2Components or b) ressing Ctrl 2 !$

    -he tools bo& contains La%"l3 !"t %o3 Pictur" Bo3 Imag"3 List Bo3 Com%o Bo3

    O$tion Button3 Dir List Bo "tc$ -o place a control on the form first select the control

    with the help of mouse and move mouse over the form$ When the mouse is on the form%

    the curser turned into a 345 and we can draw the control on the form #ust as drawing a

    rectangle in 60 aint$ -he items of tools bo& arranged in 37eneral5 -ab% )ou can rearrange controls in separate tabs b) creating custom tabs b) right click the -ool Bo&$ "se

    Drag and drop to add the items to -abs$

    -he default items available on the tools bo& are8

    9$ La%"l/-his control displa)s a te&t on the form that the user can5t edit$ :abels are

    commonl) used to identif) the controls$ -he label bo& can be identified b) a name

    b) setting a name propert) +this can be done at design time onl),$ -he te&t in thelabel control can be change b) changing the caption propert)$

    ;$ !"t Bo8 -his control displa)s the te&t that the user can edit$

    $ (heck bo! controlrepresents one or more choice that the user canselect. One of the important property of ;(hec! bo$ control is ;Value. 'fthe chec! bo$ is mar!ed it will be ;1 other wise it will be ;@.

    ?$ 4ption button3 Option ButtonsAadio Buttons appears in a group# andthe user can choose only one of them. 'ts ;Value "roperty will be Trueif itis selected. Other wise it will be false.

    @$ &ist Bo!3 his contains a list of options from which user can choose onemore items. 6'n windows the Font &ist bo! is an e$ample of the use oflist bo$.:. he &elected item in a +istBo$ is gien by the Textproperty thecontrol meanwhile sorted property determines whether the items in thelist bo$ will be sorted or not.

    $ (o*boBo!3 his control is similar to the +istBo$# but it contains a e$tA0dit field. he user can either select an item from the list or enter a newstring in 0dit field. he item selected from the list gien by the controlse$t"roperty.

  • 7/24/2019 16431625-Visual-Basic-6

    6/35

    $ Ti*er:When we need to perform tas!s at regular interal we can ma!euse of Ti*er# he main property of imer (ontrol isInterval,determines how often the timer notifies our application.

    9$ ,icture Bo!:his control is used to display images and images set withPictureproperty. he "icture Bo$ control also supports few functionality

    of generating adanced drawing.99$ 1hape:his control is used to draw graphical elements such as bo$es# andcircles etc.

    9;$ I*age:his one is similar to "icture Bo$ but it support less functionalityand ma!e use of less resources.

    9

  • 7/24/2019 16431625-Visual-Basic-6

    7/35

    Pro1"ct +$lor"r

    ro#ect e&plorer displa)s /pened pro#ect and other pro#ect components such as Class"sand 4oul"s. We can start multiple pro#ects at the same time )ou have to select thepro#ect )ou want to work with b) selecting 30et as 0tart "p5$ -he pro#ect e&plorer has

    three Buttons on the -op of the window% namel) Vie! "b#ect, Vie! Code $,5%, &o''le(older.-he first two used to move between Code Window and Form Designer$ -he third

    button helps )ou to change the li)tin' )tyleof ro#ect E&plorer$ We can add 6ultipleForms 0DI 6DIG and as man) modules and classes we needed$

    ,orm La#out

    Form :a)out Window is used to arrange the position of form while running the program$

    ,orm D"sign"r

    -he form designer is the main window in the middle of the screen% in which we candesign and edit user interface$ -he same window displa)s a te&t editor in which we can

    enter and edit applications code$ -he form designer displa)s two windows% the form it

    self and the code window$ 0elect the form )ou want to view in the pro#ect e&plorer% andthen click one of two view buttons to see form or its code$

    VB +itor Co" 0inow

    In Visual Basic% the editor is called code window% it is actuall) a turbo charged te&t editor

    with man) productivit) tool built in$ we can open the code window b) double clicking a

    form or control in the form$ If we double click a form we will be taken into procedure for

    that control$ /nce the code window is open% we can go to an) procedure for an) ob#ect onthe selected form$

    O%1"ct Brows"r

    /b#ect browser allows us to browse through the Various roperties events and methodsthat are made available or e&posed$ We can access it b) selecting ob#ect browser from the

    view menu or b) pressing ,7$

    Declaring and using Variables

    % ariable is a name gien to a location in andom %ccess Memory *%M- wherea data alue is stored temporarily. he contents of the ariable will lost when theprogram is ended# we can also alter alues stored in a ariable during life time ofthe program. Visual basic Variables can store string of e$t# numbers# ob7ects#Boolean alues etc. for efficiency in sorting and using data isual basic proides adata type that store specific type of data.

    Data T5pes and their 6ange

  • 7/24/2019 16431625-Visual-Basic-6

    8/35

    Data T5pes 1torage 1i7e 6ange of Variables

    Byte 1 byte @ to 5DD

    rue or =alseBoolean

    'nteger; b)tes 5 bytes ?85#EFG to 85#EFE

    ?5#19E#9HH#F9H to 5#19E#9HH#F9E:ong

    &ingle= b)tes 9 bytes E digits

    19 digitsDouble

    (urrency b)tes H bytes 9 decimal place

    @ to appro$imately 5 billion0tring

    %ny ob7ect referenceOb7ect9 b)tes

    Variant*Withnumber-= b)tes

    1F bytes %ny numeric alue up to a rangeof ,oubles.

    &ynta$3 ? ,im Iariable nameJ %s I,taypeJ

    Before we ma!e use of ariables we need to declare first# in general case. henaming conention is that# name of ariables

    Begin with letter

    (ant contain an embedded period or embedded type declaration character.

    Must not e$ceed 5DD characters

    Must be uniue with in the scope# which is the range from which theariable can

    referenced a procedure# a form# so on

    We can mi$ characters and numbers together. We ma!e use of !ey words DIMand ;AS to declare a ariable.

    Visual Basic has two types of ariable declarations

    1- 0$plicit declaration and

  • 7/24/2019 16431625-Visual-Basic-6

    9/35

    K"lace ;4ption e!plicit statement in the declaration section of a =orm#(lass# or

    Modules

    5- 'mplicit declaration.

    'n the first we use a statement to define the ,ata ype of the ariable. hestatement does not assign a alue to the ariable but merely tell isual basic whatalues it can contain.

    0$ample3 ? ,im name %s &tring

    Variable declared without type is called implicitdeclaration. hese are calledvariants# i.e. we can store any desired alue in it.

    0$ample3 ? ,im &tr

    &tr L1@@1

    &tr L/0(

    1cope of Variables

    Variables are not necessarily aailable through out the entire program. heir usecan be localized to certain parts of the program only. +ocalizing in this ariable isa part of good programming practice. he parts of program in which a ariable

    can use is termed as ;scope of the variable./lobal ariable orPublicariablescan be declared in Generalsection of the of the =orm. Variable declared in thesubprogram orFunctions are local *"riate- to thatSubprogram orFunctiononly.

    /odule 1cope Variables

    Visual Basic also has module scope ariables which are declared in the generalsection of a (ode Module. Module scope ariables aailable through out all theprocedure in that Module and not in other modulesA=orm *if you want to do so#you must use the !eyword ;Public to declare them-.2ou can create module leelariables by declaring them with thePrivate!eyword in the declaration sectionat the top of the Module. hey can be accessed by procedures and functions inthat module.

    Note: -2ou cant declare public ariables within a procedure# only within the,eclaration section of a module.

    (lass /odule Variables

  • 7/24/2019 16431625-Visual-Basic-6

    10/35

    here is another important ariable concerning local ariable# they only e$istwithin the (lass Module. (lass module has limited access to the ariable# they canonly access by means of the ob7ectA ariables of that class.

    1tatic Variables

    'n addition to scope# ariables hae life time# the period of time during whichthey retain their alue. he alues in module?leel and public ariables arepresered for the lifetime of your application. Coweer# local ariables declaredwith ,im e$ist only while the procedure in which they are declared is e$ecuting.)sually# when a procedure is finished e$ecuting# the alues of its local ariablesare not presered and the memory used by the local ariables are not presereand the memory used by the local ariables is declared. he ne$t time theprocedure is e$ecuted< all its local ariables are reinitialized.

    Coweer# you can presere the alue of a local ariable by ma!ing the ariable

    ;static. y using the Static !e"#or$ to $eclare one or more variables insi$e aproce$ure% exactl" as "ou #oul$ #ith the Dim statement.

    &Preserve values even #hen the proce$ure% usin' the Static (ariable is en$e$.

    Constants

    Often you will find that your code contains a constant alue that reappears oerand oer. Or you may find that code depends on certain number that are difficultto remember numbers that# in and of them hae no obliious meaning.

    'n this case# you can greatly improe the readability of your code and ma!e iteasier to maintain?by using constants. % constant is a meaning full name thatta!es the place of a number or string that does not change. %lthough a constantsomewhat resembles a ariable# you cant modify a constant# or assign a newalue to it as you can to a ariable. here are two sources for constants.

    1. 'ntrinsic (onstants:which are proided by application and controls. Otherapplication li!e M& Office also proides a list of constants you can use with theirob7ect# method# properties. (onstants are also defined in the ob7ect library foreach %ctie > (ontrol.5. &ymbolic or )ser defined (onstants: By ma!e use of the !eyword ;)onstyou (an create constants of any type.0g3 (onst pa L 1588

    Data (onversion

    %s we !now ariables are used to store manipulate data we handle with the proprogram we build. here are occasions that we are conert data into another ,ataype. Cere is a list of inbuilt function to do the 7ob.

  • 7/24/2019 16431625-Visual-Basic-6

    11/35

    Function

    (boolConvert An E&pression to(byteBoolean(curB)te(dateCurrenc)

    (,blDate(intDouble(+ngInteger(&ng:ong

    &ingle

    (&tr &tring

    CVar(V0rrVariant

    Error

    8ork with variables

    Now we !now different type of pro7ects and how to ariables and ob7ects. +etsstart our first ariable pro7ects.

    Q) Read Principle Amount, Interest Rate, Term [Period] andcalculate Interestamont and Net amount

    1. &tart &tandard 0>0 "ro7ect5. ,ouble (lic! the =orm to reach the (ode Window or press ;=E.8. hen locate /eneral &ection from the ,rop ,own list bo$ that appear top ofthe code window6+eft &ide:9. ype the following code

    ,im %mount %s 'nteger,im 'rate %s 'nteger,im erm %s ,ouble,im 'amount,im NetD. ) can use comment section to ,escribe the code as follows in any part ofthe code window9 his program calculate &imple 'nterestF. Now we need to ead and manipulate the alues. %dd following codes to

  • 7/24/2019 16431625-Visual-Basic-6

    12/35

    (lic! 0ent of =orm# 6&elect the (lic! eent from the Ne$t list bo$ from the ightside of the code window:.

    "riate &ub =orm(lic!*-

    NetL@ ; 0mpty Values in Variables *'nitializing Pero Values to

    Variables-

    'amountL@

    %mountL'nputBo$ *Q0nter "rinciple %mount#Variable est-

    'rateL 'nputBo$ *Q0nter 'nterest ate#Variable est-

    ermL 'nputBo$ *Q0nter ermA"eriod#Variable est-

    'amoutL'rateK%mountA1@@Kerm

    NetL'amountR%mount

    "rint Q %mount#'amout

    "rint QNet amount# Net

    0nd &ub

    E. Now eady to est the pro7ectH. "ress =D or =ind &tart Button from ool Bar A un Menu

    KCere !Input"o#$is used to 'nput Values# it has two arguments first one is itsmessage and second is Optional title# and i.e. u can aoid it. ) can also leae theargument by inserting .

    K$Print$ is used to display alues to =orm.

    $!ercise:

    1. ead wo Numbers and do Basic (alculations *R# ?# K# A-S

  • 7/24/2019 16431625-Visual-Basic-6

    13/35

    K emember =irst thin! of ariables and their type# you need to create.

    K 'f u has to display decimal points use ,ata ypes li!e ,ouble

    5. ead a number and fin (ube*$8-S

    8. &ole the euation *aRb-5S

    Control structur"s in Visual Basic

    % control structure allows you to control the flow of your programs e$ecution. 'fleft unchec!ed by control?flow statements# a programs logic will flow throughstatement from left to right and right to bottom. While some ery simpleprograms can be written with only this unidirectional flow# and while some flowcan be controlled by using operators to regulate precedence of operations# mostof the power and utility of nay programming language comes from its ability to

    change statement order with structures and loops.

    Decision /aking 1tate*ents

    ,ecision ma!ing statements useful when we need to ealuate particularconditions and diert the e$ecution of the program in another pathAflow.

    VB support two decision ma!ing statements

    If;;Then###;else;;$nd if

    If;# Then##;else If;##$nd If

    1elect case;##;$nd 1elect

    If;;Then###;else;;$nd if

    &yna$3 'f *(ondition- hen

    Irue Bloc! &tatementsJ

    0lse

    I=alse Bloc! &tatementsJ

  • 7/24/2019 16431625-Visual-Basic-6

    14/35

    0nd if

    If;#Then##;else If;##$nd If

    &yna$3

    'f *(ondition- hen

    Irue Bloc! &tatementsJ

    0lse 'f I(onditionJ hen

    Irue Bloc! &tatementsJ

    0lse

    I=alse Bloc! &tatementsJ

    0nd if

    1elect (ase

    1inta!:

    &elect (ase*0$pression-

    (ase 3Value1

    &tatement1

    (ase 3Value 5

    0tatement ;

    $$$$$$$$$$$$$$$$$$$$$$$

    $$$$$$$$$$$$$$$$$$$$$$$

    Case Else

    0tatement

  • 7/24/2019 16431625-Visual-Basic-6

    15/35

    End 0elect$

    he 1elect (ase structure compares one e$pression to different alues. he1elect (asestructure tests a single e$pression which is ealuated once at the top

    of the structure. he result of the test is then compare with seeral alues and if itmatches one of the corresponding bloc! of statement is e$ecuted. he bloc! of(ase $lse statement is optional and is e$ecuted non of of the preious (aseValues match the e$pression.

    +("nt ri("n $rogramming un"r 0inows Plat&orm

    As we know% Visual Basic is a vent driven progra**ing laguage#,rogra* responds to specific action known as $vents# An $vent is an5action perfor*ing b5 using an5 of input devices% pri*aril5 *ouse ad

    ke5board# 8hen we click on specific buttons and *enus% action wasfired#

    =or the easy handling of these eents# Visual Basic uses events proce$ures.*+ater #e #ill $iscuss about $ifferent t"pe of proce$ures,Visual Basic &ystemcreates eent procedures. We can add codeAcommands that are meant toe$ecute.

    Q0nent ,rien programming carries many adantages oer conentionalprogramming techniues. he first one is its ease of use and manipulates data inmany ways.

    =or the best practice of eent programming# we need to understand some basiceents.

    Form is the container of other controls that are available in the !ool Bo

    +ets chec! out few of the important eents of =orm

    1. Load.Load event occure when for loaded to the screen.

    5. (lick.0ent occures when user clic! on the form

    8. Activate. his eent occure when we ma!e use of the form

    9. Initiali7e.. is triggered 7ust before +oad 0ent

    Comm"ntsDocum"ntation

  • 7/24/2019 16431625-Visual-Basic-6

    16/35

    (omments are the none e$ecutable sections of the programm. We can use thisfacility to add descriptions of code or bloc! specific areas of the code.

    We can use a 9 *&ingle uote- to comment a section of the code. 't can beappeared any place on the code window# no matter it is within the 0ent

    procedure or within /eneral &ection.

    =or e$ample3

    "riate &ub =orm1(lic! *-

    ;,isplay a &tring to e$t Bo$ (ontrol

    e$t1.e$ L Welcome to Tnol

    0nd sub

    to convert it to Integer Value#?The Te!t propert5 of Te!t Bo! return@bring the input te!t#

  • 7/24/2019 16431625-Visual-Basic-6

    17/35

    'n the (lear ButtonUs (lic! 0ent we need yo add necessary codes to clear theBo$es.

    "riate &ub O!Btn(lic!*-H

    H 0ample DocumentationH -his 0ection Clear the Field

    H

    e$t1.e$tL e$t8.e$tL e$t8.e$tL

    0nd &ub

    Now 2ou are ready to test your first pro7ect. "ress F+

    emember we can programmed same thing in different ways as the programmerwish to do things.

    - Buil a Sim$l" Imag" Vi"w"r

    -o Build a 0imple Image Viewer rogram we need a Image%Drive :ist%Director):ist%File :ist%:abel and :ist Controls$

    Image and icture Controls area common in nature$ Both of them have Pictur"propert)which is used to load picture to the control$In icturecontrol )ou can see that the picture

    is appeared in its actual siJe at the same time the Imagecontrol lets wrap it to the control

    with help of Str"tchropert)% Image also support some additional drawing functions$$Both the controls have a methods called :oadicture which help us to load a specific file%

    all we have to do is specif) the path of that picture to be shown$

    Label*Label control i) u)ed to di)play a tet. It can+t be u)ed to input value.It+) mainproperty i)Caption, !ic di)play tet 'iven.

    Drive Li)t,Directory Li)t,(ile Li)t 8-heseControls work as a batch% i$e% the) are meant towork together$

    (rame Control is use to group controls% so that we can move them together$We change

    the caption propert)to K0elect a FileK$

    Arrange these controls as shown below$

  • 7/24/2019 16431625-Visual-Basic-6

    18/35

    9$ *ow 0et 0tretch propert) ofIma'e boto !ru".;$ Add a label control #u)t above te(ile Li)tcontrol and )et it+) caption a) -(ile

    &ype-

    +ist1.%dd'tem K.bmp+ist1.%dd'tem K.7pg+ist1.%dd'tem K.gif$nd 1ub,rivate 1ub &ist(lick=-=ile1."attern L +ist1.e$t$nd 1ub

    Cow 5ou are read5 to test 5our application# ou can change the icon ofthe for*#

  • 7/24/2019 16431625-Visual-Basic-6

    19/35

    " (reating a calculator

    %&nderstandin' control array

    In Visual Basic% we can use copies of one control instance% and the) act as a group% -he)are also called as HControl arra)H$ :et us build calculator$First up all we need buttons for showing the Digits +L,$

    1. 'nsert a button and size it and copy the control and paste# when the systemprompt for 2es or No# (lic! on the 2es button.

    5. Ne$t we need 9 more buttons*array of controls- for Operationhandling*&uch as R#?#$#A-#

    'nsert a button and copy paste# 8 times.

    1. +ast# we need four more buttons *not arrayAcopy- for QL# ON# O==# (0

    functionality and add a te$t bo$.

    Now you are ready for coding.

    Declare variables first in the 7eneral section$

    ,im $# y %s 'nteger

    ,im optr %s &tring

    9$ %dd following codes in the (lic! 0ent of)omman$utton(*code

    window-. Cit =E to ino!e the code window.

    ,rivate 1ub (o**and(lick=inde! As Integer>

    H-hese codes take necessar) action to displa) the inserted value into the -e&t bo&$ 0incethe Buttons are Harra)% we onl) need a single line of code$

    e$t1.e$t L e$t1.e$t inde$

    $nd 1ub

    his section store information regarding the operations re'uired b) the user and it storedetails as a string to variable HoptrH% so that we can make use of it in the ne&t section$

    ,rivate 1ub (o**and"(lick=inde! As Integer-

  • 7/24/2019 16431625-Visual-Basic-6

    20/35

    &elect (ase *inde$-

    (ase @

    optr L R

    (ase 1

    optr L ?

    (ase 5

    optr L K

    (ase 8

    optr L A

    0nd &elect

    U&tore alue into ariables and clear the te$t bo$.

    $ L Val*e$t1.e$t-

    e$t1.e$t L

    $nd 1ub

    Now we are ready for the operations.%ll we need is to chec! optr ariable putnecessary operation and set the out put to the te$t bo$. %dd following codes tothe eualto*L- buttonUs (lic! 0ent.

    ,rivate 1ub (o**and'(lick=>

    y L Val*e$t1.e$t-

    &elect (ase *optr-

    (ase R

    z L $ R y

    (ase ?

    z L $ ? y

  • 7/24/2019 16431625-Visual-Basic-6

    21/35

    (ase K

    z L $ K y

    (ase A

    z L $ A y

    0nd &elect

    e$t1.e$t L z

    $nd 1ub

    he following code

    add ON O== functionality to the program. %ll we need is to disable and enablecontrols# we are using. =or this purpose we used the loop structure since we areusing two control arrays it become simple.

    ,rivate 1ub (o**and)(lick=>

    his section ON the calculator.

    =or i L @ o (ommand1.)Bound

    (ommand1.'tem*i-.0nabled L rue

    Ne$t i

    =or i L @ o (ommand5.)Bound

    (ommand5.'tem*i-.0nabled L rue

    Ne$t

    (ommand8.0nabled L rue

    e$t1.0nabled L rue

  • 7/24/2019 16431625-Visual-Basic-6

    22/35

    $nd 1ub

    Now you are ready to run your (alculator.

    Note3

    1. 2ou can change your "rograms icon and caption as you li!e.

    5. Ma!ing your pro7ect a complete stand alone application reuire one morestep.

    /o to =ile?JMa!e and enter name for it. Now the application is a stand alone

    calculator# which means that it doesnUt need VB to run the application.

    8. Cr"at" a thum% nail (i"w"r o& imag"s.9Un"rstaning control arra# :

    %s we already learned control array is the copy of a control. Cere we are going toe$periment with the =ile system controls and image bo$ as control array.

    9$ =irst we need to Draw Drive &ist bo!% Director5 &ist bo! and file

    list bo!;$ &et the =ile +ist Bo$sPatternpropert5 as ?#-pg to aoid accidentalloading of files

    $ %dd following codes torive list *o# , irectory list *o#es?$ &et Visibleproperty o+ ile(toalse*Cide it-

    ,rivate 1ub Dir(hange=>

    =ile1."ath L ,ir1."ath

    $nd 1ub

    ,rivate 1ub Drive(hange=>

    On 0rror /oo tt3

  • 7/24/2019 16431625-Visual-Basic-6

    23/35

    ,ir1."ath L ,rie1

    0$it &ub

    tt3

    MsgBo$ he diice cannot acces

    $nd 1ub

    0 .%dd following codes to forms load eent.

    ,rivate 1ub For*&oad=>

    +ist1.%dd'tem K.7pg

    +ist1.%dd'tem K.bmp

    +ist1.%dd'tem Kgif

    ,rie1 L 03X ;set initial drie

    ,ir1."ath L unni ; set initial directory

    'mage1.&tretch L rue ;resize the picture for image bo$.

    $nd 1ub

    2#%dd following code to the list bo$ clic! eent# it ta!e necessary action whenyou select file type from the list bo$ and resetpatternproperty of the =ile listbo$.

    ,rivate 1ub &ist(lick=>

    =ile1."attern L +ist1.e$t

    $nd 1ub

    E# Now it is time to +oad thumbnail iews . o do this we use the image bo$array. &ince it is an array we can use some looping structure.

    %dd following codes to (ommand Buttons clic! 0ent

    ,rivate 1ub (o**and(lick=>

  • 7/24/2019 16431625-Visual-Basic-6

    24/35

    ,im '# ( %s 'nteger

    ( L =ile1.+ist(ount

    MsgBo$ (

    'f ( J 5G hen

    ( L 5G

    0nd 'f

    't also chec!s the count of the files# which not be greater than that of no of imagebo$es. 'f it e$ceed we need to reset the alue

    =or ' L @ o (

    =ile1.+ist'nde$ L '

    'mage1.'tem*'- L +oad"icture*,ir1."ath A =ile1.=ileName-

    Ne$t

    $nd 1ub

    #We also need an additional =orm which we can View picture. %dd aima'e*o#to it and set stretcproperty alue to true.On the first form # ,ouble clic!on the image bo$*array- and add following code to display clic!ed "icture.

    ,rivate 1ub I*ageDbl(lick=Inde! As Integer>

    =orm5.&how

    =orm5.'mage1."icture L 'mage1.'tem*'nde$-."icture

    $nd 1ub

    #%dd code to display file location as tool tip of the thumb iew.

    ,rivate 1ub I*age/ouse/ove=Inde! As Integer% Button As

  • 7/24/2019 16431625-Visual-Basic-6

    25/35

    Integer% 1hift As Integer%X As 1ingle% As 1ingle>

    'mage1.'tem*'nde$-.oolipe$t L ,ir1."ath =ile1.=ileName

    $nd 1ub

    Now you can test your pro7ect

    Note3 emember to double clic! the folder

    ; . 0or) with Varia%l" ... Som" Basic Varia%l" O$"rations

    Following program will help )ou to learn more about variables and help )ou to

    understand how can we control flow of the variable using H decision making $$$

    statementsH$-his program will read *ame %Mualification and 6ark secured$ and also calculate inde&mark according to the option user using$

    9$ Create a form as follows +"se &et Boe), Combo Bo, "ption button),Label) and cec boe)% and declare following variables in the 7eneral section 0o

    as we can access them through out all part of the form$

    ;$ Dimin3 tot AsInt"g"r

    H0/.-0 L 9>

    H . Cross L >Check Bo& have three values%9%; % =means unchecked%-means checked and 7for

    gra)ed

  • 7/24/2019 16431625-Visual-Basic-6

    26/35

    If Check9$Value O 9 -hen

    ind& O 9ElseIf Check;$Value O 9 -hen

    ind& O 9>

    ElseIf Check

    ElseIf Check9$Value O 9 And Check;$Value O 9 And Check 4 >End If

    As for the Check bo& % option too have a Value propert) which have onl) two value

    -rue!False

    If /ption9$Value O -rue -henind& O ind& 4 ;

    ElseIf /ption;$Value O -rue -hen

    ind& O ind& 4 9>

    ElseIf /ption

    End If

    -e&t>$-e&t O ind&

    -e&t?$-e&t O ind& 4 Val+-e&t

  • 7/24/2019 16431625-Visual-Basic-6

    27/35

    End If

    +n Su%H 9< is the ke) value for the Enter Pe)% so this event will be triggered when we hit the enter ke)$

    Pri(at" Su% !"t8=== * Val9!"tB:

    !"t;.!"t >=== * Val9!"tC:

    !"t>.!"t >=== * Val9!"tD:

    !"t6.!"t >=== * Val9!"t+:

    !im"r-.+na%l" !ru"

    +n Su%

    ;.Now w" n"" som" %asic calculations an n"" to raw ln"s 9as Chart Bars:

    Pri(at" Su% !im"r-

    D. A&t"r this tas) w" also ha(" to sto$ th" !im"r.

    http://knol.google.com/k/manoj-ap/creating-a-chart-with-picture-box/3bx1ymwndxj4/37http://knol.google.com/k/manoj-ap/creating-a-chart-with-picture-box/3bx1ymwndxj4/37
  • 7/24/2019 16431625-Visual-Basic-6

    28/35

    ,or i - !o ->==

    Pictur"-.Lin" 9= 2 i3 !"t7.!"t:*9= 2 i3 Pictur"-.H"ight:3 (%"

    Pictur"-.Lin" 9->== 2 i3 !"t8.!"t:*9->== 2 i3 Pictur"-.H"ight:3 (%?r""n

    Pictur"-.Lin" 98=== 2 i3 !"t;.!"t:*98=== 2 i3 Pictur"-.H"ight:3 (%Blu"

    Pictur"-.Lin" 9;>== 2 i3 !"t>.!"t:*9;>== 2 i3 Pictur"-.H"ight:3 (%"llow

    Pictur"-.Lin" 96=== 2 i3 !"t6.!"t:*96=== 2 i3 Pictur"-.H"ight:3 (%4ag"nta

    N"t i

    !im"r-.+na%l" ,als"

    +n Su%

    Advance Visual Basic ,rogra**ing

    -.Cr"at" Color Pic)"rShaing +&&"ct to a $ictur"

    his tutorial of bF e$plain how a programmer can create both a picture colorpic!er and color shader.In Visual Basic ,a development environment, a coder can create picture color

    picker, in which user can pick the color of the picture and apply it to another .Thisprogram use some of the uilt in functions.

    I think this is a good source for oth students and teachers as well.

    To do the work we have to SetPi%e"and GetPi%e" uilt in !unctions "dd these declaration as follows to the #eneral section of the $ode %odule

    &rivate 'eclare !unction #et&i(el Li )gdi*+) ByVal hdc "s Long, ByVal - "sLong, ByVal "s Long/ "s Long

    &rivate 'eclare !unction 0et&i(el Li )gdi*+) ByVal hdc "s Long, ByVal - "sLong, ByVal "s Long, ByVal cr$olor "s Long/ "s Long

  • 7/24/2019 16431625-Visual-Basic-6

    29/35

    0econdly we have to create the 0hadepicture !unction , to this &lease "ddthese code to the %odule

    0hadeicture

    &ulic 0u 0hade&icture&ic0ource "s &ictureBo(, &icTarget "s &ictureBo(,ith$olor "s Long, Thickness "s Integer/

    2n 3rror 4esume 5e(t

    'im s4ate, $ol "s Long

    'im -, "s 0ingle

    'im -%a(, %a( "s 0ingle

    'im cBlue, c#reen, c4ed "s 'oule 6'etermines the pi(el color

    'im sBlue, s#reen, s4ed "s 'oule 6'etermines the 07"'I5# color

    6#etting the 4#B values of selected color

    sBlue 8 !i(ith$olor 9 +:;/ 9 +:;/

    s#reen 8 !i(ith$olor < sBlue = +:;/ = +:;// 9 +:;/

    s4ed 8 !i(ith$olor < sBlue = +:;/ = +:;/ < s#reen = +:;//

    6$alculate screen height > width of the image

    -%a( 8 &ic0ource.idth 9 0creen.Twips&er&i(el- < 1

    %a( 8 &ic0ource.7eight 9 0creen.Twips&er&i(el < 1

    6Initialising 0hading

    &icTarget.$ls

    s4ate 8 Thickness 9 1?

    6&rocess all pi(els and alter them accordingly

  • 7/24/2019 16431625-Visual-Basic-6

    30/35

    !or - 8 ? To -%a(

    !or 8 ? To %a(

    $ol 8 #et&i(el&ic0ource.hdc, -, /

    If 5ot $ol 8 ? Then 6Because lack colors are usually the orders of an

    image and never change order color.It will affect the clarity.

    6#etting the 4#B values of current pi(el

    cBlue 8 !i($ol 9 +:;/ 9 +:;/

    c#reen 8 !i($ol < cBlue = +:;/ = +:;// 9 +:;/

    c4ed 8 !i($ol < cBlue = +:;/ = +:;/ < c#reen = +:;//

    64esetting the 4#B values of current pi(el with the s4ate of shading

    c4ed 8 c4ed @ s4ed < c4ed/ = s4ate

    c#reen 8 c#reen @ s#reen < c#reen/ = s4ate

    cBlue 8 cBlue @ sBlue < cBlue/ = s4ate

    If 5ot $ol 8 1+;*++:; Then 0et&i(el &icTarget.hdc, -, , 4#Bc4ed,

    c#reen, cBlue/ 60kipping transparent col and setting the pi(el

    3lse

    0et&i(el &icTarget.hdc, -, , $ol

    3nd If

    5e(t

    &icTarget.4efresh

    5e(t -

    3nd 0u

    5ow ou are ready to Test our !irst 0hading &rogram ,By using this ou can#ive any color you like to the pictures you using in a VB &roAect

  • 7/24/2019 16431625-Visual-Basic-6

    31/35

    In order to test the &rogram you need a !orm which contain Three Pi$turebo%control+/riginal% rocessed,Colorallet as icture 9 , a $ro"" bar, anda !i$ture o- $o"or !a""et and another as atet !i$ture.

    Then "dd these $odes

    &rivate 0u 2riginalC%ouse'ownButton "s Integer, 0hift "s Integer, - "s0ingle, "s 0ingle/

    %sgBo( 2riginal.&oint-, /

    3nd 0u

    &rivate 0u &icture1C%ouse'ownButton "s Integer, 0hift "s Integer, - "s0ingle, "s 0ingle/

    0hade&icture 2riginal, &rocessed, &icture1.&oint-, /, scr4ate.Value

    3nd 0u

    &rivate 0u scr4ateC$hange/

    lThick 8 scr4ate.Value

    3nd 0u

    &rivate 0u !ormCDnload$ancel "s Integer/

    $lipoard.0etTe(t )httpE99tech

  • 7/24/2019 16431625-Visual-Basic-6

    32/35

    5ow ou are ready to test your proAectou will get the followign result as you move through colors

    7. USIN? !H+ SH+LL87.DLL in VB6

    0hell

  • 7/24/2019 16431625-Visual-Basic-6

    33/35

    Pri(at" Su% Comman-=

  • 7/24/2019 16431625-Visual-Basic-6

    34/35

    Dim shll

  • 7/24/2019 16431625-Visual-Basic-6

    35/35

    rivate 0ub CommandRClick+,

    Dim shll