Search In Site

Showing posts with label Visual Basic. Show all posts
Showing posts with label Visual Basic. Show all posts

03 July, 2013

Visual Basic Statements and Expressions


Visual Basic Statements and Expressions
·  The simplest statement is the assignment statement. It consists of a variable name, followed by the assignment operator (=), followed by some
sort of expression.
Examples:
StartTime = Now
Explorer.Caption = "Captain Spaulding"
BitCount = ByteCount * 8
Energy = Mass * LIGHTSPEED ^ 2
NetWorth = Assets - Liabilities
The assignment statement stores information.
· Statements normally take up a single line with no termi nator. Statements can be stacked by using a colon (:) to separate them. Example:
StartTime = Now : EndTime = StartTime + 10
(Be careful stacking statements, especially with If/End If structures. You may not get the response you desire.)
· If a statement is very long, it may be continued to the next line using the
continuation character, an underscore (_). Example:
Months = Log(Final * IntRate / Deposit + 1) _
/ Log(1 + IntRate)
·  Comment statements begin with the keyword Rem or a single quote ('). For
example:
Rem This is a remark
' This is also a remark
x = 2 * y ' another way to write a remark or comment
You, as a programmer, should decide how much to comment your code.
Consider such factors as reuse, your audience, and the legacy of your code.

How to create PDF file from Visual Basic


How to create PDF file from Visual Basic
: example "Hello, PDF!"
This page contatins step-by-step tutorial aimed to teach you how to createPDFfile from Visual Basic application using PDF Creator Pilot library.
1)InstallPDF Creator Pilotlibrary on your computer and runVisual Basic.
You will see New Project Wizard:
2)SelectStandard EXEproject type and clickOpento create new project.
Visual Basic will generate new projectProject1and will automatically open the main form of the generated project:
3)Now we should place a button that will launchl PDF generation function. To place the button just clickCommandButtonon the controls toolbar on the left:
Now press your right mouse button and then drag the mouse so you will set the rectangle for a new control and release the mouse button to create the control. It will look like this:
4)Visual Basic will create new button control on theForm1. Double-click on the control to letVisual Basiccreate the click handler function in the project code: Visual Basicwill create new function which will be launched every time when user clicks the button:
5)Now you have to write the code that will generate PDF file using PDF Creator Pilot . 
To generate PDF document you have to do the following:
1.connect to the PDF Creator Pilot library;
2.set the filename for PDF document;
3.draw "Hello, PDF!" message on the PDF page;
4.disconnect from the library.
Here is the source code:
Private Sub Command1_Click()
’ connect to library
SetPDF = CreateObject("PDFCreatorPilot.piPDFDocument")
’ initialize PDF Engine
PDF.StartEngine "demo@demo", "demo"
’ set PDF ouput filename
PDF.FileName = "HelloPDF_VB.pdf"
PDF.AutoLaunch = True’ auto-open generated pdf document
’ start document generation
PDF.BeginDoc
’ draw "HELLO, PDF" message on the current PDF page
PDF.PDFPAGE_BeginText
PDF.PDFPAGE_SetActiveFont "Verdana", True, False, False, False, 14, 0
PDF.PDFPAGE_TextOut 10, 20, 0, "HELLO, PDF!"
PDF.PDFPAGE_EndText
’ finalize document generation
PDF.EndDoc
’ disconnect from library
SetPDF = Nothing
End Sub
This function will generate PDF document and save it as "HelloPDF_VB.PDF"file in the application’s folder.
Hint:
You can simply copy the source code from the snippet above and then paste it in theVisual Basiccode editor as it’s shown on the
screenshot below:
6)PressF5to run the application (you can also use "Run" | "Start" menu command).
Visual Basic will run the application and you will see its main form:
7)Click "Command1" button and application will generate"HelloPDF_VB.PDF"file.
If you have any PDF viewer (for example,Adobe Acrobat Reader) installed on your computer then the library will launch it to open
the generated PDF document:
You can find the source code of this example in the"\Examples\VB\"sub-folder.

01 July, 2013

working with Images in Visual Basic

Visual Basic has quite an array of techniques for dealing with images. In this chapter, we’ll work with bitmapped images in our programs, creating some powerful effects. We’ll see how to load images in, display them in a variety of ways, including flipping them and stretching them, creating image effects, and saving them back to disk. Images can be an asset to your program, enhancing the visual interface a great deal. We won’t work on creating images here instead, we’ll work on reading them in, working on them, and displaying them from image files on disk.
There are a number of different image formats that you use today: bitmap (.bmp), GIF, JPEG, WMF (Windows metafile format), enhanced WMF, icon (.ico), compressed bitmap (.rle), and more. Visual Basic can handle all these formats.
However, you’ll notice some anachronisms that have crept in over the years that indicate Visual Basic’s historical development 
for example, the picture clip control, which we’ll see in this chapter, can only handle bitmaps with a maximum of 16 colors. This control is still a useful one, but it has largely been superseded by the more powerful image list control.

Picture Boxes Vs. Image Controls
The main controls that programmers use to display images are image controls and picture boxes. That’s not to say there aren’t other ways to display, of course: you can load images into many controls, like buttons, and even display them in forms, as we’ll see in this chapter. However, when  programmers think of displaying and working with images, they often think of picture boxes and image controls. It’s worth noting the difference between these controls. The image control really has one main purpose: to display images. If that’s your goal, the image control is a good choice. On the other hand, picture boxes offer you a great deal more, if you need it. You can even think of picture boxes as mini-paint programs, because they include methods to let you draw text (on top of the current image in the picture box, which is good if you want to label elements in that image), draw circles, lines, boxes, and so on. 
Note, however, that the added power of picture boxes comes with an added cost in terms of heavier use of system resources. If you don’t need a picture box’s added functionality, use an image control.

Image Effects: Working With Images Bit By Bit
In this chapter, we’ll have some fun seeing how to work with images bit by bit. There are two main ways of doing that in Visual Basic: sticking with the Visual Basic methods, and using Windows methods directly.We’ll stick with the Visual Basic methods, which, although slower, are vastly easier to use and get the job done well. However, you should know that we’ll take a look at the Windows way of doing things later in the book, in the chapter on  connecting to Windows directly. (And you may have noticed our bitmapped menu item example in the chapter on menus works directly with Windows to create a bitmap object that it loads into a menu.) We’ll see quite a few image effects in this chapter: embossing images, engraving images, grayscale Images, image lightening, blurring images, making an image seem to sweep from upper left to lower right, and more. All these effects are powerful techniques that you might not expect from Visual Basic.  That’s it for the overview of images for the moment it’s time to turn to the Immediate Solutions.

Adding Images To Controls
The Aesthetic Design Department is calling again. Can’t you add some images to the controls in your program? That would make it look so much nicer.
These days, you can add images to many Visual Basic controls. For example, you can now display images in checkboxes, command buttons, and option buttons if you first set their Style property to Graphical (Style = 1), then place the name of the image file you want to use in the control’s Picture property.
At runtime, you can load a picture into the control’s Picture property using the LoadPicture function:
Private Sub Command1_Click()
Command1.Picture = LoadPicture("c:\image.bmp")
End Sub

Besides buttons, you can also display images in the Visual Basic image combo box The Windows common controls can also display images,including such controls as tree views, list views, and tab strips. There, you load the images you want into an image list control, and then connect that image list to the control using the control’s ImageList property. For more information,

Adding Images To Forms
The Aesthetic Design Department is on the phone again. The form in your program looks pretty drab. How about spicing it up with an image of the company founder? Hmm, you wonder, how would you do that? You can load an image into a form using the form’s Picture property, both at design time or at runtime. As an example, we’ve placed an image in the form. Note that the controls on that form are layered on top of the form’s image. At runtime, you can use the LoadPicture function to read in an image and display it in a form like this:
Private Sub Command1_Click()
Form1.Picture = LoadPicture("c:\image.bmp")
End Sub


Using Image Controls
You use image controls to display images. Although that might seem obvious, it’s usually the deciding factor in whether or not to use an image control or a picture box. Image controls are simple controls that don’t use many system resources, whereas picture boxes are more powerful controls that do. When
you just have an image to display, this is the control to use. You load an image into an image control using its Picture property at design time or runtime. When you load an image in at runtime, use the LoadPicture function this way:
Private Sub Command1_Click()
Image1.Picture = LoadPicture("c:\image.bmp")
End Sub

In addition, image controls size themselves to the image they display automatically, unless you set their Stretch property to True, in which case they size the image to fit themselves. Image controls support events like Click, DblClick, MouseDown, MouseMove, and MouseUp. However, they do not  support all the events that picture boxes support, such as Key events. In general, you use image controls for one purpose only: to display an image (which can include stretching that image). Both image controls and picture boxes can read in images in all the popular formats: GIF, JPEG, BMP, and so on.

Using Picture Boxes
Picture boxes are like mini-paint programs. Not only can they display images they can also create or modify them. You can use the built-in methods of picture boxes to draw text, ellipses, lines, boxes, and more, on top of the images they display.You load an image into a picture box using its Picture property at design time or runtime. When you load an image in at runtime, use the LoadPicture function this way:
Private Sub Command1_Click()
Picture1.Picture = LoadPicture("c:\image.bmp")
End Sub

29 June, 2013

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>

 
Twitter Bird Gadget