Visual C# (Windows) Guide
Using Dialog Boxes

The .NET framework has a series of built-in dialog boxes that can be used in applications. All of the functionality that you need is there and it makes sense to know how to use them. Fortunately, it's pretty easy.

Start by knocking up a form that looks like the one in the screenshot.

form design

The buttons are named, btnOpen, btnSave and btnFont and the textbox is left at textBox1.

We are using the IO library here so add a using System.IO; at the top of the form.

Opening The File

private void btnOpen_Click(object sender, EventArgs e)
{
   OpenFileDialog openDialog = new OpenFileDialog();
   openDialog.Title = "Save A File";
   openDialog.Filter = "Text Files (*.txt)|*.txt|HTML Files (*.htm)|*.htm";
   if (openDialog.ShowDialog() == DialogResult.OK)
   {
      textBox1.Text = File.ReadAllText(openDialog.FileName);
   }
}

We set the Title and Filter properties before we call the function to show the dialog.

Saving The File

private void btnSave_Click(object sender, EventArgs e)
{
   SaveFileDialog saveDialog = new SaveFileDialog();
   saveDialog.Title = "Save A File";
   saveDialog.Filter = "Text Files (*.txt)|*.txt|HTML Files (*.htm)|*.htm";
   if (saveDialog.ShowDialog() == DialogResult.OK)
   {
      File.WriteAllText(saveDialog.FileName, textBox1.Text);
   }
}

Changing The Font

private void btnFont_Click(object sender, EventArgs e)
{
   FontDialog fDialog = new FontDialog();
   if (fDialog.ShowDialog() == DialogResult.OK)
   {
      textBox1.Font = fDialog.Font;
   }
}

Having A Go

  1. Design a form-based application that is based on a 9x9 sudoku grid. Each cell of the grid should be a text box. Design a file structure which will allow you to save and load sudoku grids. You can use 0 to represent an empty cell.
  2. The rich text box control allows you more control than the plain old text box. You can work with the formatting of selections of text and it has its own SaveFile method. Use the MSDN to help you use this control to make a text editing application in which you can change the colour and formatting of selected text rather than the whole box.