Exploring the Dialogs Controls in Vb

download Exploring the Dialogs Controls in Vb

of 13

Transcript of Exploring the Dialogs Controls in Vb

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    1/13

    Exploring the Dialogs Controls in Vb.Net

    (Page 1 of 4 )

    Dialog-box controls are an important part of programs, especially if you expect users to interactwith your application. This article explains how to handle several different dialog-box controls.

    Dialogs controls, or more correctly dialog-box controls, are controls that allow the user tointeract with the program and retrieve information. They usually pop up when they are called,and allow you to choose a color from a color-pick dialog, or open or save a file through a dialog,and so forth. The user interacts with these at run time, and they present an interface from whichthe choice is made. They are related to the CommonDialog controls in VB6 and function in asimilar way. However they are now based on well defined classes in the System.Windows.Formsname space as we shall see in this tutorial.

    In the course of this tutorial you will see how they fit into the class structure. You will also try tounderstand their usage by means of an example which uses some of the properties and methodsof these controls.

    Accessing the controls in the IDE

    When you create a WindowsApplication project and add a form to the project, you can see thesecontrols in the Toolbox as shown in this picture. Besides these dialog-box controls there are threeother dialog-box controls related to printing. These are not covered in this tutorial. While theseare built-in controls, it is also possible to create user defined or custom dialog-box controls.Custom dialog-box controls are also not considered.

    These controls are like any other controls; they can be dragged and dropped on the form.However, they are not visible items and find themselves in a tray below the form as shown inthis picture, where one each of these controls have been placed on the form, Form1.

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    2/13

    FontDialog Control

    As the name implies, this control, when called up, brings up a familiar dialog box, Font, fromwhich a variety of choices regarding fonts can be made as shown in this picture. When you clickOKto this screen, your font choices will be applied to a string in some part of your program.

    This next picture shows you the class view of theFontDialogclass with all the members shownon the right. You can use the New() method to instantiate this object and set its properties andmethods. You may notice its hierarchical relationship with the CommonDialog controls.

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    3/13

    Create a WindowsApplication Project, add a form to it and name it suitably. Add a button and atext box control to the form. Drag and drop aFontDialogControl onto the form and it will

    immediately move to the tray underneath the form. To the click event of the button type in thefollowing code:

    Private Sub Button2_Click(ByVal sender As System.Object,

    ByVal e As _ System.EventArgs) Handles Button2.ClickFontDialog1.ShowDialog()

    'FontDialog window pops-upIf Windows.Forms.DialogResult.OK Then

    Me.TextBox1.Text = "Viva la Republica!"

    Me.TextBox1.Font = FontDialog1.Font End IfEnd Sub

    When you build the project and run the program the form pops up. If you now click on the button

    which has the above code in its click event, the FontDialog window shown earlier pop ups.When you make your choice and click on the OK button your choice will be applied to the textbox's font.FontDiaog1.Fonthas all the selected features. For the picture shown next the choiceswere,Font: Lucida Console, FontStyle: Bold, Size:11, and Effects: Underline as seen in the nextpicture. Of course the text box size will not accommodate whatever size you choose, although atdesign-time the text box size is related to your choice of the font, with the default beingMicrosoft MS.

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    4/13

    Exploring the Dialogs Controls in Vb.Net -ColorDialog Control

    (Page 2 of 4 )

    Again as the name suggests, when this control is called up, it will show a color-pick dialog,Color, where you can pick a color that can be applied to the object's color related properties.This can be the backcolor of a text box for example, or the form's backcolor, or forecolor, and soforth.

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    5/13

    The next picture shows the class view of the ColorDialogclass. Again you can trace it back tothe CommonDialogcontrol in the inheritance chain. There are many properties, out of whichonly the Color() property is used. The ShowDialog() is however inherited from theCommonDialog.

    Now to the above project add another button, and after dragging and dropping a ColorDialog

    control onto the form, add the following code to the click event of the button:Private Sub Button1_Click(ByVal sender As System.Object,

    ByVal e As _ System.EventArgs) Handles Button1.ClickColorDialog1.ShowDialog() If Windows.Forms.DialogResult.OK

    Then Me.TextBox1.BackColor = ColorDialog1.Color End IfEnd Sub

    The first line of code, ColorDialog1.ShowDialog() pops up the Color pick window shownearlier. When you make the choice and click OK, the Windows form gets the message and

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    6/13

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    7/13

    This next picture shows you the class view of the FolderBrowserDialog class and its members inthe right panel. You can instantiate using the NEW() keyword.

    Exploring the Dialogs Controls in Vb.Net -OpenFileDialog Control

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    8/13

    (Page 3 of 4 )

    When this control's ShowDialog() method is called, an Open dialog window pops up as shownin the next picture. As we shall see later in the example, you can filter the file extension youwant opened; that is the reason it is showing (*.htm;*. html) as the extensions.

    This next picture shows the class view of this control. However we will be mostly using theproperties and methods of the FileDialog class.

    The properties used for the example shown, such as the filter for file extension, come because

    they are inherited from theFileDialogClass as shown here.

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    9/13

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    10/13

    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 whichopens up this next dialog where you can choose the WebBrowser control as shown. When youclickOK, after placing a check mark this control will be added to the toolbox, from where youcan 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 nextpicture.

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    11/13

    Now to the click event of the button marked "Show selected file in browser " add the followingcode:

    '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()

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    12/13

    If Windows.Forms.DialogResult.OK Then

    WebBrowser1.Navigate(FolderBrowserDialog1.SelectedPath _

    & "" & OpenFileDialog1.FileName)Call SavetoLocal(OpenFileDialog1.FileName)

    End IfEnd If

    End IfEnd 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 = str1SaveFileDialog1.ShowDialog()

    End Sub

    After you build the project and run the form you will see the following. The WebBrowser controlrenders the chosen HTML file. The SaveFileDialog will save this file to a chosen location whichmay be verified.

    Microsoft documentation shows DialogResult.OKas a result of the Dialog control'sShowDialog() method. HoweverDialogResultis an enum, and the qualifying expression will notbe evaluated in their many examples. Windows.Forms.DialogResult.OK is the proper expressionto use, as in this tutorial.

  • 8/14/2019 Exploring the Dialogs Controls in Vb

    13/13

    Summary

    All the dialog-box controls that you find under Dialogs in the ToolBox were described, and theexamples show some of the methods and properties for these controls. These controls are veryuseful as they confer a great deal of flexibility in programming. Object browser is an invaluableaid 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.