Toolbox classes with no visual interface

We have been writing programs that incorporate visible objects like Buttons and Labels in the user interface, but not all objects are visible to the user.

An OpenFileDialog is an "invisible" object. This image display program allows the user to select an image to be displayed using an OpenFileDialog. The OpenFileDialog does not appear in the user interface, but, even if you can't see it, it is included in the program, and you can refer to its properties and execute its methods as you would with a visible user-interface object.

This is illustrated in the event handlers of the image display program:

     'select and display image
      Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
          ofdPicture.ShowDialog()
          lblFile.Text = ofdPicture.FileName
          picDisplay.Image = Image.FromFile(ofdPicture.FileName)
      End Sub

      'clear image
      Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
          lblFile.Text = ""
          picDisplay.Image = Nothing
      End Sub

      'terminate execution
      Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
          Me.Close()
      End Sub

The first event handler uses the ShowDialog method of an OpenfileDialog. When the user clicks on the Display button, ShowDialog is executed and a file browser dialog box is displayed so the user can select a file:

The user searches for the file to be displayed, and when he or she selects the file (image02.gif in this example), its path and name are assigned to the Filename property of ofdPicture.

The program then displays the file name and path on the label at the top of window, showing the user the name of the file to be displayed.

Finally, the file and path are used to retrieve the image from disk and assign it to the image property of the picture box.

The other two event handlers are very simple. The second event handler sets the image to Nothing, which clears the PictureBox, and the third event handler terminates execution.

You might wonder how the user includes OpenFileDialogs and other objects that do not have user interfaces in a project. When a Button, Label or other object is included, the programmer places it on the Form and sets its properties. Since these non-interface objects do not appear on the form, they are shown in the component tray window that opens just below the form, as shown below:

This enables the programmer to set properties at design time as well as during execution.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.