Working with different dialog controls in VB.NET

Abhisek
Posted by in Windows Forms category on for Intermediate level | Views : 8709 red flag

This article describes how to work with different dialog controls in VB.NET with code and detailed description.
In VB.NET there are many dialog controls like SaveFile Dialog, Print Dialog, Font Dialog etc. This code snippet demonstrates how to work with different dialog controls in VB.NET. Dialog controls help us to do some work quickly and easily and also provides us many options.

First add a rich textbox to your windows form and add five buttons(Save, Print, Open, Font, Color).

Change the name property of richtextbox to txtFile and buttons to btnSave, btnPrint, btnOpen, btnFont, btnColor respectively.

Then add OpenFile Dialog, Print Dialog, SaveFile Dialog, Font Dialog and Color Dialog to your application.

SEE THE CODE:



Now add the following code to the respective event handlers of buttons.


Imports System.IO
Imports System.Drawing.Printing

Public Class Form1
Inherits System.Windows.Forms.Form

' Declare variable...
' set the default path
Private strFileName As String = "C:\Temp\Text Document.txt"
Private objStreamToPrint As StreamReader
Private objPrintFont As Font

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents txtFile As System.Windows.Forms.TextBox
Friend WithEvents btnSave As System.Windows.Forms.Button
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents btnFont As System.Windows.Forms.Button
Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog
Friend WithEvents btnColor As System.Windows.Forms.Button
Friend WithEvents btnOpen As System.Windows.Forms.Button
Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
Friend WithEvents btnPrint As System.Windows.Forms.Button
Friend WithEvents PrintDialog1 As System.Windows.Forms.PrintDialog
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Private Sub InitializeComponent()
Me.btnOpen = New System.Windows.Forms.Button
Me.txtFile = New System.Windows.Forms.TextBox
Me.btnSave = New System.Windows.Forms.Button
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.btnFont = New System.Windows.Forms.Button
Me.FontDialog1 = New System.Windows.Forms.FontDialog
Me.btnColor = New System.Windows.Forms.Button
Me.ColorDialog1 = New System.Windows.Forms.ColorDialog
Me.btnPrint = New System.Windows.Forms.Button
Me.PrintDialog1 = New System.Windows.Forms.PrintDialog
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SuspendLayout()
'
'btnOpen
'
Me.btnOpen.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnOpen.Location = New System.Drawing.Point(368, 8)
Me.btnOpen.Name = "btnOpen"
Me.btnOpen.Size = New System.Drawing.Size(75, 23)
Me.btnOpen.TabIndex = 0
Me.btnOpen.Text = "Open"
'
'txtFile
'
Me.txtFile.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtFile.Location = New System.Drawing.Point(10, 8)
Me.txtFile.Multiline = True
Me.txtFile.Name = "txtFile"
Me.txtFile.Size = New System.Drawing.Size(352, 264)
Me.txtFile.TabIndex = 1
'
'btnSave
'
Me.btnSave.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnSave.Location = New System.Drawing.Point(368, 40)
Me.btnSave.Name = "btnSave"
Me.btnSave.Size = New System.Drawing.Size(75, 23)
Me.btnSave.TabIndex = 2
Me.btnSave.Text = "Save"
'
'SaveFileDialog1
'
Me.SaveFileDialog1.FileName = "doc1"
'
'btnFont
'
Me.btnFont.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnFont.Location = New System.Drawing.Point(368, 72)
Me.btnFont.Name = "btnFont"
Me.btnFont.Size = New System.Drawing.Size(75, 23)
Me.btnFont.TabIndex = 3
Me.btnFont.Text = "Font"
'
'btnColor
'
Me.btnColor.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnColor.Location = New System.Drawing.Point(368, 104)
Me.btnColor.Name = "btnColor"
Me.btnColor.Size = New System.Drawing.Size(75, 23)
Me.btnColor.TabIndex = 4
Me.btnColor.Text = "Color"
'
'btnPrint
'
Me.btnPrint.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnPrint.Location = New System.Drawing.Point(368, 136)
Me.btnPrint.Name = "btnPrint"
Me.btnPrint.Size = New System.Drawing.Size(75, 23)
Me.btnPrint.TabIndex = 5
Me.btnPrint.Text = "Print"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(448, 277)
Me.Controls.Add(Me.btnPrint)
Me.Controls.Add(Me.btnColor)
Me.Controls.Add(Me.btnFont)
Me.Controls.Add(Me.btnSave)
Me.Controls.Add(Me.txtFile)
Me.Controls.Add(Me.btnOpen)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Dialogs"
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

#End Region

' event handeler for Open
Public Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
' Set the Open dialog properties...
With OpenFileDialog1
.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
.FilterIndex = 1
.InitialDirectory = "C:\Temp\"
.Title = "Open File Dialog"
End With

' Show the Open dialog and if the user clicks the OK button,
' load the file...
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
strFileName = OpenFileDialog1.FileName
Dim objReader As StreamReader = New StreamReader(strFileName)
txtFile.Text = objReader.ReadToEnd()
objReader.Close()
objReader = Nothing
End If
End Sub

' event handeler for Save
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
' Set the Save dialog properties...
With SaveFileDialog1
.DefaultExt = "txt"
.FileName = strFileName
.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
.FilterIndex = 1
.InitialDirectory = "C:\Temp\"
.OverwritePrompt = True
.Title = "Save File Dialog"
End With

' Show the Save dialog and if the user clicks the Save button,
' load the file...
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
strFileName = SaveFileDialog1.FileName
Dim objWriter As StreamWriter = _
New StreamWriter(strFileName, False)
objWriter.Write(txtFile.Text)
objWriter.Close()
objWriter = Nothing
End If
End Sub
' event handeler for Font
Private Sub btnFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFont.Click
' Set the FontDialog control properties...
FontDialog1.ShowColor = True

' Show the Font dialog...
If FontDialog1.ShowDialog() = DialogResult.OK Then
' If the OK button was clicked set the font
' in the text box on the form...
txtFile.Font = FontDialog1.Font
' Set the color of the font in the text box on the form...
txtFile.ForeColor = FontDialog1.Color
End If
End Sub
' event handeler for Color
Private Sub btnColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColor.Click
' Show the Color dialog...
If ColorDialog1.ShowDialog() = DialogResult.OK Then
' Set the ForeColor property of all the controls on the form...
btnOpen.ForeColor = ColorDialog1.Color
btnSave.ForeColor = ColorDialog1.Color
btnFont.ForeColor = ColorDialog1.Color
btnColor.ForeColor = ColorDialog1.Color
End If
End Sub
' event handeler for Print
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
' Declare an object for the PrintDocument class...
Dim objPrintDocument As PrintDocument = New PrintDocument()

' Set the DocumentName property...
objPrintDocument.DocumentName = "Text File Print Demo"

' Set the PrintDialog properties...
PrintDialog1.AllowPrintToFile = False
PrintDialog1.AllowSelection = False
PrintDialog1.AllowSomePages = False

' Set the Document property to the objPrintDocument object...
PrintDialog1.Document = objPrintDocument

' Show the Print dialog...
If PrintDialog1.ShowDialog() = DialogResult.OK Then
' If the user clicked on the OK button then set the
' StreamReader object to the file name in the strFileName
' variable...
objStreamToPrint = New StreamReader(strFileName)

' Set the print font...
objPrintFont = New Font("Arial", 10)

' Add an event handler for the PrintPage event of the
'objPrintDocument object...
AddHandler objPrintDocument.PrintPage, AddressOf objPrintDocument_PrintPage

' Set the PrinterSettings property of the objPrintDocument
' object to the PrinterSettings property returned from the
' PrintDialog control...
objPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings

' Print the text file...
objPrintDocument.Print()

' Clean up...
objStreamToPrint.Close()
objStreamToPrint = Nothing
End If

End Sub


Private Sub objPrintDocument_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs)
' Declare variables...
Dim sngLinesPerpage As Single = 0
Dim sngVerticalPosition As Single = 0
Dim intLineCount As Integer = 0
Dim sngLeftMargin As Single = e.MarginBounds.Left
Dim sngTopMargin As Single = e.MarginBounds.Top
Dim strLine As String

' Work out the number of lines per page.
' Use the MarginBounds on the event to do this...
sngLinesPerpage = _
e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)

' Now iterate through the file printing out each line.
' This assumes that a single line is not wider than the page
' width. Check intLineCount first so that we don't read a line
' that we won't print...
strLine = objStreamToPrint.ReadLine()

While (intLineCount < sngLinesPerpage And Not (strLine Is Nothing))

' Calculate the vertical position on the page...
sngVerticalPosition = sngTopMargin + _
(intLineCount * objPrintFont.GetHeight(e.Graphics))

' Pass a StringFormat to DrawString for the
' Print Preview control...
e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, _
sngLeftMargin, sngVerticalPosition, New StringFormat())

' Increment the line count...
intLineCount = intLineCount + 1

' If the line count is less than the lines per page then
' read another line of text...
If (intLineCount < sngLinesPerpage) Then
strLine = objStreamToPrint.ReadLine()
End If

End While

' If we have more lines then print another page...
If (strLine <> Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If

End Sub



End Class


Description:



First we are creating the Dialogs and change there properties.

When user will click on the Save button then a savefile dialog will be open where the user can save the text entered inside the textbox with a name. If user will not give any path then it will save the file to the default location.

When user click on Open button it will give you an openfile dialog in which the user can browse for a particular text file and show it on the rich textbox.

When user click on Print button a print dialog will open in which the user can select his printer, paper type and other options to print the file.

When user click on font and color button font dialog and color dialog will be opened respectively.


Page copy protected against web site content infringement by Copyscape

About the Author

Abhisek
Full Name: Abhisek Panda
Member Level: Bronze
Member Status: Member
Member Since: 10/11/2009 6:25:59 AM
Country: India
Abhisek Panda

Thanks and Regards Abhisek Panda Go Green And Save Your Future

Login to vote for this post.

Comments or Responses

Posted by: Vinay13mar on: 10/15/2012 | Points: 25

Login to post response

Comment using Facebook(Author doesn't get notification)