Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29948 |  Welcome, Guest!   Register  Login
Home > Articles > Windows Forms > Working with different dialog controls in VB.NET

Working with different dialog controls in VB.NET

Article posted by Abhisek on 10/12/2009 | Views: 5358 | Category: Windows Forms | Level: Intermediate 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.


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:0 year(s)
Home page:
Member since:Sunday, October 11, 2009
Level:Bronze
Status: [Member]
Biography:Thanks and Regards
Abhisek Panda
Go Green And Save Your Future
>> Write Response - Respond to this post and get points
Related Posts

The Article will explain how to Add and remove controls from the Windows Forms during the Runtime.

In this article, I will be explain How to create a key in the registry using C#?

Here I explained the step by step procedure to deploy windows application by extending PART - 1 of this Article

This article will explain how to validate a valid IMEI number of a mobile phone through VB.NET.

In this article you will learn about the hash algorithm and types are available in .Net.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:59:07 AM