Dialogs are a window that has certain specific function such as saving or opening a file, choosing a color, or printing a document.
Different dialog box available are: ColorDialog: The ColorDialog (System.Windows.Forms.ColorDialog) is used when you want
to pick different colors.
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = colorDialog1.ShowDialog();
if (result == DialogResult.OK)
{
this.BackColor = colorDialog1.Color;
}
}
FontDialog: The FontDialog control (System.Windows.Forms.FontDialog) is use for selecting different kinds of font and font-related properties.
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = fontDialog1.ShowDialog();
if (result == DialogResult.OK)
{
textBox1.Font = fontDialog1.Font;
}
}
OpenFileDialog: The OpenFileDialog control (System.Windows.Forms.OpenFileDialog) allows you to open and read file.
private void button1_Click(object sender, EventArgs e)
{
//Filter to only text files
openFileDialog1.Filter = "Text Files|*.txt";
//No initial file selected
openFileDialog1.FileName = String.Empty;
//Open file dialog and store the returned value
DialogResult result = openFileDialog1.ShowDialog();
//If Open Button was pressed
if (result == DialogResult.OK)
{
//Create a stream which points to the file
Stream fs = openFileDialog1.OpenFile();
//Create a reader using the stream
StreamReader reader = new StreamReader(fs);
//Read Contents
textBox1.Text = reader.ReadToEnd();
//Close the reader and the stream
reader.Close();
}
}
SaveFileDialog: The SaveFileDialog control (System.Windows.Forms.SaveFileDialog) allows you to save or write data to a specified file.
private void button1_Click(object sender, EventArgs e)
{
//Specify the extensions allowed
saveFileDialog1.Filter = "Text File|.txt";
//Empty the FileName text box of the dialog
saveFileDialog1.FileName = String.Empty;
//Set default extension as .txt
saveFileDialog1.DefaultExt = ".txt";
//Open the dialog and determine which button was pressed
DialogResult result = saveFileDialog1.ShowDialog();
//If the user presses the Save button
if (result == DialogResult.OK)
{
//Create a file stream using the file name
FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
//Create a writer that will write to the stream
StreamWriter writer = new StreamWriter(fs);
//Write the contents of the text box to the stream
writer.Write(textBox1.Text);
//Close the writer and the stream
writer.Close();
}
}
FolderBrowserDialog: The FolderBrowserDialog contrl (System.Windows.Forms.FolderBrowserDialog) allows you to browse for a directory in your system. The control uses a tree view to show all the folders.
Property
Description
Description
Allows you to add a descriptive text above the tree view of the FolderBrowserDialog.
RootFolder
Gets or sets the folder that the FolderBrowserDialog will consider as the root or the top level folder.
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
//Show the path using the text box
textBox1.Text = folderBrowserDialog1.SelectedPath;
//Obtain information about the path
DirectoryInfo selectedPath = new DirectoryInfo(textBox1.Text);
//Clear the list box first
listBox1.Items.Clear();
//Check if there are directories then add a label
if (selectedPath.GetDirectories().Length > 0)
listBox1.Items.Add("== Directories ==");
//Show all the directories using the ListBox control
foreach (DirectoryInfo dir in selectedPath.GetDirectories())
{
//Show only the name of the directory
listBox1.Items.Add(dir.Name);
}
//Check if there are files then add a label
if (selectedPath.GetFiles().Length > 0)
listBox1.Items.Add("== Files ==");
//Show all the directories using the ListBox control
foreach (FileInfo file in selectedPath.GetFiles())
{
listBox1.Items.Add(file.Name);
}
}
}
PageSetupDialog
PrintPreviewDialog
PrintDialog