Dialog Control in C# 6

This article is based on the original article that was previously written using an older version of Visual Studio. You can find the original article on the given below link:

Let's suppose that we have a windows form with RitchTextBox and four buttons (Color, Font, SaveFile, OpenFile), with four different Dialogs (ColorDialog, FontDialog, SaveDialog, OpenDialog,).

Example as given below: Figure 1

form

ColorDialog:

In C#, setting colors of controls is a piece of cake. The ColorDialog class does everything for you. The ColorDialog is derived from common dialog. ShowDialog displays color dialog and let you pick a color. Color property of the ColorDialog returns the current selected color.

form

Design-time:

To create a ColorDialogcontrol at design-time, you simply drag and drop a ColorDialogcontrol from Toolbox to a Form in Visual Studio. After you drag and drop a ColorDialog on a Form, the ColorDialog looks like:

color

After dropping the colordialog on form we will write the code to show the colordialog on button click,

Code C#:

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     colorDialog1.ShowDialog();  
  4.     richTextBox1.SelectionColor = colorDialog1.Color;  
  5.   
  6. }  
FontDialog:

A FontDialog control is used to select a font from available fonts installed on a system. A typical Font Dialog looks like Figure 3 where you can see there is a list of fonts, styles, size and other options. Please note a FontDialog may have different fonts on different system depending on what fonts are installed on a system.

We can create a FontDialog control using a Forms designer at design-time or using the FontDialog class in code at run-time (also known as dynamically). Unlike other Windows Forms controls, a FontDialog does not have and does not need visual properties like others. You use a FontDialog to list all the fonts and select one of them and usually apply selected fonts on controls or some contents.

font

Design-time:

To create a FontDialog control at design-time, you simply drag and drop a FontDialog control from Toolbox to a Form in Visual Studio. After you drag and drop a FontDialog on a Form, the FontDialog looks like,

dialog

After drop the fontDialog on form we will write the code to show the fontDialog on button click,

Code C#:
  1. private void button2_Click(object sender, EventArgs e)   
  2. {  
  3.     fontDialog1.ShowDialog();  
  4.     richTextBox1.SelectionFont = fontDialog1.Font;  
  5. }  
SaveFileDialog:

A SaveFileDialog control is used to save a file using Windows Save File Dialog. A typical Save File Dialog looks like Figure 4 where you can see Windows Explorer like features to navigate through folders and save a file in a folder.

save

Creating a SaveFileDialog:

We can create a SaveFileDialog control using a Forms designer at design-time or using the SaveFileDialog class in code at run-time (also known as dynamically). Unlike other Windows Forms controls, a SaveFileDialog does not have and does not need visual properties like others.

dialog

After drop the SaveFileDialogon form we will write the code to show the SaveFileDialogon button click,

Code C#:
  1. private void button3_Click(object sender, EventArgs e)  
  2. {  
  3.     saveFileDialog1.ShowDialog();  
  4.     richTextBox1.SaveFile(saveFileDialog1.FileName);  
  5. }  
OpenFileDialog:

An OpenFileDialog control is used to browse and select a file on a computer. A typical Open File Dialog looks like Figure 5 where you can see Windows Explorer like features to navigate through folders and select a file.

open

Creating aOpenFileDialog:

We can create an OpenFileDialog control using a Forms designer at design-time or using the OpenFileDialog class in code at run-time (also known as dynamically). Unlike other Windows Forms controls, an OpenFileDialog does not have and not need visual properties like others.

dialog

After drop the openFileDialogon form we will write the code to show the openFileDialogon button click,

Code C#:
  1. private void button4_Click(object sender, EventArgs e)   
  2. {  
  3.     if (DialogResult.OK == openFileDialog1.ShowDialog())  
  4.     {  
  5.         richTextBox1.LoadFile(openFileDialog1.FileName);  
  6.     }  
To read other articles on the Dialog, please click on the below references link:

Up Next
    Ebook Download
    View all
    Learn
    View all