Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 38

10-1

Learn Visual Basic 6.0


10. Other Visual Basic Topics
Review and Preview In this last class, we look at a lot of relatively unrelated topics - a Visual Basic playground. Well cover lots of things, each with enough detail to allow you, as a now-experienced Visual Basic programmer, to learn more a out the topics that interest you.

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

Learn Visual Basic 6.0

&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.

Other Visual Basic Topics


)as*ed %dit Control

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%.

Te,t )asked 1dit 1vents5 Change Validation %rror

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

Learn Visual Basic 6.0

)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%.

Other Visual Basic Topics


0 viously, there are many more properties used with the chart control. We only look at these two to illustrate what can e done with this powerful control. 'hart 'ontrol 1xamples5

10-5

7tart a new application. !dd a chart control to the form. ! default ar graph will appear5

'hange the ChartT4pe property to a 5 and o tain a line chart5

10-6

Learn Visual Basic 6.0

or o tain a fancy ;6 chart y using a 'hart&ype of 65

&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

"ou should recogni9e uttons such as *lay, 8ewind, *ause, etc.

Other Visual Basic Topics


*rogramming the )ultimedia 'ontrol5

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

Learn Visual Basic 6.0

Rich Te,t!o, Control

&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.

Other Visual Basic Topics


<se the following code in the Com!o18Clic* event5 Private Sub Combo1_Cli%/() 0i%#"e.t1o.1.SelFontName Combo1."e.t )nd Sub

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.

10-10 Learn Visual Basic 6.0


7lider 'ontrol *roperties5 Value )in; )a, Tic*1re<uenc4 Tic*'t4le 'mallChange LargeChange 'electRange 'el'tart 'elLength 'urrent slider value. 1sta lish upper and lower slider limits. 6etermines how many ticks appear on slider. 6etermines how and where ticks appear. !mount slider value changes when user presses left or right arrow keys. !mount slider value changes when user clicks the slider or presses *g<p or *g6n arrow keys. 1na le selecting a range of values. 7tarting selected value. >ength of select range of values.

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

Other Visual Basic Topics 10-11


and this code in the 'lider18)ouse=p event5 Private Sub Slider1_Mouse6p(1utton 's *nte+er3 S#ift 's *nte+er3 . 's Sin+le3 y 's Sin+le) (n )rror 0esume Ne.t *f S#ift 1 "#en Slider1.SelLen+t# Slider1.4alue - Slider1.SelStart Label5.Caption Slider1.4alue )lse Slider1.SelLen+t# , )nd *f )nd Sub 8un the application. 1sta lish a starting value for the selected range y moving the slider to a desired point. &hen, click the slider thum while holding down the 7hift key and move it to the desired upper value.

10-12 Learn Visual Basic 6.0


Ta!!ed ialog Control

&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

7tart an application and put a ta

6esign each ta with some controls, then run the application. .ote how each ta in the folder has its own working space.

Other Visual Basic Topics 10-13


=p own Control

&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

10-14 Learn Visual Basic 6.0


Tool!ar Control

!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)*

Other Visual Basic Topics 10-15


When done, the image control should look like this5

'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

10-16 Learn Visual Basic 6.0


! new utton is added to the tool ar y clicking 7nsert Button. !t a minimum, for each utton, specify the ToolTipTe,t property, and the 7mage num er. Values I used are5 7nde, ? : ; B + 2 ToolTipTe,t .ew Cile 0pen Cile 7ave Cile *rint Cile -.one3elp me/ 7mage ? : ; B , +

.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.

Other Visual Basic Topics 10-17


=sing the :indows Clip!oard &he Clip!oard o #ect has no properties or events, ut it has several methods that allow you to transfer data to and from the Windows clip oard. 7ome methods transfer text, some transfer graphics. ! method that works with oth text and graphics is the Clear method5 Clip!oard.Clear 'lear the clip oard contents.

&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.

10-18 Learn Visual Basic 6.0


Printing with Visual Basic !ny serious Visual Basic application will need to use the printer to provide the user with a hard copy of any work done or results $text or graphics% o tained. *rinting is one of the more complex programming tasks within Visual Basic. Visual Basic uses two primary approaches to printing text and graphics5 "ou can produce the output you want on a form and then print the entire form using the Print1orm method. "ou can send text and graphics to the Printer o #ect and then print them using the 9ewPage and %nd oc methods. Well look at how to use each approach, examining advantages and disadvantages of oth. !ll of these techni4ues use the system default printer. "ou can also select a printer in Visual Basic, ut we wont look at that here. &he Print1orm method sends a pixel- y-pixel image of the specified form to the printer. &o print, you first display the form as desired and via code invoke the command5 Print1orm. &his command will print the entire form, using its selected dimensions, even if part of the form is not visi le on the screen. If a form contains graphics, they will e printed only if the forms #utoRedraw property is True. &he *rintCorm method is y far the easiest way to print from an application. But, graphics results may e disappointing ecause they are reproduced in the resolution of the screen, not the printer. !nd small forms are still small when printed. *rintCorm 1xample5 7tart a new application. *ut an image ox on the form. 7i9e it and set the 'tretch property to True. 7et the Picture property to some picture $metafiles are est, you choose%. !dd a la el ox. *ut some formatted text in the ox. )y form looks like this5

Other Visual Basic Topics 10-19


!dd this code to the 1orm8Clic* event5 Private Sub Form_Cli%/() PrintForm )nd Sub 8un the application. 'lick on the form $not the image or la el% and things should print. .ot too hard, huhG <sing the Printer o #ect to print in Visual Basic is more complicated, ut usually provides superior results. But, to get these etter results re4uires a it $and, at times, more than a it% of coding. &he *rinter o #ect is a drawing space that supports many methods, like Print, P'et, Current$, Current>, Line, PaintPicture $used to print contents of *icture oxes%, and Circle, to create text and graphics. "ou use these methods #ust like you would on a form. When you finish placing information on the *rinter o #ect, use the %nd oc method to send the output to the printer. &he 9ewPage method allows printing multi-page documents. &he *rinter o #ect also has several properties that control print 4uality, page si9e, num er of copies, scaling, page num ers, and more. 'onsult Visual Basic on-line help for further information. &he usual approach to using the *rinter o #ect is to consider each printed page to e a form with its own coordinate system. <se this coordinate system and the a ove listed methods to place text and graphics on the page. When complete, use the %nd oc method $or 9ewPage method if there are more pages%. !t that point, the page will print. &he main difficulty in using the *rinter o #ect is planning where everything goes. I usually use the 'cale method to define an E.+H y ??H sheet of standard paper in ,.,?H increments5 *rinter.7cale $,, ,% - $E+,, ??,,% I then place everything on the page relative to these coordinates. &he example illustrates the use of a few of these techni4ues. 'onsult other Visual Basic documentation for advanced printing techni4ues.

10-20 Learn Visual Basic 6.0


*rinter 0 #ect 1xample5 In this example, well first define a standard sheet of paper. &hen, well use the Line method to draw a ox, the Circle method to draw a circle, and the Print method to Idraw some text. 7tart a new application. We dont need any controls on the form - all the printing is done in the 1orm8Clic* procedure. Private Sub Form_Cli%/() Printer.S%ale (,3 ,)-(78,3 11,,) Printer.Line (1,,3 1,,)-(9,,3 :,,)3 3 1 Printer.Cir%le (9583 88,)3 :,, Printer.Current; 1,, Printer.Current< 7,, Printer.Print &"#is is some te.t.& Printer.)nd$o% )nd Sub ! few words on each line in this code. Cirst, we esta lish the printing area to e E+, units wide y ??,, units long. &his allows us to place items on a standard page within ,.,? inches. .ext, we draw a ox, starting ? inch from the left and ? inch from the top, that is ; inches wide and : inches high. &hen, a circle, centered at mid-page, with radius of ; inches is drawn. Cinally, a line of text is printed near the ottom of the page. &he %nd oc method does the printing for us. &he printed page is shown to the right.

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.

Other Visual Basic Topics 10-21


)ultiple 1orm Visual Basic #pplications !ll applications developed in this class use a single form. In reality, most Visual Basic applications use multiple +orms. &he #!out window associated with most applications is a common example of using a second form in an application. We need to learn how to manage multiple forms in our pro#ects. &o add a form to an application, click the 9ew Corm utton on the tool ar or select 1orm under the 7nsert menu. 1ach form is designed using exactly the same procedure we always use5 draw the controls, assign properties, and write code. 6isplay of the different forms is handled y code you write. "ou need to decide when and how you want particular forms to e displayed. &he user always interacts with the Iactive form. &he first decision you need to make is to determine which form will e your startup +orm. &his is the form that appears when your application first egins. &he startup form is designated using the Pro&ect Properties window, activated using the Visual Basic Pro&ect menu5
'tartup 1orm

10-22 Learn Visual Basic 6.0


!s mentioned, the startup form automatically loads when your application is run. When you want another form to appear, you write code to load and display it. 7imilarly, when you want a form to disappear, you write code to unload or hide it. &his form management is performed using various *e4words5 (e4word >oad 7how v )odeless 7how v )odal 3ide <nload Tas* >oads a form into memory, ut does not display it. >oads $if not already loaded% and displays a modeless form $default 'how form style%. >oads $if not already loaded% and displays a modal form. 7ets the forms Visi!le property to 1alse. Corm remains in memory. 3ides a form and removes it from memory.

! 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.

Other Visual Basic Topics 10-23


)ultiple Corm 1xample5 7tart a new application. *ut two command uttons on the form $ 1orm1%. 7et ones Caption to ispla4 1orm3 and the others Caption to ispla4 1orm5. &he form will look like this5

!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

10-24 Learn Visual Basic 6.0


!dd a third form to the application $ 1orm5%. &his form will e modal. *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%/() 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.

Other Visual Basic Topics 10-25


Visual Basic )ultiple ocument 7nter+ace -) 7/

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

'tandard +orm Child +orm Parent +orm

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.

10-26 Learn Visual Basic 6.0


:. When a child is minimi9ed, its icon appears on the )6I parent form instead of the users desktop. When the parent form is minimi9ed, the entire application is represented y a single icon. When restored, all forms are redisplayed as they were. ;. When a child form is maximi9ed, its caption is com ined with the parent forms caption and displayed in the parent title ar. B. By setting the #uto'howChildren property, you can display child forms automatically when forms are loaded $ True%, or load child forms as hidden $1alse%. +. &he active child forms menus $if any% are displayed on the parent forms menu ar, not the child form. 2. .ew child forms can e created at run-time using a special form of the im statement and the 'how statement $the example illustrates this process%. J. &he parent forms #ctive1orm property indicates which child form is currently active. &he #ctiveControl property indicates which control on the active child form has focus. E. &he #rrange command can e used to determine how the child forms and their icons $if closed% are displayed. &he syntax is5 !rrange style where style can take on these values5 't4le , ? : ; '4m!olic Constant v 'ascade v &ile3ori9ontal v &ileVertical v !rrangeIcons %++ect 'ascade all nonminimi9ed )6I child forms. 3ori9ontally tile all nonminimi9ed )6I child forms. Vertically tile all nonminimi9ed )6I child forms. !rrange icons for minimi9ed )6I child forms.

Other Visual Basic Topics 10-27


)ultiple-6ocument !pplication $)6I% 1xample5 Well create an )6I application which uses a simple, text ox- ased, editor as the child application. &here are a lot of steps, even for a simple example. 7tart a new application. 'reate a parent form y selecting ) 7 1orm from the 7nsert menu. !t this point, the pro#ect will contain an )6I parent form $) 71orm1% and a standard form $1orm1% which we will use as a child form. )ake )6ICorm? the startup form. We work with the parent form first5 ?. 7et the following properties5 'aption .ame Window7tate )6I 1xample frm*arent :-)aximi9ed

:. 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

10-28 Learn Visual Basic 6.0


.ow, well work with Corm? which will hold the child application5 +. 6raw a text ox on the form. 7et the following properties for the form and the text ox5 1orm15 'aption )6I'hild .ame Visi le Te,t15 >eft )ulti>ine 7crollBars &ext &op )y form resem les this5 'hild Corm &rue frm'hild Calse , &rue :-Vertical MBlankN ,

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.

Other Visual Basic Topics 10-29


Creating a ?elp 1ile 6uring this course, weve made extensive use of the Visual Basic on-line help system. In fact, one of the ma#or advances in software in the past few years has een improvements in such interactive help. !dding a help +ile to your Visual Basic application will give it real polish, as well as making it easier to use. "our help file will contain text and graphics information needed to e a le to run your application. &he help file will e displayed y the uilt-in Windows help utility that you use with every Windows application, hence all functions availa le with that utility are availa le with your help system. Cor example, each file can contain one or more topics that your user can select y clicking a hot spot, using a *e4word search, or !rowsing through text. !nd, its easy for your user to print any or all help topics. 'reating a complete help file is a ma#or task and sometimes takes as much time as creating the application itself/ Because of this, we will only skim over the steps involved, generate a simple example, and provide guidance for further reference. &here are five ma#or steps involved in uilding your own help file5 ?. 'reate your application and develop an outline of help system topics. :. 'reate the ?elp Te,t 1ile $or &opic Cile% in 8&C format. ;. 'reate the ?elp Pro&ect 1ile $3*@%. B. 'ompile the 3elp Cile using the ?elp Compiler and *ro#ect Cile. +. #ttach the 3elp Cile to your Visual Basic application. 7tep ? is application-dependent. Well look riefly at the last four steps here. )ore complete details, including formatting and file structure re4uirements, are availa le in many Visual Basic references.. 'reating a 3elp &ext Cile5 &o create a ?elp Te,t 1ile, you need to use a word processor capa le of saving documents in rich-text format $RT1 %. :ord and :ordPer+ect do admira le #o s. "ou must also e familiar with text formatting procedures such as underlining, dou le-underlining, typing hidden text, and using footnotes. &his formatting is used to delineate different parts of the help file. "ou should make sure all formatting options are visi le when creating the 3elp &ext Cile.

10-30 Learn Visual Basic 6.0


&he 3elp &ext Cile is asically a cryptically encoded list of h4perte,t #umps $#ump phrases% and context strings. &hese are items that allow navigation through the topics in your help file. 7ome general rules of 3elp &ext Ciles5 &opics are separated y hard page reaks. 1ach topic must have a uni4ue context string. 1ach topic can have a title. ! topic can have many keywords attached to it to ena le 4uick access utili9ing a search facility. &opics can have uild-tag indicators and can e assigned a rowse se4uence. @umps can e to another secondary window or to another file.

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

Other Visual Basic Topics 10-31


7ome things to note5 Topic1 and Topic5 $hypertext #umps% are dou leunderlined to indicate clicka le #umps to topics. Topic3 is single-underlined to indicate a #ump to a pop-up topic. &he words ?7 8TOP7C1, ?7 8TOP7C3, and ?7 8TOP7C5 $context strings% are formatted as hidden text. .ote page reaks separate each section. 6o not put a page reak at the end of the file. !lso, note the use of footnotes. &he . footnote is used to specify a 3elp context I6, the @ provides &opic &itles for searching, and ( yields search keywords. &he footnotes for this example are5

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%

10-32 Learn Visual Basic 6.0


MBI&)!*7N M!>I!7N M)!*N MWI.60W7N MB!==!=1N 7pecifies any itmaps needed for uild. 'an e used to specify context strings to topics $optional%. !ssociates context strings with num ers. <sed with context-sensitive help $optional%. 6efines primary and secondary windows $re4uired only if secondary windows used%. >ists files to e included in ?LP file.

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.

Other Visual Basic Topics 10-33


!ttaching the 3elp Cile5 &he final step is to attach the compiled help +ile to your application. !s a first step, open the Pro&ect Properties window under the Pro&ect menu. <nder ?elp 1ile, select the name of your ?LP file y clicking the ellipsis $ ...%. &his ties the help file to the application, ena ling the user to press 11 for help. "ou can also add a 3elp item somewhere in your menu structure that invokes help via its Clic* event. If you do this, you must write code to invoke the help file. &he code involves a call to the Windows !*I function, :in?elp. But, after last class, were not daunted y such functions, are weG Cirst, we need the function declaration $from the !*I &ext Viewer%5 6eclare Cunction Win3elp >i Quser;:Q !lias QWin3elp!Q $ByVal hwnd !s >ong, ByVal lp3elpCile !s 7tring, ByVal w'ommand !s >ong, ByVal dw6ata !s >ong% !s >ong We also need a constant $also from the !*I &ext Viewer%5 'onst 31>*OI.61( L K3; R 6isplay index

&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.

10-34 Learn Visual Basic 6.0


3elp Cile 1xample5 We can now try our example help file in a Visual Basic application. Well only use the 11 option here. 7tart a new application. Bring up the Pro&ect Properties window via the Pro&ect menu. 7elect the correct 3elp Cile y clicking the ellipsis and finding your newly created file. 'lick O(. .ow, run your application $I know theres nothing in the application, ut thats all right%. 0nce, its running press 11. &his 3elp screen should appear5

)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.

Other Visual Basic Topics 10-35


=o ack to the &a le of 'ontents screen and click the 'earch utton. ! dialog ox displaying the help files list of keywords appears. In our example, the three topics all have the same keyword $the ( footnotes%, '7)PL% Topics . When you dou le-click on this keyword, you see all the associated topic titles $the @ footnotes%5

"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.

10-36 Learn Visual Basic 6.0


Class 'ummar4 &hats all I know a out Visual Basic. "ou should now have a good readth of knowledge concerning the Visual Basic environment and language. &his readth should serve as a spring oard into learning more as you develop your own applications. Ceel free to contact me, if you think I can answer any 4uestions you might have. Where do you go from hereG With Visual Basic 2.,, you can extend your knowledge to write We - ased applications, develop massive data ase frontends using Visual Basics powerful data ase tools and techni4ues, and even develop your own !ctive( $custom% controls. 0ther classes cover such topics. !nd, the last example5

Other Visual Basic Topics 10-37 %,ercise 10 The =ltimate #pplication


6esign an application in Visual Basic that everyone on the planet wants to uy. 6raw o #ects, assign properties, attach code. &horoughly de ug and test your application. 'reate a distri ution disk. Cind a distri utor or distri ute it yourself through your newly created company. Become fa ulously wealthy. 8emem er those who made it all possi le y rewarding them with #o s and stock options. )4 'olutionD

7till working on it ...

10-38 Learn Visual Basic 6.0

&his page intentionally not left lank.

You might also like