Dialog Boxes in Windows Forms Applications

First create a project for a Windows Forms application in Visual Studio.

Then open the Form1.cs GUI file and make the design with one MenuStrip, two TextBoxes and a Label and drag an openfileDialog, saveFileDialog, folderBrowserDialog, colorDialog and fontDialogs). Make one Menu as in the following:

Dialog Box

Now we move to the code.

openFileDialog Click Event

  1. private void openDialogToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     openFileDialog1.Filter = "Text file (*.txt)|*.txt|All file(*.*)|*.*";  
  4.     if (openFileDialog1.ShowDialog() == DialogResult.OK)  
  5.     {  
  6.         stropen = openFileDialog1.FileName;  
  7.         textBox1.Text = System.IO.File.ReadAllText(stropen);  
  8.     }  
  9. }  
Create Project Windows Form Application

saveFileDialog Click Event

Write text in the TextBox area and then click on the Save menu option.
  1. private void saveDialogToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     saveFileDialog1.Filter = "Text file (*.txt)|*.txt|All file(*.*)|*.*";  
  4.     if (saveFileDialog1.ShowDialog() == DialogResult.OK)  
  5.     {  
  6.         strsave = saveFileDialog1.FileName;  
  7.         System.IO.File.WriteAllText(strsave, textBox1.Text);  
  8.     }  
  9. }  
openFileDialog

fontDialog Click Event
  1. private void fontDialogToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     if (fontDialog1.ShowDialog() == DialogResult.OK)  
  4.     {  
  5.         textBox1.Font = fontDialog1.Font;  
  6.         textBox1.ForeColor = fontDialog1.Color;  
  7.   
  8.     }  
  9. }  
saveFileDialog

colorDialog Click Event
  1. private void colorDialogToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     if (colorDialog1.ShowDialog() == DialogResult.OK)  
  4.     {  
  5.         //this.BackColor = colorDialog1.Color;  
  6.         //menuStrip1.BackColor = colorDialog1.Color;  
  7.         textBox1.BackColor = colorDialog1.Color;  
  8.     }  
  9. }  
fontDialog

browserDialog Click Event
  1. private void browseDialogToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;  
  4.     if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)  
  5.     {  
  6.         textBox2.Text = folderBrowserDialog1.SelectedPath;  
  7.     }  
  8. }  
browserDialog

Thank you.


Similar Articles