Professional Documents
Culture Documents
Learn Visual Basic 6.0: Review and Preview
Learn Visual Basic 6.0: Review and Preview
Custom Controls ! custom control is an extension to the standard Visual Basic tool ox. "ou use custom controls #ust as you would any other control. In fact, youve used $or at least seen% custom controls efore. &he common dialog ox, the BList ox, the BCom!o ox, and the B"rid tool, are all examples of custom controls. 'ustom controls can e used to add some really cool features to your applications. 'ustom controls are also referred to as #ctive$ controls. !ctive( is a technology newly introduced y )icrosoft to descri e what used to e known as OL% #utomation. *rior to Visual Basic +.,, the only way to create your own controls was to use ' or '--. .ow, with !ctive( technology, you can create your own controls knowing only Visual Basic/ 0f course, this would e a course y itself $and is%. &o use a custom control, you must load it into the tool ox. &o do this, choose Components from the Visual Basic Pro&ect menu. &he 'omponents $custom controls% dialog ox is displayed.
'
10-2
&o add a control, select the check ox next to the desired selection. When done, choose O( and the selected controls will now appear in the tool ox. 1ach custom control has its own set of properties, events, and methods. &he est reference for each control is the Microsoft Visual Basic Component Tools Guide manual that comes with Visual Basic 2.,. !nd, each tool also features on-line help. 3ere, well look at several custom controls and rief examples of their usage. !nd, well give some of the more important and uni4ue properties, events, and methods for each. &he main purpose here is to expose you to a few of these controls. "ou are encouraged to delve into the tool ox and look at all the tools and find ones you can use in your applications.
10-3
&he mas*ed edit control is used to prompt users for data input using a mask pattern. &he mask allows you to specify exactly the desired input format. With a mask, the control acts like a standard text ox. &his control is loaded y selecting the )icroso+t )as*ed %dit Control from the 'omponents dialog ox. *ossi le uses for this control include5 &o prompt for a date, a time, num er, or currency value. &o prompt for something that follows a pattern, like a phone num er or social security num er. &o format the display and printing of mask input data.
)asked 1dit *roperties5 )as* 6etermines the type of information that is input into the control. It uses characters to define the type of input $see on-line help for complete descriptions%. 'ontains data entered into the control $including all prompt characters of the input mask%.
1vent called when the data in the control changes. 1vent called when the data eing entered y the user does not match the input mask.
10-4
)asked 1dit 1xample5 Well use the masked edit control to o tain a phone num er. *lace a masked edit control on a form. 7et the masked edit controls )as* property e4ual to5 -.../0...0.... 7et the 1ont 'i2e property to 13. )y form now looks like this5
8un the example and notice how simple it is to fill in the phone num er. Break the application and examine the &ext property of the control in the Immediate Window. Chart Control
&he chart control is an ama9ing tool. In fact, its like a complete program in itself. It allows you to design all types of graphs interactively on your form. &hen, at run-time, draw graphs, print them, copy them, and change their styles. &he control is loaded y selecting )icroso+t Chart Control from the 'omponents dialog ox. *ossi le uses for this control include5 &o display data in one of many :6 or ;6 charts. &o load data into a grid from an array.
'hart 'ontrol *roperties5 ChartT4pe Random1ill 1sta lishes the type of chart to display. <sed to fill chart with random values $good for chcking out chart at design-time%. 6ata is normally loaded from a data grid o #ect associated with the chart control $consult on-line help%.
10-5
7tart a new application. !dd a chart control to the form. ! default ar graph will appear5
10-6
&hese few 4uick examples should give you an appreciation for the power and ease of use of the chart control. )ultimedia Control
&he multimedia control allows you to manage )edia 'ontrol Interface $)'I% devices. &hese devices include5 sound oards, )I6I se4uencers, '6-80) drives, audio players, videodisc players, and videotape recorders and players. &his control is loaded y selecting the )icroso+t )ultimedia Control from the 'omponents dialog ox. &he primary use for this control is5 &o manage the recording and play ack of )'I devices. &his includes the a ility to play '6s, record W!V files, and play ack W!V files.
When placed on a form, the multimedia control resem les the uttons you typically see on a V'85
10-7
&he multimedia control uses a set of high-level, device-independent commands, known as )C7 $media control interface% commands, to control various multimedia devices. 0ur example will show you what these commands look like. "ou are encouraged to further investigate the control $via on-line help% for further functions. )ultimedia 'ontrol 1xample5 Well use the multimedia control to uild a simple audio '6 player. *ut a multimedia control on a form. *lace the following code in the 1orm8Load 1vent5 Private Sub Form_Load() 'Set initial properties Form1.MMControl1.Notify False Form1.MMControl1.!ait "rue Form1.MMControl1.S#areable False Form1.MMControl1.$evi%e"ype &C$'udio& '(pen t#e devi%e Form1.MMControl1.Command &(pen& )nd Sub &his code initiali9es the device at run time. If an audio '6 is loaded into the '6 drive, the appropriate uttons on the )ultimedia control are ena led5
&his utton ena ling is an automatic process - no coding is necessary. &ry playing a '6 with this example and see how the utton status changes.
10-8
&he rich te,t!o, control allows the user to enter and edit text, providing more advanced formatting features than the conventional text ox control. "ou can use different fonts for different text sections. "ou can even control indents, hanging indents, and ulleted paragraphs. &his control is loaded y selecting the )icroso+t Rich Te,t!o, Control from the 'omponents dialog ox. *ossi le uses for this control include5 8ead and view large text files. Implement a full-featured text editor into any applications.
8ich &ext ox *roperties, 1vents, and )ethods5 )ost of the properties, events, and methods associated with the conventional text ox are availa le with the rich text ox. ! ma#or difference etween the two controls is that with the rich text ox, multiple font si9es, styles, and colors are supported. 7ome uni4ue properties of the rich text ox are5 1ile9ame 'el1ont9ame 'el1ont'i2e 'el1ontColor 'an e used to load the contents of a .txt or .rtf file into the control. 7et the font name for the selected text. 7et the font si9e for the selected text. 7et the font color for the selected text.
7ome uni4ue methods of the rich text ox are5 Load1ile 'ave1ile 8ich &ext ox 1xample5 *ut a rich text ox control on a form. *ut a com o ox on the form $we will use this to display the fonts availa le for use%. <se the following code in the 1orm8Load event5 Private Sub Form_Load() $im * 's *nte+er For * , "o S%reen.FontCount - 1 Combo1.'dd*tem S%reen.Fonts(*) Ne.t * )nd Sub 0pen a file and load the contents into the control. 7ave the control contents into a file.
10-9
8un the application. &ype some text. 3ighlight text you want to change the font on. =o to the com o ox and select the font. .otice that different areas within the text ox can have different fonts5
'lider Control
&he slider control is similar to a scroll ar yet allows the a ility to select a range of values, as well as a single value. &his control is part of a group of controls loaded y selecting the )icroso+t :indows Common Controls from the 'omponents dialog ox. *ossi le uses for this control include5 &o set the value of a point on a graph. &o select a range of num ers to e passed into an array. &o resi9e a form, field, or other graphics o #ect.
7lider 'ontrol 1xample5 Well uild a slider that lets us select a range of num er somewhere etween the extreme values of , to ?,,. *ut two la el oxes and a slider on a form5
7et the slider control 'mallChange to 1, LargeChange to 10, )in to 0, )a, to 100, Tic*1re<uenc4 to 10, and 'electRange to True. <se the following in the 'lider18)ouse own event5 Private Sub Slider1_Mouse$o2n(1utton 's *nte+er3 S#ift 's *nte+er3 . 's Sin+le3 y 's Sin+le) *f S#ift 1 "#en Slider1.SelStart Slider1.4alue Label1.Caption Slider1.4alue Slider1.SelLen+t# , Label5.Caption && )nd *f )nd Sub
&he ta!!ed dialog control provides an easy way to present several dialogs or screens of information on a single form using the same interface seen in many commercial Windows applications. &his control is loaded y selecting the 'heridan Ta!!ed ialog Control from the 'omponents dialog ox. &he ta ed dialog control provides a group of ta s, each of which acts as a container $works #ust like a frame or separate form% for other controls. 0nly one ta can e active at a time. <sing this control is easy. @ust uild each ta container as separate applications5 add controls, set properties, and write code like you do for any application. .avigation from one container to the next is simple5 #ust click on the corresponding ta . &a ed 6ialog 'ontrol 1xample5 ed dialog control on the form5
6esign each ta with some controls, then run the application. .ote how each ta in the folder has its own working space.
&he updown control is a pair of arrow uttons that the user can click to increment or decrement a value. It works with a !udd4 control which uses the updown controls value property. &his control is part of a group of controls loaded y selecting the )icroso+t :indows Common Controls from the 'omponents dialog ox. <p6own 'ontrol *roperties5 Value )in; )a, 7ncrement Orientation 'urrent control value. 1sta lish upper and lower control limits. !mount control value changes each time an arrow is clicked. 6etermines whether arrows lie hori9ontally or vertically.
<p6own 'ontrol 1vents5 Change =pClic* ownClic* Invoked when value property changes. Invoked when up arrow is clicked. Invoked when down arrow is clicked.
<p6own 'ontrol 1xample5 Well uild an example that lets us esta lish a num er etween ? and :+. !dd a updown control and a la el ox to a form. 7et the updown controls )in property to ? and )ax property to :+. &he form should resem le5
<se this simple code in the =p own18Change event, then give it a try5 Private Sub 6p$o2n1_C#an+e() Label1.Caption 6p$o2n1.4alue )nd Sub
!lmost all Windows applications these days use tool ars. ! tool ar provides 4uick access to the most fre4uently used menu commands in an application. &he tool!ar control is a mini-application in itself. It provides everything you need to design and implement a tool ar into your application. &his control is part of a group of controls loaded y selecting the )icroso+t :indows Common Controls from the 'omponents dialog ox. *ossi le uses for this control include5 *rovide a consistent interface etween applications with matching tool ars. *lace commonly used functions in an easily-accessed space. *rovide an intuitive, graphical interface for your application.
&o create a asic tool ar, you need to follow a se4uence of steps. "ou add uttons to a Button collection - each utton can have optional text andAor an image, supplied y an associated 7mageList control $another custom control%. Buttons can have tooltips . In more advanced applications, you can even allow your user to customi9e the tool ar to their liking/ !fter setting up the tool ar, you need to write code for the ButtonClic* event. &he index of the clicked utton is passed as an argument to this event. 7ince tool ar uttons provide 4uick access to already coded menu options, the code in this event is usually #ust a call to the respective menu items Clic* procedure. &ool ar 'ontrol 1xample Well look at the simplest use of the tool ar control - uilding a fixed format tool ar $pictures only% at design time. Well create a tool ar with five uttons5 one to create a new file, one to open a file, one to save a file, one to print a file, and one for help. *lace a tool ar and imagelist control on a form. 8ight click on the imagelist control to set the pictures to e used. <sing the 7mages ta , assign the following five images5 Image ? - .1W.B)*, Image : - 0*1..B)*, Image ; 7!V1.B)*, Image B - *8I.&.B)*, and Image + - 31>*.B)*
'lick O( to close this ox. .ow, right mouse click on the tool ar control. &he Propert4 Pages dialog ox will appear. <sing the =eneral ta , select the imagelist control #ust formed. .ow, choose the Buttons ta to define each utton5
.ote utton + is a placeholder $set 't4le property to t!rPlaceholder% that puts some space etween the first four uttons and the 3elp utton. When done, my form looked like this5
7ave and run the application. .ote the uttons #ust click - we didnt write any code $as mentioned earlier, the code is usually #ust a call to an existing menu items click event%. 'heck out how the tool tips work. Duick .ote on &ooltips5 )any of the Visual Basic controls support tooltips to inform the user of what a particular control. 7imply set individual controls ToolTipTe,t property to a non- lank text string to ena le this capa ility.
&o move text information to and from the clip oard, use the 'etTe,t and "etTe,t methods5 Clip!oard.'etTe,t Clip!oard."etTe,t *laces text in clip oard. 8eturns text stored in clip oard.
&hese methods are most often used to implement cutting, copying, and pasting operations. &o move graphics to and from the clip oard, use the 'et ata and "et ata methods5 Clip!oard.'et ata Clip!oard."et ata *laces a picture in clip oard. 8eturns a picture stored in clip oard.
When using the clip oard methods, you need to know what type of data you are transferring $text or graphics%. &he "et1ormat method allows that5 'lip oard.=etCormat$datatype% *ossi le datatypes are5 T4pe 661 conversation info 8ich text format &ext Bitmap )etafile 6evice-independent itmap 'olor palette Value 3BC,, 3BC,? ? : ; E F '4m!olic Constant v 'C>ink v 'C8&C v 'C&ext v 'CBitmap v 'C)etafile v 'C6IB v 'C*alette 8eturns True if the clip oard contents are of the type specified y datatype.
8un the application. 'lick the form to start the printing. 8elate the code to the finished drawing. &he est way to learn how to print in Visual Basic is to do lots of it. "oull develop your own approaches and techni4ues as you gain familiarity. <se 1ormPrint for simple #o s. Cor detailed, custom printing, youll need to use the Printer o #ect.
! modeless form can e left to go to other forms. ! modal form must e closed efore going to other forms. &he startup form is modeless. %,amples >oad Corm? I >oads Corm? into memory, ut does not display it Corm?.7how I >oads $if needed% and shows Corm? as modeless Corm?.7how v )odal I >oads $if needed% and shows Corm? as modal. Corm?.3ide I 7ets Corm?s Visi le property to Calse 3ide I 3ides the current form <nload Corm? I <nloads Corm? from memory and hides it. 3iding a form allows it to e recalled 4uickly, if needed. 3iding a form retains any data attached to it, including property values, print output, and dynamically created controls. "ou can still refer to properties of a hidden form. <nload a form if it is not needed any longer, or if memory space is limited. If you want to speed up display of forms and memory is not a pro lem, it is a good idea to Load all forms when your application first starts. &hat way, they are in memory and availa le for fast recall.
!ttach this code to the two command uttons Clic* events. Private Sub Command1_Cli%/() Form5.S#o2 vbModeless )nd Sub Private Sub Command5_Cli%/() Form:.S#o2 vbModal )nd Sub !dd a second form to the application $1orm3%. &his form will e modeless. *lace a command utton on the form. 7et its Caption to ?ide 1orm.
!ttach this code to the uttons Clic* event. Private Sub Command1_Cli%/() Form5.=ide Form1.S#o2 )nd Sub
!ttach this code to the uttons Clic* event. Private Sub Command1_Cli%/() Form:.=ide Form1.S#o2 )nd Sub )ake sure Corm? is the startup form $check the Pro&ect Properties window under the Pro&ect menu%. 8un the application. .ote the difference etween modal $Corm;% and modeless $Corm:% forms.
In the previous section, we looked at using multiple forms in a Visual Basic application. Visual Basic actually provides a system for maintaining multipleform applications, known as the )ultiple ocument 7nter+ace $) 7%. )6I allows you to maintain multiple forms within a single container form. 1xamples of )6I applications are Word, 1xcel, and the Windows 1xplorer program. !n )6I application allows the user to display many forms at the same time. &he container window is called the parent form, while the individual forms within the parent are the child forms. Both parent and child forms are modeless, meaning you can leave one window to move to another. !n application can have only one parent form. 'reating an )6I application is a two-step process. "ou first create the )6I form $choose #dd ) 7 1orm from Pro&ect menu% and define its menu structure. .ext, you design each of the applications child forms $set ) 7Child property to True%. 6esign-&ime Ceatures of )6I 'hild Corms5 !t design time, child forms are not restricted to the area inside the parent form. "ou can add controls, set properties, write code, and design the features of child forms anywhere on the desktop. "ou can determine whether a form is a child y examining its ) 7Child property, or y examining the pro#ect window. &he pro#ect window uses special icons to distinguish standard forms, )6I child forms, and )6I parent forms5
8un-&ime Ceatures of )6I 'hild Corms5 !t run0time, the parent and child forms take on special characteristics and a ilities. 7ome of these are5 ?. !t run-time all child forms are displayed within the parent forms internal area. &he user can move and si9e child forms like any other form, ut they must stay in this internal area.
:. 7et up the following menu structure5 Caption KCile K.ew K!rrange K'ascade K3ori9ontal &ile KVertical &ile K!rrange Icons 9ame mnuCile mnuCile.ew mnu!rrange mnu!rrangeItem mnu!rrangeItem mnu!rrangeItem mnu!rrangeItem 7ndented .o "es .o "es Index L , "es Index L ? "es Index L : "es Index L ;
;. !ttach this code to the mnu1ile9ew8Clic* procedure. &his code creates new child forms $named +rmChild - developed next%. Private Sub mnuFileNe2_Cli%/() $im Ne2$o% 's Ne2 frmC#ild Ne2$o%.S#o2 )nd Sub B. !ttach this code to the mnu#rrange7tem8Clic* procedure. &his esta lishes how child forms are displayed. Private Sub mnu'rran+e*tem_Cli%/(*nde. 's *nte+er) 'rran+e *nde. )nd Sub
2. !ttach this code to the 1orm8Resi2e procedure. &his insures that whenever a child window is resi9ed, the text ox fills up the entire window. Private Sub Form_0esi>e() "e.t1.=ei+#t S%ale=ei+#t "e.t1.!idt# S%ale!idt# )nd Sub 8un the application. 'reate new forms y selecting 9ew from the 1ile menu. &ry resi9ing forms, maximi9ing forms $notice how the parent form title ar changes%, minimi9ing forms, closing forms. &ry all the #rrange menu options.
0nce completed, your 3elp &ext Cile must e saved as an 8&C file. 3elp &ext Cile 1xample5 Well create a very simple help text file with three topics. I used Word 2., in this example. 'reate a document with the following structure and footnotes5
When done, save this file as '7)PL%.RT1 $8ich &ext Cormat%. 'reating the 3elp *ro#ect Cile5 &he ?elp Pro&ect 1ile contains the information re4uired y the 3elp 'ompiler to create the 3elp file. &he file is created using any text editor and must e saved as unformatted text $#'C77%. &he file extension is ?PA. &he 3elp *ro#ect Cile can contain up to nine sections, each of which supplies information a out the source file to compile. 7ections names are placed within s4uare rackets B C. 7emicolons are used to indicate a comment. 7ections can e in any order. &he sections are5 M0*&I0.7N MCI>17N MB<I>6&!=7N M'0.CI=N 7pecifies options for uild $optional%. 7pecifies 3elp &ext Ciles $8&C% $re4uired%. 7pecifies any uild tags $optional%. !uthor defined menus, macros, etc. $optional%
3elp *ro#ect Cile 1xample5 Cor our simple example, the 3elp *ro#ect Cile is e4ually simple5 M0*&I0.7N '0.&1.&7L3I6O'0.&1.&7 &I&>1L7I)*>1 !pplication 3elp MCI>17N 7I)*>1.8&C &his file specifies the context I6 of the &a le of 'ontents screen and the name of the 8&C file that contains the help text. 7ave this file as '7)PL%.?PA $in &ext, or !7'II format%.
'ompiling the 3elp Cile5 &his is the easiest step. &he help compiler is located in the cDEProgram 1ilesE ev'tudioEv!Ehc directory and is the program hc.e,e. "our file is compiled within the 607 window. 0nce in that window, move to the directory containing your 3*@ file and type5 c5P*rogram CilesP6ev7tudioPv PhcPhc filename.3*@ where filename is your 3elp *ro#ect Cile. &his process generates a inary help resource file and may take a long time to complete. !ny errors are pro a ly due to pro lems in the 8&C file$s%. &he created file has the same name as your 3elp *ro#ect Cile with an ?LP extension.
3elp Cile 1xample5 &o compile the example, at a 607 prompt, type5 cDEProgram 1ilesE ev'tudioEv!EhcEhc '7)PL%.?PA &he help file 7I)*>1.3>* will e created $if no errors occur% and saved in the same directory as your 3*@ file.
&his constant will declare the 3elp files index page upon invocation of Win3elp. &here are other constants that can e used with Win3elp - this is #ust a simple example. &he eclare statement and constant definitions usually go in the general declarations area of a code module and made Pu!lic. If you only have one form in your application, then put these statements in the general declarations area of your form $and declare them Private%. 0nce everything is in-place, to invoke the 3elp file from code, use the function call5 6im 8 !s >ong . . 8 L Win3elp$ startupform.hWnd, filename.3>*, 31>*OI.61(, '>ng$,%% where startupform is the name of your application main form and filename is the help file name, including path information.
)ove the mouse cursor to Topic1 and notice the cursor changes to a hand. 'lick there and the corresponding &opic ? screen appears5
&he ?7 8TOP7C1 text in the &a le of 'ontents screen links to the corresponding context I6 $the . footnote% in the topic page. &his link is a &ump. &he link to &opic : is a pop-up #ump, try it and youll see.
"ou can now select your topic of choice. )ore 3elp Cile &opics5 !fter all this work, you will still only have a simple help file, nothing that rivals those seen in most applications. &o improve your help system, you need to add some more stu++. Information on these advanced help topics is found in many Visual Basic references. ! ig feature of help systems is conte,t0sensitive help. With this, you place the cursor on or in something your interested in knowing a out and press 11. ! 3elp topic, if one exists, shows up. &he application is smart enough to know what you want help with. "raphics always spiff up a help system. 3elp systems use a special type of graphics called h4pergraphics. >astly, ?elp macros add functionality to your help system. &here are over +, macro routines uilt into the 6>> Win3elp application. If, after seeing the rather daunting tasks involved in creating a help system, you dont want to tackle the #o , take heart. &here are several third party software packages that assist in help system authoring and development. >ook at computer maga9ine advertisements $especially the Visual Basic Programmers Journal% for potential leads.