Exploring the Dialogs Controls in Vb.Net - Usage of these Controls
(Page 4 of 4 )
To the project already created add a WebBrowser control. If this is not already in the toolbox, you may add it to the toolbox from Tools -->ChooseToolBoxItems... from the main menu which opens up this next dialog where you can choose the WebBrowser control as shown. When you click OK, after placing a check mark this control will be added to the toolbox, from where you can drag and drop it onto your form. You should also drag and drop the the three controls, OpenFileDialog, SaveFileDialog, and the FolderBrowserDialog.

Add one more button to the form, and the finished form should appear as shown in the next picture.

Now to the click event of the button marked "Show selected file in browser " add the following code:
'This event opens a dialog from which a folder is chosen. After the folder
'is chosen, the OpenFileDialog properties such as Filter, InitialDirectory
'are set and then the ShowDialog() is called. When this window is closed
'with an 'OK' choice, the WebBrowser navigates to the folder and then on to
'the filename and gets rendered in the WebBrowser control shown as a large
'white area in the form. It is assumed that you will browse to get a web
'page with an *.htm extension. This is then followed by a call to the
'SaveToLocal() procedure
Private Sub Button3_Click(ByVal sender As
System.Object, ByVal e As _
System.EventArgs) Handles Button3.Click
FolderBrowserDialog1.ShowDialog()
If Windows.Forms.DialogResult.OK Then
If Windows.Forms.DialogResult.OK Then
OpenFileDialog1.Filter = "(*.htm;*.html)|*.htm;*.html)"
OpenFileDialog1.InitialDirectory = _
FolderBrowserDialog1.SelectedPath
OpenFileDialog1.ShowDialog()
If Windows.Forms.DialogResult.OK Then
WebBrowser1.Navigate(FolderBrowserDialog1.SelectedPath _
& "" & OpenFileDialog1.FileName)
Call SavetoLocal(OpenFileDialog1.FileName)
End If
End If
End If
End Sub
'this procedure saves the file to a browsed folder. Although the
'InitialDirectory property has been set, it appears not to accept it.
'However, you can choose the directory where you want to it to be saved.
Private Sub SaveToLocal (ByVal str1 As String)
SaveFileDialog1.Filter = "(*.htm)|*.htm"
SaveFileDialog1.InitialDirectory = "C:"
SaveFileDialog1.FileName = str1
SaveFileDialog1.ShowDialog()
End Sub
After you build the project and run the form you will see the following. The WebBrowser control renders the chosen HTML file. The SaveFileDialog will save this file to a chosen location which may be verified.

Microsoft documentation shows DialogResult.OK as a result of the Dialog control's ShowDialog() method. However DialogResult is an enum, and the qualifying expression will not be evaluated in their many examples. Windows.Forms.DialogResult.OK is the proper expression to use, as in this tutorial.

Summary
All the dialog-box controls that you find under Dialogs in the ToolBox were described, and the examples show some of the methods and properties for these controls. These controls are very useful as they confer a great deal of flexibility in programming. Object browser is an invaluable aid in understanding the hierarchies and the inheritance chains, and you must review it as often as you can. You may also review online help when you want to look for more specifics.
| 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. |