Home

Windows Control: The Open Dialog Box

 

Documents

 

Introduction

When creating an application, if it is a word processor, after opening the application, it may display an empty area in which the user can start typing. If the user is working on a spreadsheet, he or she may start typing numbers and performing calculations. In the same way, a user who is facing a graphics application would start drawing in it. The object that a user would be using is called a document. After working on a document for a while, there are some other automatic ideas that come in the mind of a user. One would lead to saving the document. Another would consist of printing it. All these routine operations should be available to the user. This aspect of the application is taken care of by the person who creates the application. Not every application allows a user to save or to print its documents. If you want these operations to be possible, you must (explicitly) provide them.

     

Windows Common Dialog Boxes

To support the various operations of saving a document, opening an existing document, printing a document, setting up printing, etc, Microsoft Windows provides a series of standard dialog boxes that are available almost regardless of the programming environment you are using to develop your application. These common dialog boxes are stored in libraries (DLLs) that ship with the operating system but they may be provided in a raw format. For this reason, except if programming in Win32, the programming environment you use provides a customized and friendlier technique of adding these dialog boxes to your application. In the same way, the .NET Framework provides its own implementation of these ready-made dialog boxes in a manner that makes it easier to implement them.

To use a standard Windows dialog box, from the Toolbox, click the button that corresponds to the dialog box you want to add and click the form anywhere. The position of the control on the form has no importance because it is only a representative: it will not appear when the form is running. Once the desired dialog’s icon is on the form, place a button on the form or create a menu item that will be used to call the dialog box.

Introduction to the Open File Dialog Box

 

Description

Besides saving files, another common operation performed by users consists of opening files. This refers to existing files since a file must primarily exist. To support this operation, Microsoft Windows provides a standard object: the Open File dialog box:

Description of the Open File Dialog Box
  

Practical LearningPractical Learning: Introduction the Open File Dialog Box

  1. In the Solution Explorer, right-click Form1.vb and click Rename
  2. Type PictureViewer.vb and press Enter twice
  3. Right-click the form and click View Code
  4. Declare a private Bitmap variable named bmpPicture
     
    Public Class PictureViewer
        Private bmpPicture As Bitmap
    End Class
  5. In the Class Name combo box, select PictureViewer
  6. In the Method Name combo box, select Paint and implement the event as follows:
     
    Private Sub pbxViewerPaint(ByVal sender As Object, 
                                ByVal e As System.Windows.Forms.PaintEventArgs) 
                                    Handles pbxViewer.Paint
            If Not bmpPicture Is Nothing Then
                pbxViewer.Image = bmpPicture
            End If
    End Sub
  7. In the Class Name combo box, select mnuToolsFlip
  8. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuToolsFlipClick(ByVal sender As Object, 
                                       ByVal e As System.EventArgs) 
                                       Handles mnuToolsFlip.Click
            If Not bmpPicture Is Nothing Then
                bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipY)
                Invalidate()
            End If
    End Sub
  9. In the Class Name combo box, select mnu Tools Mirror
  10. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuToolsMirrorClick(ByVal sender As Object, 
                                         ByVal e As System.EventArgs) 
                                         Handles mnuToolsMirror.Click
            If Not bmpPicture Is Nothing Then
                bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipX)
                Invalidate()
            End If
    End Sub
  11. In the Class Name combo box, select mnuToolsRotate90 Left
  12. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuToolsRotate90LeftClick(ByVal sender As Object, 
                                               ByVal e As System.EventArgs) 
                                               Handles mnuToolsRotate90Left.Click
            If Not bmpPicture Is Nothing Then
                bmpPicture.RotateFlip(RotateFlipType.Rotate270FlipNone)
                Invalidate()
            End If
    End Sub
  13. In the Class Name combo box, select mnuToolsRotate90 Right
  14. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuToolsRotate90RightClick(ByVal sender As Object, 
                                                ByVal e As System.EventArgs) 
                                            Handles mnuToolsRotate90Right.Click
            If Not bmpPicture Is Nothing Then
                bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipNone)
                Invalidate()
            End If
    End Sub
  15. In the Class Name combo box, select mnuToolsRotate180
  16. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuToolsRotate180Click(ByVal sender As Object, 
                                            ByVal e As System.EventArgs) 
                                            Handles mnuToolsRotate180.Click
            If Not bmpPicture Is Nothing Then
                bmpPicture.RotateFlip(RotateFlipType.Rotate180FlipNone)
                Invalidate()
            End If
    End Sub
  17. In the Class Name combo box, select mnuTools FlipRotate90 Left
  18. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuToolsFlipRotate90LeftClick(ByVal sender As Object, 
                                               ByVal e As System.EventArgs) 
                                       Handles mnuToolsFlipRotate90Left.Click
            If Not bmpPicture Is Nothing Then
                bmpPicture.RotateFlip(RotateFlipType.Rotate270FlipX)
                Invalidate()
            End If
    End Sub
  19. In the Class Name combo box, select mnuTools FlipRotate90 Right
  20. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuToolsFlipRotate90RightClick(ByVal sender As Object, 
                                                ByVal e As System.EventArgs) 
                                        Handles mnuToolsFlipRotate90Right.Click
            If Not bmpPicture Is Nothing Then
                bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipX)
                Invalidate()
            End If
    End Sub
  21. In the Class Name combo box, select mnuTools FlipRotate180
  22. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuToolsFlipRotate180Click(ByVal sender As Object, 
                                                ByVal e As System.EventArgs) 
                                        Handles mnuToolsFlipRotate180.Click
            If Not bmpPicture Is Nothing Then
                bmpPicture.RotateFlip(RotateFlipType.Rotate180FlipX)
                Invalidate()
            End If
    End Sub
  23. In the Class Name combo box, select mnu Original Size
  24. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuOriginalSizeClick(ByVal sender As Object, 
                                          ByVal e As System.EventArgs) 
                                          Handles mnuOriginalSize.Click
            If Not bmpPicture Is Nothing Then
                pbxViewer.SizeMode = PictureBoxSizeMode.Normal
    
                mnuOriginalSize.Checked = True
                mnuCenterImage.Checked = False
                mnuOccupyClientArea.Checked = False
                mnuZoom.Checked = False
            End If
    End Sub
  25. In the Class Name combo box, select mnu Center Image
  26. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuCenterImageClick(ByVal sender As Object, 
                                         ByVal e As System.EventArgs) 
                                         Handles mnuCenterImage.Click
            If Not bmpPicture Is Nothing Then
                pbxViewer.SizeMode = PictureBoxSizeMode.CenterImage
    
                mnuOriginalSize.Checked = False
                mnuCenterImage.Checked = True
                mnuOccupyClientArea.Checked = False
                mnuZoom.Checked = False
            End If
    End Sub
  27. In the Class Name combo box, select mnu Occupy Client Area
  28. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuOccupyClientAreaClick(ByVal sender As Object, 
                                          ByVal e As System.EventArgs) 
                                          Handles mnuOccupyClientArea.Click
            If Not bmpPicture Is Nothing Then
                pbxViewer.SizeMode = PictureBoxSizeMode.StretchImage
    
                mnuOriginalSize.Checked = False
                mnuCenterImage.Checked = False
                mnuOccupyClientArea.Checked = True
                mnuZoom.Checked = False
            End If
    End Sub
  29. In the Class Name combo box, select mnu Zoom
  30. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuZoomClick(ByVal sender As Object, 
                                  ByVal e As System.EventArgs) 
                                  Handles mnuZoom.Click
            If Not bmpPicture Is Nothing Then
                pbxViewer.SizeMode = PictureBoxSizeMode.Zoom
    
                mnuOriginalSize.Checked = False
                mnuCenterImage.Checked = False
                mnuOccupyClientArea.Checked = False
                mnuZoom.Checked = True
            End If
    End Sub
  31. Return to the form

Open File Dialog Box Creation

To provide file opening support, the .NET Framework provides the OpenFileDialog class which is derived from the FileDialog class that in fact provides most of its functionality. The easiest way to use it is to click the OpenFileDialog button OpenFileDialog from the Toolbox and click the form. You can click anywhere on the form because the OpenFileDialog object would not be seen at run time. After placing it on the form, you can use the Properties window to configure it.

If you prefer to dynamically create an Open dialog box, declare a variable of type OpenFileDialog and use the new operator to allocate memory using its default constructor. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Private FileOpener As OpenFileDialog

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

            FileOpener = New OpenFileDialog

        End Sub

    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

Practical LearningPractical Learning: Enabling or Disabling Menu Items

  1. From the Dialogs section of the Toolbox, click OpenFileDialog OpenFileDialog and click the form
  2. While the open file control is still selected under the form, in the Properties window, click (Name) and type FileOpenerPicture

Characteristics of an Open File Dialog Box

 

The Filename

One of the most important properties of an Open dialog box is the file it presents to the user. This is represented by the FileName property. If you want a default file to be specified when the dialog box comes up, you can specify this in the FileName property of the Properties window. If you need to use this property, you should make sure the file can be found. If the file is located in the same folder as the application, you can provide just its name. If the file is located somewhere else in the hard drive, you should provide its complete path. Most of the time, you will not be concerned with this property if you are creating an application that will allow the users to open any files of their choice. Once a file is located, it can be accessed using the OpenFileDialog.FileName property.

Practical LearningPractical Learning: Using the File Name

  • Still in the Properties window for the open file control, click FileName and delete its string

The Filter

To make your application more effective, you should know what types of files your application can open. This is taken care of by specifying a list of extensions for the application. To control the types of files that your application can open, specify their extensions using the Filter Property. The Filter string is created exactly like that of a SaveFileDialog control as we saw earlier.

Practical LearningPractical Learning: Filtering Files

  1. Still in the Properties window for the open file control, click Filter and type:
    Bitmap Files (*.bmp)|*.bmp|JPEG Files (*.jpg,*.jpeg)|*.jpg|GIF Files (*.gif)|*.gif|PNG Files (*.png)|*.png
  2. Save the form

The Filter Index

Like the SaveFileDialog control, the default extension is the one the dialog box would first filter during file opening. If you want the Open dialog box to easily recognize a default type of file when the dialog box opens, you can specify the extension's type using the DefaultExt property. You can also use the FilterIndex we saw earlier to indicate the default index of the Files of Type combo box.

For convenience, or for security reasons, Open dialog boxes of applications are sometimes asked to first look for files in a specific location when the Open File dialog box comes up. This default folder is specified using the InitialDirectory property.

Practical LearningPractical Learning: Specifying the Default Extension

  1. Still in the Properties window for the open file control, click DefaultExt and type bmp
  2. Save the form

Showing the Dialog Box

The essence of using the Open dialog box is to be able to open a file. This job is handled by the ShowDialog() method. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Private FileOpener As OpenFileDialog

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

            FileOpener = New OpenFileDialog
            FileOpener.ShowDialog()

        End Sub

    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

After opening the dialog box, if the user selects a file and clicks OK or presses Enter, the file would be opened.

Practical LearningPractical Learning: Using the Open File Dialog Box

  1. In the Class Name combo box, select mnu File Open Picture
  2. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuFileOpenPictureClick(ByVal sender As Object, 
                                             ByVal e As System.EventArgs) 
                                             Handles mnuFileOpenPicture.Click
            If OpenPicture.ShowDialog() = Windows.Forms.DialogResult.OK Then
                bmpPicture = New Bitmap(OpenPicture.FileName)
                Invalidate()
                mnuOriginalSizeClick(sender, e)
            End If
    End Sub
  3. In the Class Name combo box, select mnuFileExit
  4. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuFileExitClick(ByVal sender As Object, 
                                      ByVal e As System.EventArgs) 
                                      Handles mnuFileExit.Click
            End
    End Sub
  5. Execute the application
  6. Open a picture and use the menu items
     
  7. Close the form and return to your programming environment

Opening Various Files

Most of the time, users are interested in opening one file to view or manipulate. It is also possible for a user to want to select various files and open them at the same time. When the user clicks OK after using the Open dialog box, before taking the next step, you may need to find out whether the user selected various files. To get this information, you can check the value of the OpenFileDialog.Multiselect Boolean property. If the user had selected various files, this property produces a true result. If the user selected only one file, this property renders a false result. The result of this checking process allows you either to agree to open the files or to take some action of your choice.

Opening a File As Read-Only

After a file has been opened, the user may want to alter it. For example, if it is a text document, the user may want to add and/or delete some words. In some cases, you may want the user to be able to open a file but not to be able to modify it. To provide this restriction, you can set the document as read-only. In some other cases, you may want to let the user decide on this. If you want to give this option to the user, you can start by displaying a read-only check box on the dialog box. To support this, the OpenFileDialog class is equipped with the ShowReadOnly property. If you set it to true, the dialog box would be equipped with an Open As Read-Only check box in its lower section:

Open

By default, the Open As Read-Only check box is cleared when the dialog box comes up. The user has the option of keeping it that way or checking it when opening a file. The OpenFileDialog class provides the ReadOnlyChecked property to accompany the read-only option. If you want to display a check mark in the Open As Read-Only check box, you can set the ReadOnlyChecked property to true. On the other hand, when the user selects a file to open, you can check the value of the ReadOnlyChecked property. If it is true, this indicates that the user had clicked the Open As Read-Only check box.

 

Home Copyright © 2008-2012 FunctionX