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
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.
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:
After dropping the colordialog on form we will write the code to show the colordialog on button click,
Code C#:
- private void button1_Click(object sender, EventArgs e)
- {
- colorDialog1.ShowDialog();
- richTextBox1.SelectionColor = colorDialog1.Color;
-
- }
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.
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,
After drop the fontDialog on form we will write the code to show the fontDialog on button click,
Code C#:
- private void button2_Click(object sender, EventArgs e)
- {
- fontDialog1.ShowDialog();
- richTextBox1.SelectionFont = fontDialog1.Font;
- }
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.
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.
After drop the SaveFileDialogon form we will write the code to show the SaveFileDialogon button click,
Code C#:
- private void button3_Click(object sender, EventArgs e)
- {
- saveFileDialog1.ShowDialog();
- richTextBox1.SaveFile(saveFileDialog1.FileName);
- }
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.
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.
After drop the openFileDialogon form we will write the code to show the openFileDialogon button click,
Code C#:
- private void button4_Click(object sender, EventArgs e)
- {
- if (DialogResult.OK == openFileDialog1.ShowDialog())
- {
- richTextBox1.LoadFile(openFileDialog1.FileName);
- }
- }
To read other articles on the Dialog, please click on the below references link: