Showing posts with label VB.Net Dialog Boxes. Show all posts
Showing posts with label VB.Net Dialog Boxes. Show all posts

Saturday, 14 September 2013

VB.Net - PrintDialog Control

The PrintDialog control lets the user to print documents by selecting a printer and choosing which sections of the document to print from a Windows Forms application.
There are various other controls related to printing of documents. Let us have a brief look at these controls and their purpose. These other controls are:
  • The PrintDocument control - it provides support for actual events and operations of printing in Visual Basic and sets the properties for printing.
  • The PrinterSettings control - it is used to configure how a document is printed by specifying the printer.
  • The PageSetUpDialog control - it allows the user to specify page-related print settings including page orientation, paper size and margin size.
  • The PrintPreviewControl control - it represents the raw preview part of print previewing from a Windows Forms application, without any dialog boxes or buttons.
  • The PrintPreviewDialog control - it represents a dialog box form that contains a PrintPreviewControl for printing from a Windows Forms application.
Following is the Print dialog box:
VB.Net Print Dialog Box

Properties of the PrintDialog Control

The following are some of the commonly used properties of the PrintDialog control:
S.NPropertyDescription
1AllowCurrentPageGets or sets a value indicating whether the Current Page option button is displayed.
2AllowPrintToFileGets or sets a value indicating whether the Print to file check box is enabled.
3AllowSelectionGets or sets a value indicating whether the Selection option button is enabled.
4AllowSomePagesGets or sets a value indicating whether the Pages option button is enabled.
5DocumentGets or sets a value indicating the PrintDocument used to obtain PrinterSettings.
6PrinterSettingsGets or sets the printer settings the dialog box modifies.
7PrintToFileGets or sets a value indicating whether the Print to file check box is selected.
8ShowHelpGets or sets a value indicating whether the Help button is displayed.
9ShowNetworkGets or sets a value indicating whether the Network button is displayed.

Methods of the PrintDialog Control

The following are some of the commonly used methods of the PrintDialog control:
S.NMethod Name & Description
1Reset
Resets all options to their default values.
2RunDialog
When overridden in a derived class, specifies a common dialog box.
3ShowDialog
Runs a common dialog box with a default owner.

Example

In this example, let us see how to show a Print dialog box in a form. Take the following steps:
  1. Add a PrintDocument control, a PrintDialog control and a Button control on the form. The PrintDocument and the PrintDialog controls are found on the Print category of the controls toolbox.
  2. Change the text of the button to 'Print'.
  3. Double-click the Print button and modify the code of the Click event as shown:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   PrintDialog1.Document = PrintDocument1
   PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
   PrintDialog1.AllowSomePages = True
   If PrintDialog1.ShowDialog = DialogResult.OK Then
      PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
      PrintDocument1.Print()
   End If
End Sub
When the application is compiled and run using Start button available at the Microsoft Visual Studio tool bar, it will show following window:
VB.Net Print Dialog Example
Click the Print button to make the Print dialog box appear.

VB.Net - SaveFileDialog Control

The SaveFileDialog control prompts the user to select a location for saving a file and allows the user to specify the name of the file to save data. The SaveFileDialog control class inherits from the abstract class FileDialog.
Following is the Save File dialog box:
VB.Net Save File Dialog Box

Properties of the SaveFileDialog Control

The following are some of the commonly used properties of the SaveFileDialog control:
S.NPropertyDescription
1AddExtensionGets or sets a value indicating whether the dialog box automatically adds an extension to a file name if the user omits the extension.
2CheckFileExistsGets or sets a value indicating whether the dialog box displays a warning if the user specifies a file name that does not exist.
3CheckPathExistsGets or sets a value indicating whether the dialog box displays a warning if the user specifies a path that does not exist.
4CreatePromptGets or sets a value indicating whether the dialog box prompts the user for permission to create a file if the user specifies a file that does not exist.
5DefaultExtGets or sets the default file name extension.
6DereferenceLinksGets or sets a value indicating whether the dialog box returns the location of the file referenced by the shortcut or whether it returns the location of the shortcut (.lnk).
7FileNameGets or sets a string containing the file name selected in the file dialog box.
8FileNamesGets the file names of all selected files in the dialog box.
9FilterGets or sets the current file name filter string, which determines the choices that appear in the "Save as file type" or "Files of type" box in the dialog box.
10FilterIndexGets or sets the index of the filter currently selected in the file dialog box.
11InitialDirectoryGets or sets the initial directory displayed by the file dialog box.
12OverwritePromptGets or sets a value indicating whether the Save As dialog box displays a warning if the user specifies a file name that already exists.
13RestoreDirectoryGets or sets a value indicating whether the dialog box restores the current directory before closing.
14ShowHelpGets or sets a value indicating whether the Help button is displayed in the file dialog box.
15SupportMultiDottedExtensionsGets or sets whether the dialog box supports displaying and saving files that have multiple file name extensions.
16TitleGets or sets the file dialog box title.
16ValidateNamesGets or sets a value indicating whether the dialog box accepts only valid Win32 file names.

Methods of the SaveFileDialog Control

The following are some of the commonly used methods of the SaveFileDialog control:
S.NMethod Name & Description
1OpenFile
Opens the file with read/write permission.
2Reset
Resets all dialog box options to their default values.

Example

In this example, let's save the text entered into a rich text box by the user, using the save file dialog box. Take the following steps:
  1. Drag and drop a Label control, a RichTextBox control, a Button control and a SaveFileDialog control on the form.
  2. Set the Text property of the label and the button control to 'We appreciate your comments' and 'Save Comments' respectively.
  3. Double-click the Save Comments button and modify the code of the Click event as shown:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
      If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK _
   Then
         My.Computer.FileSystem.WriteAllText _
         (SaveFileDialog1.FileName, RichTextBox1.Text, True)
      End If
End Sub
When the application is compiled and run using Start button available at the Microsoft Visual Studio tool bar, it will show following window:
VB.Net SaveFile Dialog Example
We have set the Filter property of the SaveFileDialog control to display text file types with .txt extensions only.
Write some text in the text box and click on the Save Comment button to save the text as a text file in your computer.

VB.Net - OpenFileDialog Control

The OpenFileDialog control prompts the user to open a file and allows the user to select a file to open. The user can check if the file exists and then open it. The OpenFileDialog control class inherits from the abstract class FileDialog.
If the ShowReadOnly property is set to True, then a read-only check box appears in the dialog box. You can also set the ReadOnlyChecked property to True, so that the read-only check box appears checked.
Following is the Open File dialog box:
VB.Net Open File dialog box

Properties of the OpenFileDialog Control

The following are some of the commonly used properties of the OpenFileDialog control:
S.NPropertyDescription
1AddExtensionGets or sets a value indicating whether the dialog box automatically adds an extension to a file name if the user omits the extension.
2AutoUpgradeEnabledGets or sets a value indicating whether this FileDialog instance should automatically upgrade appearance and behavior when running on Windows Vista.
3CheckFileExistsGets or sets a value indicating whether the dialog box displays a warning if the user specifies a file name that does not exist.
4CheckPathExistsGets or sets a value indicating whether the dialog box displays a warning if the user specifies a path that does not exist.
5CustomPlacesGets the custom places collection for this FileDialog instance.
6DefaultExtGets or sets the default file name extension.
7DereferenceLinksGets or sets a value indicating whether the dialog box returns the location of the file referenced by the shortcut or whether it returns the location of the shortcut (.lnk).
8FileNameGets or sets a string containing the file name selected in the file dialog box.
9FileNamesGets the file names of all selected files in the dialog box.
10FilterGets or sets the current file name filter string, which determines the choices that appear in the "Save as file type" or "Files of type" box in the dialog box.
11FilterIndexGets or sets the index of the filter currently selected in the file dialog box.
12InitialDirectoryGets or sets the initial directory displayed by the file dialog box.
13MultiselectGets or sets a value indicating whether the dialog box allows multiple files to be selected.
14ReadOnlyCheckedGets or sets a value indicating whether the read-only check box is selected.
15RestoreDirectoryGets or sets a value indicating whether the dialog box restores the current directory before closing.
16SafeFileNameGets the file name and extension for the file selected in the dialog box. The file name does not include the path.
17SafeFileNamesGets an array of file names and extensions for all the selected files in the dialog box. The file names do not include the path.
18ShowHelpGets or sets a value indicating whether the Help button is displayed in the file dialog box.
19ShowReadOnlyGets or sets a value indicating whether the dialog box contains a read-only check box.
20SupportMultiDottedExtensionsGets or sets whether the dialog box supports displaying and saving files that have multiple file name extensions.
21TitleGets or sets the file dialog box title.
22ValidateNamesGets or sets a value indicating whether the dialog box accepts only valid Win32 file names.

Methods of the OpenFileDialog Control

The following are some of the commonly used methods of the OpenFileDialog control:
S.NMethod Name & Description
1OpenFile
Opens the file selected by the user, with read-only permission. The file is specified by the FileName property.
2Reset
Resets all options to their default value.

Example

In this example, let's load an image file in a picture box, using the open file dialog box. Take the following steps:
  1. Drag and drop a PictureBox control, a Button control and a OpenFileDialog control on the form.
  2. Set the Text property of the button control to 'Load Image File'.
  3. Double-click the Load Image File button and modify the code of the Click event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
   End If
End Sub
When the application is compiled and run using Start button available at the Microsoft Visual Studio tool bar, it will show following window:
VB.Net Open File Dialog Example
Click on the Load Image File button to load an image stored in your computer.
VB.Net Open File Dialog Example

VB.Net - FontDialog Control

It prompts the user to choose a font from among those installed on the local computer and lets the user select the font, font size, and color. It returns the Font and Color objects.
Following is the Font dialog box:
VB.Net Font Dialog
By default the Color ComboBox is not shown on the Font dialog box. You should set the ShowColorproperty of the FontDialog control to be True.

Properties of the FontDialog Control

The following are some of the commonly used properties of the FontDialog control:
S.NPropertyDescription
1AllowSimulationsGets or sets a value indicating whether the dialog box allows graphics device interface (GDI) font simulations.
2AllowVectorFontsGets or sets a value indicating whether the dialog box allows vector font selections.
3AllowVerticalFontsGets or sets a value indicating whether the dialog box displays both vertical and horizontal fonts, or only horizontal fonts.
4ColorGets or sets the selected font color.
5FixedPitchOnlyGets or sets a value indicating whether the dialog box allows only the selection of fixed-pitch fonts.
6FontGets or sets the selected font.
7FontMustExistGets or sets a value indicating whether the dialog box specifies an error condition if the user attempts to select a font or style that does not exist.
8MaxSizeGets or sets the maximum point size a user can select.
9MinSizeGets or sets the minimum point size a user can select.
10ScriptsOnlyGets or sets a value indicating whether the dialog box allows selection of fonts for all non-OEM and Symbol character sets, as well as the ANSI character set.
11ShowApplyGets or sets a value indicating whether the dialog box contains anApply button.
12ShowColorGets or sets a value indicating whether the dialog box displays the color choice.
13ShowEffectsGets or sets a value indicating whether the dialog box contains controls that allow the user to specify strikethrough, underline, and text color options.
14ShowHelpGets or sets a value indicating whether the dialog box displays a Help button.

Methods of the FontDialog Control

The following are some of the commonly used methods of the FontDialog control:
S.NMethod Name & Description
1Reset
Resets all options to their default values.
2RunDialog
When overridden in a derived class, specifies a common dialog box.
3ShowDialog
Runs a common dialog box with a default owner.

Events of the FontDialog Control

The following are some of the commonly used events of the FontDialog control:
S.NEventDescription
1ApplyOccurs when the Apply button on the font dialog box is clicked.

Example

In this example, let's change the font and color of the text from a rich text control using the Font dialog box. Take the following steps:
  1. Drag and drop a RichTextBox control, a Button control and a FontDialog control on the form.
  2. Set the Text property of the button control to 'Change Font'.
  3. Set the ShowColor property of the FontDialog control to True.
  4. Double-click the Change Color button and modify the code of the Click event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   If FontDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      RichTextBox1.ForeColor = FontDialog1.Color
      RichTextBox1.Font = FontDialog1.Font
   End If
End Sub
When the application is compiled and run using Start button available at the Microsoft Visual Studio tool bar, it will show following window:
VB.Net Font dialog Example
Enter some text and Click on the Change Font button.
VB.Net Font Dialog Example
The Font dialog appears, select a font and a color and click the OK button. The selected font and color will be applied as the font and fore color of the text of the rich text box.
VB.Net Font Dialog Example

VB.Net - ColorDialog Control

The ColorDialog control class represents a common dialog box that displays available colors along with controls that enable the user to define custom colors. It lets the user select a color.
The main property of the ColorDialog control is Color, which returns a Color object.
Following is the Color dialog box:
VB.Net Color Dialog

Properties of the ColorDialog Control

The following are some of the commonly used properties of the ColorDialog control:
S.NPropertyDescription
1AllowFullOpenGets or sets a value indicating whether the user can use the dialog box to define custom colors.
2AnyColorGets or sets a value indicating whether the dialog box displays all available colors in the set of basic colors.
3CanRaiseEventsGets a value indicating whether the component can raise an event.
4ColorGets or sets the color selected by the user.
5CustomColorsGets or sets the set of custom colors shown in the dialog box.
6FullOpenGets or sets a value indicating whether the controls used to create custom colors are visible when the dialog box is opened
7ShowHelpGets or sets a value indicating whether a Help button appears in the color dialog box.
8SolidColorOnlyGets or sets a value indicating whether the dialog box will restrict users to selecting solid colors only.

Methods of the ColorDialog Control

The following are some of the commonly used methods of the ColorDialog control:
S.NMethod Name & Description
1Reset
Resets all options to their default values, the last selected color to black, and the custom colors to their default values.
2RunDialog
When overridden in a derived class, specifies a common dialog box.
3ShowDialog
Runs a common dialog box with a default owner.

Events of the ColorDialog Control

The following are some of the commonly used events of the ColorDialog control:
S.NEventDescription
1HelpRequestOccurs when the user clicks the Help button on a common dialog box.

Example

In this example, let's change the fore color of a label control using the color dialog box. Take the following steps:
  1. Drag and drop a label control, a button control and a ColorDialog control on the form.
  2. Set the Text property of the label and the button control to 'Give me a new Color' and 'Change Color' respectively.
  3. Change the font of the label as per your likings
  4. Double-click the Change Color button and modify the code of the Click event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   If ColorDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Label1.ForeColor = ColorDialog1.Color
   End If
End Sub
When the application is compiled and run using Start button available at the Microsoft Visual Studio tool bar, it will show following window:
VB.Net Color Dialog Result
Clicking on the Change Color button, the color dialog appears, select a color and click the OK button. The selected color will be applied as the fore color of the text of the label.