Dialog Controls

• These are meant for creating dialog boxes at run time.
• A dialog box can be defined as a “Force responsive window”, which requires the user’s attention at run time. That means, without answering the dialog box, the user can’t work with the form.
• In order to display different types of dialog boxes, .NET offers several dialog box controls.

1) ColorDialog
2) FontDialog
3) FolderBrowserDialog
4) OpenFileDialog
5) SaveFileDialog
6) PrintDialog

Note: All the dialog box controls are known as “invisible controls”.

ColorDialog

Displays a dialog box, for a color selection.

API: System.Windows.Forms.ColorDialog
Naming Convension: colorDlgxxxxxx
ColorDialog
• To invoke the dialog box at run time:
colorDialog1.ShowDialog();
• Get the currently selected color in the dialog box:
colorDialog1.Color;

Demo on ColorDialog

ColorDialog
private void btnBackColor_Click(object sender, EventArgs e)
{
colorDlgBackColor.ShowDialog();
this.BackColor = colorDlgBackColor.Color;
}

FontDialog

Displays a dialog box, for a font selection (with font name, bold, italic, regular, underline, font size options).
API: System.Windows.Forms.FontDialog
Naming Convension: fontDlgxxxxxx
ColorDialog

• To invoke the dialog box at run time:
fontDialog1.ShowDialog();
• Get the currently selected color in the dialog box:
fontDialog1.Font;
ColorDialog
Demo on FontDialog
private void btnFont_Click(object sender, EventArgs e)
{
fontDialogMyText.ShowDialog();
txtMyText.Font = fontDialogMyText.Font;
}

FolderBrowserDialog

Displays a dialog box, for a folder selection.

API: System.Windows.Forms.FolderBrowserDialog
Naming Convension: folderBrowserDlgxxxxxx
ColorDialog

• To invoke the dialog box at run time:
folderBrowserDialog1.ShowDialog();
• Get the currently selected folder’s full path in the dialog box:
folderBrowserDialog1.SelectedPath;

Demo on FolderBrowserDialog

ColorDialog
using System.IO;

private void btnBrowseFolder_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
string selectedfolder = folderBrowserDialog1.SelectedPath;
lblSelectedFolder.Text = "Selected Folder: " + selectedfolder;
DirectoryInfo d = new DirectoryInfo(selectedfolder);
if (d.Exists)
{
DirectoryInfo[] subdirs = d.GetDirectories();
FileInfo[] files = d.GetFiles();
lstSubFolders.Items.Clear();
lstFiles.Items.Clear();
foreach (DirectoryInfo dobj in subdirs)
lstSubFolders.Items.Add(dobj.Name);
foreach (FileInfo fobj in files)
lstFiles.Items.Add(fobj.Name);
}
else
MessageBox.Show("The seleced folder not found.");
}

OpenFileDialog

Displays a dialog box, for a file selection for opening of a file.

API: System.Windows.Forms.OpenFileDialog
Naming Convension: openFileDlgxxxxxx
ColorDialog

• To invoke the dialog box at run time:
openFileDialog1.ShowDialog();
• Get the currently selected file path:
openFileDialog1.FileName;
Demo on OpenFileDialog

ColorDialog
using System.IO;

private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Reset();
openFileDialog1.ShowDialog();
txtFileName.Text = openFileDialog1.FileName;
}

private void btnOpen_Click(object sender, EventArgs e)
{
string filename = txtFileName.Text;
FileInfo fobj = new FileInfo(filename);

if (fobj.Exists)
{
StreamReader sr = new StreamReader(filename);
string content = sr.ReadToEnd();
txtContent.Text = content;
sr.Close();
}
else
MessageBox.Show("File not found.");
}

SaveFileDialog

Displays a dialog box, for a file selection for saving a file.

API: System.Windows.Forms.SaveFileDialog
Naming Convension: saveFileDlgxxxxxx
ColorDialog

• To invoke the dialog box at run time:
saveFileDialog1.ShowDialog();
• Get the currently selected file path:
saveFileDialog1.FileName;

Demo on SaveFileDialog
ColorDialog

using System.IO;
private void btnBrowse_Click(object sender, EventArgs e)
{
saveFileDialog1.Reset();
saveFileDialog1.ShowDialog();
txtFileName.Text = saveFileDialog1.FileName;
}

private void btnSave_Click(object sender, EventArgs e)
{
string filename = txtFileName.Text;
if (filename != "")
{
FileInfo fobj = new FileInfo(filename);
if (!fobj.Exists)
{
StreamWriter sw = new StreamWriter(filename);
string content = txtContent.Text;
sw.Write(content);
sw.Close();
MessageBox.Show("Successfully Saved.");
}
else
MessageBox.Show("File already exists!");
}
else
MessageBox.Show("Select any file name first.");
}

Printing in .NET

.NET supports to print any type of documents through the printer.
API: System.Drawing.Printing.PrintDocument
This class object represents a document (any type of file), that is ready to print.

Implementation:
• Import the namespace
using System.Drawing.Printing;
• Construct the object
PrintDocument obj = new PrintDocument();
• Assign the document name, that is to be printed.
obj.DocumentName = “file pat h to be printed”;
• Start Printing
obj.Print();
Demo on Printing
ColorDialog

using System.Drawing.Printing;
using System.IO;
private void btnPrint_Click(object sender, EventArgs e)
{
string filename = txtFileName.Text;
if (filename != "")
{
FileInfo fobj = new FileInfo(filename);
if (fobj.Exists)
{
PrintDocument doc = new PrintDocument();
doc.DocumentName = filename;
doc.Print();
MessageBox.Show("Printing Started...");
}
else
MessageBox.Show("File not found.");
}
else
MessageBox.Show("Enter file name");
}

Note: In the above application, there no an option to select printing options like no. of copies, name of the printer, paper range and paper orientation etc.

PrintDialog

Displays a dialog box, for printing preferences selection like no. of copies, name of the printer, paper range and paper orientation etc.
API: System.Windows.Forms.PrintDialog
Naming Convension: printDlgxxxxxx

• To invoke the dialog box at run time:
printDialog1.ShowDialog();
• Get the selected printing settings:
printFileDialog1.PrinterSettings;

Demo on PrintDialog
ColorDialog

using System.Drawing.Printing;
using System.IO;
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Reset();
openFileDialog1.ShowDialog();
txtFileName.Text = openFileDialog1.FileName;
}

private void btnPrint_Click(object sender, EventArgs e)
{
string filename = txtFileName.Text;
if (filename != "")
{
FileInfo fobj = new FileInfo(filename);
if (fobj.Exists)
{
printDialog1.Reset();
printDialog1.ShowDialog();
PrintDocument doc = new PrintDocument();
doc.PrinterSettings = printDialog1.PrinterSettings;
doc.DocumentName = filename;
doc.Print();
MessageBox.Show("Printing Started...");
}
else
MessageBox.Show("File not found.");
}
else
MessageBox.Show("Enter file name");
}