Search In Site

29 June, 2013

Adding ActiveX Controls To DHTML Pages In Visual Basic

Adding ActiveX Controls To DHTML Pages
You can add ActiveX controls to DHTML pages just as you can to standard Visual Basic projects just use the Project Components menu item to open the Components dialog box and select the ActiveX control you want to add. Then just add that control to the Web page as you would in any standard Visual Basic project.
For example, we add a standard HTML button and a progress bar ActiveX control to the Web page, Here’s the HTML code that the Page Designer adds to our Web page when we add those two new controls, the progress bar and the HTML button:
<P>Here’s an ActiveX control:</P>
<OBJECT classid=CLSID:35053A22-8589-11D1-B16A-00C0F0283628 height=24
id=ProgressBar1
style="HEIGHT: 24px; LEFT: 127px; POSITION: absolute; TOP: 248px; WIDTH:100px; Z-INDEX: 101"width=100>
<PARAM NAME="_ExtentX" VALUE=2646>
<PARAM NAME="_ExtentY" VALUE="635">
<PARAM NAME="_Version" VALUE="393216">
<PARAM NAME="BorderStyle" VALUE="0">
<PARAM NAME="Appearance" VALUE="1">
<PARAM NAME="MousePointer" VALUE="0">
<PARAM NAME="Enabled" VALUE="1">
<PARAM NAME="OLEDropMode" VALUE="0">
<PARAM NAME="Min" VALUE="0">
<PARAM NAME="Max" VALUE="100">
<PARAM NAME="Orientation" VALUE="0">
<PARAM NAME="Scrolling" VALUE="0">
</OBJECT>
<INPUT id=Button1 name=Button1 style="LEFT: 26px; POSITION: absolute; TOP:
248px; Z-INDEX: 102" type=button value="Click Me!">
Now we’re free to use the HTML button to set the progress bar’s value like this, just as you would in a standard Visual Basic project:
Private Function Button1_onclick() As Boolean
ProgressBar1.Value = 20
End Function

Adding HTML Controls To DHTML Pages In Visual Basic

Adding HTML Controls To DHTML Pages
Using the Visual Basic DHTML Page Designer, you can add the standard HTML controls to a Web page: buttons, Submit buttons, Reset buttons, text fields, text areas, password fields, option buttons, checkboxes, select controls, file upload controls, hidden fields, and lists. As you can see, the whole HTML control set is here, and you can use these controls with Visual Basic just as you would in a standard form if you create the DLL file for your DHTML page (see “Creating DHTML Pages” earlier in this chapter), or with a scripting
language such as VBScript or JavaScript.
Adding these controls to your Web page is just like adding them to a standard Visual Basic project. You just use the control’s tool in the Page Designer’s toolbox in the same way you’d use a tool in the Visual Basic toolbox.
The code that the Page Designer adds to our Web page for the Submit button looks like this:
<HTML>
<HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content='"MSHTML 4.72.3007.2"' name=GENERATOR>
</HEAD>
<BODY>
<P>Here’s some text!</P>
<P>Here’s an image:</P>
<P>&nbsp;</P>
<P><IMG id=Image1 name=Image1 src="c:\vbbb\dhtml\image1.bmp" style="LEFT: 40px; POSITION: absolute; TOP: 107px; Z-INDEX: 100">
</P>
<P>&nbsp;</P>
<P>Here’s a Submit button:
<INPUT id=SubmitButton1 name=SubmitButton1 style="LEFT: 17px;
POSITION: absolute; TOP: 170px; Z-INDEX: 103" type=submitvalue=SubmitButton1>
</P>

To add code to the Submit button, you double-click it in the Page Designer just as you would when creating a standard Visual Basic. Doing so adds an event handler procedure to the page’s code:
Private Function SubmitButton1_onclick() As Boolean
End Function

For example, here’s how we display a message box when the user clicks the Submit button:
Private Function SubmitButton1_onclick() As Boolean
MsgBox "You clicked the Submit button!"
End Function

How to Create DHTML Pages In Visual Basic

Creating DHTML Pages
The Testing Department is on the phone. You may have heard of the company’s Web site crash they need to redesign the company Web page. From scratch. Can you do it? You start up Visual Basic sure, you say, no problem. You can use Visual Basic to design dynamic HTML pages. To do that, just select the Dynamic HTML Application item in the Visual Basic New Project dialog box. This opens the DHTML Page Designer.
In the following topics, we’ll see how to use the DHTML Page Designer to implement DHTML pages. In general, you add the elements you want in your page to the right window in the Page Designer, and it gives you an idea of how the page will look in the browser. The window on the left in the Page Designer shows the logical structure of the page by indicating which HTML elements are contained in other HTML elements. Using the Page Designer, then, you can get an idea of both how your page will look and how it’s organized in HTML.
Note that you can use Visual Basic in the DHTML pages designed with Visual Basic. How is this possible? It’s possible because what you’re really creating is an ActiveX DLL project that will be loaded into the Internet Explorer when you open the Web page. This DLL runs in the Internet Explorer’s process (and you have to place the DLL file for the project on your Web site so it can be downloaded). 
To make the needed DLL file, just select the Make ProjectName.dll item in the File menu. You might just want to create an HTML Web page, without any DLL files at all, and you can do that too. Just don’t add any code to the page; stick to standard HTML elements. Usually, the HTML page is stored in the Visual Basic project. To store it in a separate HTM file, click the DHTML Page Designer Properties icon at upper left in the DHTML Page Designer, opening the Properties dialog box.
Select the Save HTML in an external file option, and give an HTM file name to save your Web page as. To test the Web page, select the DHTMLProject properties item in the Project menu, clicking the Debugging tab in the Properties pages that open. Make sure the Start Component option button is clicked and the start component is set to DHTMLPage1, then click on OK. Now select the Start item in the Run menu to open the Web page. Now that we’ve started designing our DHTML Web page, we’ll add text, images, tables, and other elements including ActiveX controls to the page in the next few topics.
<HTML>
<HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content='"MSHTML 4.72.3007.2"' name=GENERATOR>
</HEAD>
<BODY>
<P>Here’s some text!</P>
<P>Here’s an image:</P>
<P>&nbsp;</P>
<P>Here’s <INPUT id=SubmitButton1 name=SubmitButton1 style="LEFT: 17px;POSITION: absolute; TOP: 170px; Z-INDEX: 103" type=submit
value=SubmitButton1>a Submit button:
</P>
<P>&nbsp;</P>
<P><IMG id=Image1 name=Image1 src="c:\vbbb\dhtml\image1.bmp"style="LEFT: 40px; POSITION: absolute; TOP: 107px; Z-INDEX: 100">
</P>
<P>
<OBJECT classid=CLSID:35053A22-8589-11D1-B16A-00C0F0283628 height=24,id=ProgressBar1
style="HEIGHT: 24px; LEFT: 127px; POSITION: absolute; TOP: 248px; WIDTH:100px; Z-INDEX: 101"width=100>
<PARAM NAME="_ExtentX" VALUE="2646">
<PARAM NAME="_ExtentY" VALUE="635">
<PARAM NAME="_Version" VALUE="393216">
<PARAM NAME="BorderStyle" VALUE="0">
<PARAM NAME="Appearance" VALUE="1">
<PARAM NAME="MousePointer" VALUE="0">
<PARAM NAME="Enabled" VALUE="1">
<PARAM NAME="OLEDropMode" VALUE="0">
<PARAM NAME="Min" VALUE="0">
<PARAM NAME="Max VALUE="100">
<PARAM NAME="Orientation" VALUE="0">
<PARAM NAME="Scrolling" VALUE="0">
</OBJECT>
<INPUT id=Button1 name=Button1 style="LEFT: 26px; POSITION: absolute; TOP:
248px; Z-INDEX: 102" type=button value="Click Me!">
</P>
<P>Here’s an ActiveX control:</P>
<P align=center>&nbsp;</P>
<P>Here’s a table:</P>
<P>
<TABLE border=1 id=Table1 name = Table1>
<TR>
<TD>This
<TD>is
<TD>a
<TR>
<TD>3x3
<TD>HTML
<TD>table
<TR>
<TD>ready
<TD>to
<TD>use.</TD></TR></TABLE></P>
<P>Here’s a hyperlink:
<A href="http://www.microsoft.com"
id=Hyperlink11 name=Hyperlink1>Microsoft
</A>
</P>
</BODY>
</HTML>

Adding Text To DHTML Pages
Adding text to a DHTML page is easy: just click the right window in the DHTML Page Designer (which represents the way your page will look when it runs). Just use the mouse to place the blinking insertion point where you want the text to appear, and type the text you want there. For example, we’ve added the text “Here’s some text!” in the Web page in the Page Designer.
Adding the text we’ve placed in our Web page adds this HTML to the Web page itself note the Page Designer uses the <P> paragraph HTML tag for each paragraph of text:
<HTML>
<HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content='"MSHTML 4.72.3007.2"' name=GENERATOR>
</HEAD>
<BODY>
<P>Here’s some text!</P>
You can format the text by selecting the text font, size, and style (bold, italic, or underlined) with the controls at the top of the Page Designer. Besides being able to format the text, you can also specify its alignment right, center, or left with the buttons in the Page Designer’s toolbar.using buttons in the DHTML Page Designer toolbar. These HTML elements are especially important in DHTML because you can specify dynamic HTML styles and properties that apply specifically to <SPAN> or <DIV>.

Adding Images To DHTML Pages
The Aesthetic Design Department is calling again. Your new Web page is fine, but what about adding images?Can you do that? you ask. Sure, they say, that’s half of what the Web is all about. To add an image to a DHTML page in the Visual Basic DHTML Page Designer, you click the Image tool, which is the sixth tool down on the left in the Page Designer toolbox. Doing so adds an empty image to the page; move that image to the position you want and size it appropriately.To add an image to this DHTML control, set its src property (the name of this and other DHTML control properties are intended to match the corresponding HTML tag attributes; this property matches the <IMG>
tag’s src attribute). In this case, we set the src property to an image on disk: file:///C:/vbbb/dhtml/image1.bmp, although of course you can use a URL here.
Here’s how the image is added to the HTML of our Web page—note that the Page Designer sets the <IMG>tag’s position attribute to absolute, which is how it can let you position the image anywhere you want it in the Web page:
<HTML>
<HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content=‘"MSHTML 4.72.3007.2"’ name=GENERATOR>
</HEAD>
<BODY>
<P>Here’s some text!</P>
<P>Here’s an image:</P>
<P>&nbsp;</P>
<P><IMG id=Image1 name=Image1
src="c:\vbbb\dhtml\image1.bmp"
style="LEFT: 40px; POSITION: absolute; TOP: 107px; Z-INDEX: 100">
</P>
Because we’re using dynamic HTML, the image element is an active element: you can click it, for example, and add code to react to that click like this, where we display a message box indicating that the user clicked the image:
Private Function Image1_onclick() As Boolean
MsgBox "You clicked the image!"
End Function

28 June, 2013

How To Create Web Browser In Visual Basic

Web Browser In Visual Basic.
If you have the Microsoft Internet Explorer installed, you can build Web browsers using Visual Basic.Microsoft has packaged the Internet Explorer in a control, the WebBrowser control, and we’ll be able to use that control to create a Web browser in this chapter that supports such browser functionality as Back, Forward, Home, Stop, and Refresh buttons. We’ll also let the user specify what URL to navigate to with a combo box as well as keeping track of recently visited URLs in that combo box. Building a Web browser can be a worthwhile project in itself, but another popular use of the WebBrowser control is to add a Web browser to your existing program for added power. In fact, you can use the Web Browser control to open ActiveX documents in a way that makes them look like a seamless part of the program even though that document may have come from the Internet.

Creating A Web Browser
The Testing Department is calling again. They need a new Web browser program right away. What’s wrong with the old one? you ask. They say, it doesn’t display the founder’s picture. Oh, you say. It’s easy to build a Web browser in Visual Basic—you just use the Microsoft WebBrowser control. In
this and the next few topics, we’ll put together the functioning Web browser. Our browser will support Back, Next, Home, Stop, and Refresh buttons,
.In addition, the browser will have all the power of the Microsoft Internet Explorer (largely because it is the Internet Explorer; we use the WebBrowser control, which is the Internet Explorer in a control). To let the user navigate, we’ll include a combo box. When the user types a new URL in the combo box and presses the Enter key, we’ll navigate to that URL (and keep a record of the URLs we’ve been to in the combo box’s drop-down list).
To create our Web browser, follow these steps:
1. Create a new standard Visual Basic project.
2. Select the Project|Components item.
3. Click the Controls tab in the Components dialog box.
4. Select the Microsoft Internet Controls and Microsoft Windows Common Controls entries, and click on OK to close the Components dialog box.
5. Add a WebBrowser control and a toolbar to the form, stretching the WebBrowser control, WebBrowser1, to fill the space under the toolbar.
6. Add five buttons to the toolbar (right-click the toolbar, select the Properties item, click the Buttons tab, and use the Insert Button button to add the buttons).
7. Give the buttons the same captions and Key properties: Back, Next, Home, Stop, and Refresh (for example, the button with the caption “Back” will also have its Key property set to Back so we can identify which button in the toolbar was clicked).
8. Add a combo box, combo1, to the end of the toolbar (draw the combo box in the toolbar to make sure it’s part of the toolbar; don’t double-click to create a combo box and then move it to the toolbar). That sets up the Web browser but how do we work with it in code? We’ll take a look at that in the next few topics.

Specifying URLs In A Web Browser
Now that you’ve set up the controls we’ll need in a Web browser (see the previous topic), how do you let the user navigate?
You use the WebBrowser control’s Navigate method. Let’s see this at work. For example, when our Web browser first loads, we can navigate to the Microsoft Web page this way (note that you can specify URLs with or without the “http://” part in the Internet Explorer, and although we omit it here,
you can include that prefix if you prefer):
Private Sub Form_Load()
WebBrowser1.Navigate "www.microsoft.com"
...
End Sub
We also want the user to be able to navigate to a new URL, and that’s usually done with a combo box like the one we added to our Web browser in the previous topic, combo1. We start working with combo1 by displaying the present URL and adding it to the combo box’s drop-down list:
Private Sub Form_Load()
WebBrowser1.Navigate "www.microsoft.com"
Combo1.Text = "www.microsoft.com"
Combo1.AddItem Combo1.Text
End Sub
Users can select past URLs from the combo box’s drop-down list. When they do select a URL that way, a Click event is generated, and we can navigate to the newly selected URL this way: 
Private Sub Combo1_Click()
WebBrowser1.Navigate Combo1.Text
End Sub
In addition, users can type a new URL into the combo box and press Enter, just as they can in commercial browsers. When they press Enter, we can navigate to the new URL simply by calling the Combo1_Click event handler directly from the KeyPress event handler:
Private Sub Combo1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Combo1_Click
End If
End Sub
Finally, when the downloading process is complete, the WebBrowser control fires a DownloadComplete event, and we can display the present URL in the browser’s title bar, just as any commercial browser might. To do that, we get the browser’s present URL from its LocationName property:
Private Sub WebBrowser1_DownloadComplete()
Me.Caption = WebBrowser1.LocationName
...
End Sub
In addition, we can add that URL to the top of the combo box’s list this way:
Private Sub WebBrowser1_DownloadComplete()
Me.Caption = WebBrowser1.LocationName
Combo1.AddItem WebBrowser1.LocationURL, 0
End Sub
And that’s it—now the user can navigate around using the combo box. However, we have yet to make all the buttons, such as Back, Forward, and Home, active, and we’ll do that in the next two topics. The code for the browser, browser.frm version 1 (version 2, which is included on the accompanying CD-ROM, will include support for the browser buttons).
VERSION 6.00
Object = "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}#1.1#0"; "SHDOCVW.DLL"
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 7560
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 7560
StartUpPosition = 3 'Windows Default
Begin ComctlLib.Toolbar Toolbar1
Align = 1 'Align Top
Height = 630
Left = 0
TabIndex = 1
Top = 0
Width = 7560
_ExtentX = 13335
_ExtentY = 1111
ButtonWidth = 1164
ButtonHeight = 953
Appearance = 1
_Version = 393216
BeginProperty Buttons {66833FE8-8583-11D1-B16A-00C0F0283628}
NumButtons = 6
BeginProperty Button1 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Back"
Key = "Back"
EndProperty
BeginProperty Button2 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Next"
Key = "Next"
EndProperty
BeginProperty Button3 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Home"
Key = "Home"
EndProperty
BeginProperty Button4 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Stop"
Key = "Stop"
EndProperty
BeginProperty Button5 {66833FEA-8583-11D1-B16A-00C0F0283628}
Caption = "Refresh"
Key = "Refresh"
EndProperty
BeginProperty Button6 {66833FEA-8583-11D1-B16A-00C0F0283628}
Style = 4
Object.Width = 100
EndProperty
EndProperty
Begin VB.ComboBox Combo1
Height = 315
Left = 3480
TabIndex = 2
Top = 120
Width = 3975
End
End
Begin SHDocVwCtl.WebBrowser WebBrowser1
Height = 2295
Left = 120
TabIndex = 0
Top = 840
Width = 7335
ExtentX = 12938
ExtentY = 4048
ViewMode = 1
Offline = 0
Silent = 0
RegisterAsBrowser= 0
RegisterAsDropTarget= 1
AutoArrange = -1 'True
NoClientEdge = 0 'False
AlignLeft = 0 'False
ViewID = "{0057D0E0-3573-11CF-AE69-08002B2E1262}"
Location = ""
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Load()
WebBrowser1.Navigate "www.microsoft.com"
Combo1.Text = "www.microsoft.com"
Combo1.AddItem Combo1.Text
End Sub
Private Sub WebBrowser1_DownloadComplete()
Me.Caption = WebBrowser1.LocationName
Combo1.AddItem WebBrowser1.LocationURL, 0
End Sub
Private Sub Combo1_Click()
WebBrowser1.Navigate Combo1.Text
End Sub
Private Sub Combo1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Combo1_Click
End If
End Sub

Adding Back And Forward Buttons To A Web Browser
Now that we’ve set up a Web browser in which the user can navigate by typing URLs into the combo box (see the previous topic), we’ll enable the Back and Forward buttons in the browser. That’s easier than you might expect—you just use the browser’s GoBack or GoForward methods. We do that like this, where we determine which button in the toolbar has been clicked by checking the keys we’ve added to those buttons:
Private Sub Toolbar1_ButtonClick(ByVal Button As Button)
Select Case Button.Key
Case "Back"
WebBrowser1.GoBack
Case "Forward"
WebBrowser1.GoForward
End Select
End Sub
And that’s all there is to it—now the user can navigate forwards and backwards in the browser’s history. We’ve added Back and Forward buttons now, but the user also expects Refresh, Home, and Stop buttons in Web browsers, and we’ll add those buttons next. Adding Refresh, Home, And Stop Buttons To A Web Browser In the previous few topics, we’ve set up a Web browser complete with combo box to let the user enter and select URLs, as well as a Back and Forward button to let the user navigate through the browser’s history. However, we still have a few more buttons to implement: the Refresh, Home, and Stop buttons. We can implement those buttons with the Web browser control’s Refresh, GoHome, and Stop methods. We’ve given the Refresh, Home, and Stop buttons the keys “Refresh”, “Home”, and “Stop”, so we just call the appropriate Web browser method when the matching button is clicked (note that if the user clicks the Stop button, we also update the current URL as displayed in the browser’s title bar using the Web browser’s LocationName property):
Private Sub Toolbar1_ButtonClick(ByVal Button As Button)
Select Case Button.Key
Case "Back"
WebBrowser1.GoBack
Case "Forward"
WebBrowser1.GoForward
Case "Refresh"
WebBrowser1.Refresh
Case "Home"
WebBrowser1.GoHome
Case "Stop"
WebBrowser1.Stop
Me.Caption = WebBrowser1.LocationName
End Select
End Sub

How To Create An ActiveX Document In Visual Basic

Creating An ActiveX Document
The Testing Department is calling again. The ActiveX controls you’ve been building are fine, but what about going on to the next step? How about creating an ActiveX document? You ask, why are ActiveX documents the next step? Because, they say, although ActiveX controls can appear in Web pages, ActiveX documents can be Web pages. 
That is, an entire Visual Basic form with all its controls can now appear in your Web browser or other application. Let’s see an example. To create a new ActiveX document, open the Visual Basic New Project dialog box, select the ActiveX document EXE entry, and click on OK (you can create either ActiveX document EXEs or DLLs see the next topic for a discussion of the difference). This creates a new ActiveX document in Visual Basic. To get our start with ActiveX documents, just double-click the document and add this code to the Initialize procedure that opens to draw a set of crisscrossing lines and a black box in the center of the document:
Private Sub UserDocument_Initialize()
Line (0, 0)-(ScaleWidth, ScaleHeight)
Line (0, ScaleHeight)-(ScaleWidth, 0)
Line (ScaleWidth / 4, ScaleHeight / 4)-(3 * ScaleWidth / 4, _
3 * ScaleHeight / 4), , BF
End Sub

This is the procedure that’s run when the ActiveX document is first opened. Because we’re drawing from the Initialize event handler, set the document’s AutoRedraw property to True (just as you would for a form). Let’s see our new ActiveX document at work in a Web browser (you can use ActiveX documents in other applications that support them, but Web browsers are particularly handy for testing ActiveX documents in an interactive way). Select the Project1 Properties item in the Project menu, and click the Debugging tab in the Project Properties dialog box that opens. Make sure that the option button labeled Start Component is selected (the start component should be given as UserDocument1) and the box labeled Use Existing Browser is checked. Then close the dialog box by clicking on OK, and run the document with the Start item in the Run menu. Starting the document loads the document’s specification, a VBD file, into the browser, which (if it supports ActiveX documents) runs the document’s EXE file or adds the document’s DLL file to its own process. That’s all there is to it. Now we’ve created our first ActiveX document. Still, not much is going on here yet. In the following topics, we’ll develop our ActiveX documents further.

ActiveX Document DLLs Vs. EXEs
You can create both ActiveX document EXEs and DLLs. Here’s the difference: if an ActiveX document is written as an executable file (EXE file), it is an out-of-process server and runs in its own process; if it has been implemented as a dynamic link library (DLL file), it is an in-process server and runs in the same process as the client application. Although ActiveX documents are usually built as EXE projects, the benefit of DLLs is that applications that use in-process servers usually run faster than those that use out-of-process servers because the application doesn’t have to cross process boundaries to use an object’s properties, methods, and events. In addition, the performance of an in-process component, or DLL file, surpasses that of the same component compiled as an EXE. Also, multiple programs accessing the same EXE can overwrite global data, but that doesn’t happen if they each have their own in-process server.

Adding Controls To An ActiveX Document (A Tic-Tac-Toe Example)
The Testing Department is on the phone. The ActiveX document you’ve created is very nice, but why can’t you do anything with it? Well, you say, I was just about to add controls to it and create a Web browser game. Using ActiveX documents, you can display entire forms in Web browsers. To see this at work, we’ll create a mini tic-tac-toe game. This game will let users click buttons to display alternate x’s and o’s (although it won’t include the logic to actually play tic-tac-toe). Working with multiple controls in this way will demonstrate how to display entire programs as Web pages. Create a new ActiveX document (EXE or DLL), and add nine command buttons to it arranged in a 3×3 grid in classic tic-tac-toe fashion. Give each button the same name, Command, clear each button’s caption (that is, select the text in the caption and press the backspace key), and when Visual Basic asks if you want to create a control array, click Yes, because a control array will make the code shorter and easier to handle.
To alternate x’s and o’s as the user clicks buttons, we’ll need a Boolean flag, which we’ll call blnXFlag. If this flag is true, the next caption to set will be “x”; otherwise, “o”. Add the declaration of blnXFlag to the (General) section:

Dim blnXFlag As Boolean
We also initalize blnXFlag to True when the document first loads by adding this code to the Initialize event handler:
Private Sub UserDocument_Initialize()
blnXFlag = True
End Sub
Now when the user clicks a button, we alternate between setting the clicked buttons’ captions to “x” and “o” this way:
Private Sub Command_Click(Index As Integer)
If blnXFlag Then
Command(Index).Caption = "x"
Else
Command(Index).Caption = "o"
End If
...
End Sub

At the end of the code, we toggle the state of blnXFlag for the next time the user clicks a button:
Private Sub Command_Click(Index As Integer)
If blnXFlag Then
Command(Index).Caption = "x"
Else
command(Index).Caption = "o"
End If
blnXFlag = Not blnXFlag
End Sub

Let’s see our new ActiveX document game at work. Select the Project1 Properties item in the Project menu, and click the Debugging tab in the Project Properties dialog box that opens. Make sure that the option button labeled Start Component is selected (the start component should be given as UserDocument1), and the box labeled Use Existing Browser is checked. Then close the dialog box by clicking on OK, and run the game with the Start item in the Run menu. Our entire game appears in the Microsoft Internet Explorer. The user can alternate button captions just by clicking the buttons. Our ActiveX document example is a success. We’ll save the document as activextictactoe.dob (ActiveX documents have the extension .dob when
saved in Visual Basic, just as form files have the extension .frm), and the project as activextictactoe.vbp. The default name for the document is UserDocument1, as you can see in the Visual Basic Properties window; if you want to use a different name for the document, set it in the Properties window. We’ll change the document name to activextictactoedoc. When we create the VBD specification file for this document, then, that file will be activextictactoedoc.vbd, and that’s the file to open in your Web browser The code for this example, activextictactoedoc.dob version 1 (version 2 will support persistent data).
VERSION 6.00
Begin VB.UserDocument activextictactoedoc
AutoRedraw = -1 'True
ClientHeight = 2865
ClientLeft = 0
ClientTop = 0
ClientWidth = 4800
HScrollSmallChange= 225
ScaleHeight = 2865
ScaleWidth = 4800
VScrollSmallChange= 225
Begin VB.CommandButton Command
Height = 495
Index = 8
Left = 3360
TabIndex = 8
Top = 1920
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 7
Left = 3360
TabIndex = 7
Top = 1080
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 6
Left = 3360
TabIndex = 6
Top = 360
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 5
Left = 1800
TabIndex = 5
Top = 1920
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 4
Left = 1800
TabIndex = 4
Top = 1080
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 3
Left = 1800
TabIndex = 3
Top = 360
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 2
Left = 240
TabIndex = 2
Top = 1920
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 1
Left = 240
TabIndex = 1
Top = 1080
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 0
Left = 240
TabIndex = 0
Top = 360
Width = 1215
End
End
Attribute VB_Name = "activextictactoedoc"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit

Dim blnXFlag As Boolean
Private Sub Command_Click(Index As Integer) 
If blnXFlag Then
Command(Index).Caption = "x"
Else
Command(Index).Caption = "o"
End If
blnXFlag = Not blnXFlag
End Sub
Private Sub UserDocument_Initialize()
blnXFlag = True
End Sub

Creating ActiveX Controls in Vb

ActiveX Controls
We have seen ActiveX controls throughout the book those are the controls you can add to the Visual Basic toolbox using the Components dialog box. You can add those controls to a Visual Basic program like any other control. You can also use ActiveX controls on the Internet, embedding them in your Web pages, as we’ll see when we work on creating ActiveX controls.
 ActiveX controls can support properties, methods, and events. Your ActiveX control can be built entirely from scratch (in other words, you’re responsible for its appearance), it can be built on another control (such as a list box), or it can contain multiple existing controls (these ActiveX controls are said to contain constituent controls).
 Visual Basic ActiveX controls are based on the Visual Basic UserControl object. When you create an ActiveX control, you create a control class file with the extension .ctl. Visual Basic uses that file to create the actual control, which has the extension .ocx. After you register that control with Windows (you can use Windows utilities like regsvr32.exe to register a control, as we’ll see in this chapter), the control will appear in the Visual Basic Components dialog box, ready for you to add to a program. You can also use these controls in Web pages.

Code Components
Code components were formerly called OLE automation servers. These objects let you use their code in other programs.
 For example, you might have a calculation routine that you expose in a code component; doing so makes that routine available to other programs. Code components can support properties and methods. If you take a look at the kind of ActiveX components you can build with Visual Basic in the NewProject window, you’ll see all kinds:
• ActiveX document DLL, ActiveX EXE
• ActiveX control
• ActiveX EXE, ActiveX DLL (these are code components)

There’s still quite a confusion of terms here what’s the difference between a DLL and EXE ActiveX component? Let’s explore further.

In-Process Vs. Out-Of-Process Components
If an ActiveX component has been implemented as part of an executable file (EXE file), it is an out-of-process server and runs in its own process. If it has been implemented as a dynamic link library (DLL file), it is an in-process server and runs in the same process as the client application. If your ActiveX component is an out-of-process server, it is an EXE file, and can run standalone.
Applications that use in-process servers usually run faster than those that use out-of-process servers because the application doesn’t have to cross process boundaries to use an object’s properties, methods, and events.
There are a few reasons why you may want to create your ActiveX document as an in-process component (DLL file). The performance of an in-process component surpasses that of the same component compiled as an EXE. In addition, multiple programs accessing the same EXE can overwrite global data; that doesn’t happen if they each have their own in-process server. Which ActiveX Component Do I Want To Build?
With all the different types of ActiveX components to choose from, how do you decide which type of component you want to create? Take a look at this list:
• To build an invisible component that provides routines in code that you can call, build a code component (ActiveX EXE or an ActiveX DLL).
• To build a component that can run in the same process with your application, build an ActiveX DLL.
• To build a component that can serve multiple applications and can run on a remote computer,build an ActiveX EXE.
• To build a visible component that can be dropped into an application at design time, build an ActiveX control.
• To build a visible component that can take over an application window at runtime, build an ActiveX document.

That’s it for the overview of ActiveX controls and documents for the moment it’s time to turn to the Immediate Solutions. The Testing Department is calling again. Wouldn’t it be great if you built a new ActiveX control that displayed a digital clock? That control could be reused in many other programs. Hmm, you think, how do you create an ActiveX control?

Creating An ActiveX Control
Select the New Project menu item in the Visual Basic File menu to open the New Project dialog box. Select the ActiveX Control item in the New Project dialog box and click on OK. This creates a new empty ActiveX control, Believe it or not, you’ve created your first ActiveX control, UserControl1. You can even run the control with the Run menu’s Start item, which would display the control by launching the Microsoft Internet Explorer if you have it installed in your computer; however, there would be nothing to see because the control is empty.
The default name of the control is Project1, but we can change that to, say, FirstControl. To do that, select the Project1 Properties item in the Project menu, and type “FirstControl” into the Project Name box in the Project Properties dialog box, then click on OK. Also, save the project as firstcontrol.vbp. Instead of a FRM file, you save ActiveX controls in CTL files. Select the Save UserControl1 item in the file menu to save the control as firstcontrol.ctl. Here’s what appears in that file on disk:
VERSION 6.00
Begin VB.UserControl UserControl1
ClientHeight = 3600
ClientLeft = 0
ClientTop = 0
ClientWidth = 4800
ScaleHeight = 3600
ScaleWidth = 4800
End
Attribute VB_Name = "UserControl1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True


Designing An ActiveX Control From Scratch
The testing department is on the phone. Your new ActiveX control looks fine, but why doesn’t it do anything? Should it? you ask. Yes, they say. You can design the appearance of your ActiveX control entirely from scratch, creating an entirely new control, never seen before.
In that case, you’re responsible for creating the control’s appearance from scratch. Later, you can add events to your control, as well as methods and properties, To design the appearance of your entirely new control, you can use the Visual Basic graphics methods that the UserControl object supports, such as Circle, Line, PSet, Print, Cls, and Point. You can also display an image in the UserControl object by setting its Picture property.
Let’s see an example. Here, we’ll just draw two lines to crisscross an ActiveX control and draw a black box in the middle. Create a new ActiveX control now, and double-click it at design time to open the code window to the UserControl_Initialize function:

Private Sub UserControl_Initialize()
End Sub

This function is just like the Form Load procedure that we’re familiar with. Set the control’s AutoRedraw property to True so we can draw graphics from UserControl Initialize, and then draw the lines to crisscross the control, using the Line method and ScaleWidth and ScaleHeight just as you would in a Visual Basic form:
Private Sub UserControl_Initialize()
Line (0, 0)-(ScaleWidth, ScaleHeight)
Line (0, ScaleHeight)-(ScaleWidth, 0)
...
End Sub

Next, we draw a filled-in black box in the center of the control this way:
Private Sub UserControl_Initialize()
Line (0, 0)-(ScaleWidth, ScaleHeight)
Line (0, ScaleHeight)-(ScaleWidth, 0)
Line (ScaleWidth / 4, ScaleHeight / 4)-(3 * ScaleWidth / 4, _
3 * ScaleHeight / 4), , BF
End Sub

Let’s test this new ActiveX control now in the Microsoft Internet Explorer (assuming you have that browser installed). To do that, just select the Run menu’s Start item now. Doing so opens the Project Properties dialog box, Leave UserControl1 in the Start Component box, and make sure the Use Existing Browser box is clicked, then click on OK. This registers our control with Windows, creates a temporary HTML page with the control embedded in it, and starts the Internet Explorer, You can see our new ActiveX control in Figure 20.4. Now we’ve created our first ActiveX control and designed its appearance from scratch. If we wanted to, we could add events, properties, and methods to this control .Here’s the temporary HTML page that Visual Basic creates to display our ActiveX control; note that our control is registered with Windows and has its own ID, so this page can use the HTML
<OBJECT> tag to embed one of our controls in the page:
<HTML>
<BODY>
<OBJECT classid="clsid:B2A69D3B-D38C-11D1-8881-E45E08C10000">
</OBJECT>
</BODY>
</HTML>

Print Dialog Box in Visual Basic

Using The Print Dialog Box
The Testing Department is calling again. The Print button you’ve placed in your word processor, Super Duper Text Pro, is very nice, but it doesn’t let the user set the number of copies of a document they want to print. You can’t do that with a button, you explain. Right, they say use a Print dialog box.You show the Print dialog box with the Common Dialog control’s ShowPrinter method. If you know your document’s length, you can set the minimum and maximum pages to print in the Common Dialog control’s Min and Max properties; setting these properties enables the From and To page range text boxes in the Print dialog box 
This dialog box does not send data to the printer; instead, it lets the user specify how he wants data printed. Printing is up to you. How do you print? If you’ve set the PrinterDefault property to True, you can use the
Printer object to print data (the user can change the default printer from the Printer dialog box, setting a new default printer in the Windows registry or win.ini, but that new printer automatically becomes the one referred to by the Printer object). 
For example, you can print the picture in a picture box using the Printer object this way: Printer.PaintPicture Picture1.Picture, 0, 0. Otherwise, you must use Windows functions to print to the device represented by the hDC (a device context handle) property. After the user clicks on OK, you can read these properties from the Common Dialog control to determine what printer options they've selected:
" Copies The number of copies to print
" FromPage The page to start printing
" ToPage The page to stop printing
" hDC The device context for the selected printer

Let’s see an example. In this case, we’ll use the Visual Basic PrintForm method to print a copy of the current form as many times as the user specifies. We start by setting the Common Dialog control’s CancelError property to True so we can catch Cancel button clicks as trappable errors:

Private Sub Command1_Click()
On Error GoTo Cancel
...
Cancel:
End Sub

Then we set the PrinterDefault property to True and show the Print dialog box:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.PrinterDefault = True
CommonDialog1.ShowPrinter
...
Cancel:
End Sub

All that's left is to loop over the number of copies the user has requested (as returned in the Copies property) and call PrintForm each time: 

Private Sub Command1_Click()
Dim intLoopIndex As Integer
On Error GoTo Cancel
CommonDialog1.PrinterDefault = True
CommonDialog1.ShowPrinter
For intLoopIndex = 1 To CommonDialog1.Copies
PrintForm
Next intLoopIndex
Cancel:
End Sub

That's it when the user clicks Command1, the program displays the Print dialog box; the user can set the number of copies to print and when they click on OK, Visual Basic displays a dialog box with the text Printing & momentarily, and the print job starts. Our Print dialog box example is a success the code for this program is located in the printerdialog folder on this books accompanying CD-ROM.

Setting Print Dialog Flags
You can set a number of options in the Common Dialog control's Flags property when working with the Print dialog box:
" cdlPDAllPages &H0; returns or sets the state of the All Pages option button.
" cdlPDCollate &H10; returns or sets the state of the Collate checkbox.
" cdlPDDisablePrintToFile &H80000; disables the Print To File checkbox.
" cdlPDHelpButton &H800; causes the dialog box to display the Help button.
" cdlPDHidePrintToFile &H100000; hides the Print To File checkbox.
" cdlPDNoPageNums &H8; disables the Pages option button and the associated edit control.
" cdlPDNoSelection &H4; disables the Selection option button.
" cdlPDNoWarning &H80; prevents a warning message from being displayed when there is no default printer.
" cdlPDPageNums &H2; returns or sets the state of the Pages option button.
" cdlPDPrintSetup &H40; causes the system to display the Print Setup dialog box rather than the Print dialog box.
" cdlPDPrintToFile &H20; returns or sets the state of the Print To File checkbox.
" cdlPDReturnDC &H100; returns a device context for the printer selection made in the dialog box. The device context is returned in the dialog box's hDC property.
" cdlPDReturnDefault &H400; returns the default printer name.
" cdlPDReturnIC &H200; returns an information context for the printer selection made in the dialog box. An information context provides a fast way to get information about the device without creating a device context. The information context is returned in the dialog box's hDC property.
" cdlPDSelection &H1; returns or sets the state of the Selection option button. If neither cdlPDPageNums nor cdlPDSelection is specified, the All option button is in the selected state.
" cdlPDUseDevModeCopies_&H40000; if a printer driver doesn't support multiple copies, setting this flag disables the Number Of Copies control in the Print dialog box. If a driver does support multiple copies, setting this flag indicates that the dialog box stores the requested number of copies in the Copies property.
You can set more than one flag for a dialog box using the Or operator. For example:
CommonDialog1.Flags = &H10& Or &H200& :Adding the desired constant values produces the same result.


Setting The Minimum And Maximum Pages To Print
When displaying a Print dialog box, you can set the minimum and maximum allowed values for the print range (in other words, the From and To pages to print) using the Min and Max properties of the Common Dialog control. The Min property sets the smallest number the user can specify in the From text box. The Max property sets the largest number the user can specify in the To text box. For example, here we restrict
the possible pages to print to a maximum of 10, in the range 0 to 9:

Private Sub Command1_Click()

Dim intLoopIndex As Integer
On Error GoTo Cancel
CommonDialog1.PrinterDefault = True
CommonDialog1.Min = 0
CommonDialog1.Max = 9
CommonDialog1.ShowPrinter
For intLoopIndex = 1 To CommonDialog1.Copies
PrintForm
Next intLoopIndex
Cancel:
End Sub

Now when the Print dialog box appears, you can see that in the Print Range box, one option button says All 10 Pages. That is, we've set a maximum total of 10 pages for our document. The actual page range is from 0 to 9.
Setting Page Orientation
When printing, you can set the page orientation portrait (upright) or landscape (sideways)_with the Common Dialog control's Orientation property. This setting is communicated to the printer automatically, but note that not all printers will be able to set a document's orientation.
Here are the possible values for the Orientation property:
" cdlPortrait 1; documents are printed with the top at the narrow side of the paper (the default).
" cdlLandScape 2; documents are printed with the top at the wide side of the paper.

Here's an example. In this case, we're setting the printer's Orientation property to landscape:

Private Sub Command1_Click()
Dim intLoopIndex As Integer
On Error GoTo Cancel
CommonDialog1.PrinterDefault = True
CommonDialog1.Orientation = cdlLandscape
CommonDialog1.ShowPrinter
For intLoopIndex = 1 To CommonDialog1.Copies
PrintForm
Next intLoopIndex
Cancel:
End Sub

27 June, 2013

How To Show Windows Help From A Visual Basic Program

Showing Windows Help From A Visual Basic Program
You can display a Windows Help file (.hlp) with the Common Dialog control's ShowHelp method. To use this method, you first set the Common Dialog control's HelpCommand property to one of the following settings, and the HelpFile property to the actual name of the Help file to open. Here are the possible settings for HelpCommand:
" cdlHelpCommand_&H102&; executes a Help macro.
" cdlHelpContents_&H3&; displays the Help contents topic as defined by the Contents option in the [OPTION] section of the HPJ file. This constant doesn't work for Help files created with Microsoft Help Workshop Version 4.0X. Instead, you use the value &HB to get the same effect.
" cdlHelpContext_&H1&; displays Help for a particular context. When using this setting, you must also specify a context using the HelpContext property.
" cdlHelpContextPopup_&H8&; displays in a pop-up window a particular Help topic identified by a context number defined in the [MAP] section of the HPJ file.
" cdlHelpForceFile_&H9&; ensures WinHelp displays the correct Help file. If the correct Help file is currently displayed, no action occurs. If the incorrect Help file is displayed, WinHelp opens the correct file.
" cdlHelpHelpOnHelp_&H4&; displays Help for using the Help application itself.
" cdlHelpIndex_&H3&; displays the index of the specified Help file. An application should use this value only for a Help file with a single index.
" cdlHelpKey_&H101&; displays Help for a particular keyword. When using this setting, you must also specify a keyword using the HelpKey property.
" cdlHelpPartialKey_&H105&; displays the topic found in the keyword list that matches the keyword passed in the dwData parameter if there is one exact match.
" cdlHelpQuit_&H2&; notifies the Help application that the specified Help file is no longer in use.
" cdlHelpSetContents_&H5&; determines which contents topic is displayed when a user presses the F1 key.
" cdlHelpSetIndex_&H5&; sets the context specified by the HelpContext property as the current index for the Help file specified by the HelpFile property. 

This index remains current until the user accesses a different Help file. Use this value only for Help files with more than one index. Often, you want to open a Help file to its contents page, and so you'd set the HelpCommand property to the cdlHelpContents constant. Be careful, however, that constant doesn't work with some Help files (those constructed with the Microsoft Help Workshop Version 4.0X), so check if ShowHelp works properly before releasing your program. The cdlHelpContents constant works with fewer Help files than you might think in fact, it won't open the main Windows Help file itself, windows.hlp, correctly. Instead, you must use a special value, &HB:

Private Sub Command1_Click()
CommonDialog1.HelpCommand = &HB
CommonDialog1.HelpFile = "c:\windows\help\windows.hlp"
CommonDialog1.ShowHelp
End Sub

Our ShowHelp example is a success. The code for this example is located in the helpdialog folder on this book_s accompanying CD-ROM.

How To Use Font Dialog Box in Visual Basic

Using A Font Dialog Box
The Testing Department is calling again. Your new word processor, SuperDuperTextPro, is great, but why can't the users select the font they want to use?
You ask, should they be able to do that? The Testing Department says, take a look at the Font dialog box. You use the Common Dialog control's ShowFont method to show a Font dialog box.
Note that before you use the ShowFont method, you must set the Flags property of the Common Dialog control to one of three constants to indicate if you want to display screen fonts, printer fonts, or both. The possible values are as follows:
" cdlCFScreenFonts_&H1; show screen fonts
" cdlCFPrinterFonts_&H2; show printer fonts
" cdlCFBoth_&H3; show both types of fonts

If you don't set one of these in the Flags property, a message box is displayed
advising the user that There are no fonts installed, which will probably cause them to panic. When the user dismisses the Font dialog box by clicking on OK, you can determine their font selections using these properties of the Common Dialog control:
" Color The selected color. To use this property, you must first set the Flags property
to cdlCFEffects.
" FontBold_True if bold was selected.
" FontItalic_True if italic was selected.
" FontStrikethru_True if strikethru was selected. To use this property, you must first set the Flags property to cdlCFEffects.
" FontUnderline_True if underline was selected. To use this property, you must first set the Flags property to cdlCFEffects.
" FontName_The selected font name.
" FontSize_The selected font size.
Let's see an example. Here, we'll let the user set the font, font size, and font styles (like underline and bold) in a text box. We start by setting the Common Dialog control's CancelError property to True so clicking the Cancel button causes a trappable error:

Private Sub Command1_Click()
On Error GoTo Cancel
...
Cancel:
End Sub

Next, we set the Flags property and show the Font dialog box:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects
CommonDialog1.ShowFont
...
Cancel:
End Sub

Finally, we set the text box's properties to match what the user set in the Font dialog box:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects
CommonDialog1.ShowFont
Text1.FontName = CommonDialog1.FontName
Text1.FontBold = CommonDialog1.FontBold
Text1.FontItalic = CommonDialog1.FontItalic
Text1.FontUnderline = CommonDialog1.FontUnderline
Text1.FontSize = CommonDialog1.FontSize
Text1.FontName = CommonDialog1.FontName
Cancel:
End Sub


That's it now we're using Font dialog boxes.The listing for this program,
fontdialog.frm,is located in the fontdialog folder on this book's accompanying CD-ROM.

Setting Font Dialog Flags
You can set a wide variety of options when using Font dialog boxes by using the Common Dialog control's Flags property. Here are the possible values to use with that property:

" cdlCFANSIOnly_&H400; specifies that the dialog box allows only a selection of the fonts that use the Windows character set. If this flag is set, the user won't be able to select a font that contains only symbols.

" cdlCFApply_&H200; enables the Apply button on the dialog box.
" cdlCFBoth_&H3; causes the dialog box to list the available printer and screen fonts. The hDC property identifies the device context associated with the printer.
" cdlCFEffects_&H100; specifies that the dialog box enables strikethru, underline, and color effects.
" cdlCFFixedPitchOnly_&H4000; specifies that the dialog box selects only
fixed-pitch fonts.
" cdlCFForceFontExist_&H10000; specifies that an error message box is displayed if the user attempts to select a font or style that doesn't exist.
" cdlCFHelpButton_&H4; causes the dialog box to display a Help button.
" cdlCFLimitSize_&H2000; specifies that the dialog box selects only font sizes
within the range specified by the Min and Max properties.
" cdlCFNoFaceSel_&H80000; no font name was selected.
" cdlCFNoSimulations_&H1000; specifies that the dialog box doesn't allow graphic device interface (GDI) font simulations.
" cdlCFNoSizeSel_&H200000; no font size was selected.
" cdlCFNoStyleSel_&H100000; no style was selected.
" cdlCFNoVectorFonts_&H800; specifies that the dialog box doesn't allow
vector-font selections.
" cdlCFPrinterFonts_&H2; causes the dialog box to list only the fonts supported by the printer, specified by the hDC property.
" cdlCFScalableOnly_&H20000; specifies that the dialog box allows only the
selection of fonts that can be scaled.
" cdlCFScreenFonts_&H1; causes the dialog box to list only the screen fonts
supported by the system.
" cdlCFTTOnly_&H40000; specifies that the dialog box allows only the selection of TrueType fonts.
" cdlCFWYSIWYG_&H8000; specifies that the dialog box allows only the selection of fonts that are available on both the printer and on screen. If this flag is set, the cdlCFBoth and cdlCFScalableOnly flags should also be set.


You can set more than one flag for a dialog box using the Or operator. For example:
CommonDialog1.Flags = &H10& Or &H200&
Adding the desired constant values produces the same result.

Setting Max And Min Font Sizes
The Testing Department is calling again. Now users are setting the font size in your program, SuperDuperTextPro, to 3 points_and then complaining they can't read what they've typed. Can you limit the allowed font range?
Yes, you can, using the Common Dialog control's Min and Max properties. When you want to make these properties active with a Font dialog box, you must first add the cdlCFLimitSize flag to the Common Dialog control's Flags property. Then you're free to restrict the possible range of font sizes.

Here's an example. We set the Common Dialog's CancelError property to True to catch Cancel button clicks, then set the Flags property of the Common Dialog control to display both screen fonts and printer fonts, and set the cdlCFLimitSize flag:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFLimitSize
...

Then we set the minimum and maximum font sizes we want to allow, measured in points:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFLimitSize
CommonDialog1.Min = 12
CommonDialog1.Max = 24
...

Finally, we show the Font dialog box, and then make use of the newly set font size:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.Flags = cdlCFBoth Or cdlCFLimitSize
CommonDialog1.Min = 12
CommonDialog1.Max = 24
CommonDialog1.ShowFont
Text1.FontName = CommonDialog1.FontSize
Cancel:
End Sub

 
Twitter Bird Gadget