Displaying HTML Content with a Web Browser Control in Visual Basic - How to display HTML content generated by a program
(Page 4 of 4 )
There are instances when you would like to display some HTML content produced by an application. But just passing the content to the Navigate() method will not work unless it is embedded in a file. If you were to use an HTML editor, it is very easy to pass the HTML content as an argument, and voila! You have a display of the HTML rendered on a web page. However it is possible to render the display if it can be converted to a file and stored in some location. This section shows how you may be able to do this.
In order to access the input/output capabilities you need to call up a reference to the FileSystemObject. Similar to adding the component to the project, you can also establish reference to libraries by clicking on Project -> References, and from the window that shows up scroll down and select the reference to the Microsoft Script Runtime. This adds the scrrun.dll, which gives access to the FileSystemObject as shown in the object browser in Fig.6.
Fig.6
Now modify the code by adding another button, to whose click event you will add the code shown below. The function call collects the textbox contents and presents it to the outstream. The outstream uses the WriteLine() method to write the text collected to the CFtest.htm file. This file gets persisted to the "C:inetpubwwwroot" directory. The WebBrowser control can now navigate to this file using the path reference to the file.
Private Sub Command2_Click()
Dim strg
strg = Text1.Text
Call browse (strg)
End Sub
Private Sub Form_Load()
WebBrowser1.Visible = False
End Sub
Sub browse (ByVal stringi As String)
WebBrowser1.Visible = True
Dim fsys As New FileSystemObject
Dim outstream As TextStream
testfile = "C:inetpubwwwrootcftest.htm"
Set outstream = fsys.CreateTextFile(testfile, True, False)
outstream.WriteLine (stringi)
Set outstream = Nothing
WebBrowser1.Navigate2 ("http://localhost/cftest.htm")
End Sub
Now by running this project you can verify that the display indeed shows the HTML content as shown in Fig.7.
Fig.7

Summary
The web browser control is a very important ActiveX control for Internet applications. It can be used to display a web page or HTML content. It can also be used for displaying a Word document, RTF document, or XML document inside a Windows form. For example it is possible to create a help file using this control for Windows form-based applications.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |