AllTheTimeWorld.com

csharp dialogsAdds the Open dialog to select a picture

Adds the Open dialog to select a picture

Includes setting the filter to select only picture types.

Adds the Open dialog to select a picture

Includes setting the filter to select only picture types.


Text of video

The code for the OpenFileDialog is slightly more complicated. Before showing the dialog, we must set the filter. The filter consists of a description of the file type followed by the pipe | (the straight bar that is above the backslash) then the patern for the file type, then another pipe, etc.for as many file types as we want to allow.

The filter "Bitmaps|*.bmp|All files|*.*" lets the user select either Bitmaps or all files. The pattern for bitmaps is *.bmp.

Write the code as shown below:

private void mnuOpen_Click(object sender, EventArgs e)
{
  // The filter determines which file types can be selected.
  openFileDialog1.Filter = "All Pictures|*.bmp;*.gif;*.png;*.jpg|JPG|*.jpg|Bitmaps|*.bmp|GIFS|*.gif|PNG|*.png";
  this.openFileDialog1.ShowDialog();
  // If the user selects "Cancel" filename will be "".
  if(openFileDialog1.FileName!="")
  {
      picHappy.Load(openFileDialog1.FileName);
      // Display the filename in the status bar.
      statusFile.Text = openFileDialog1.FileName;
   }
}

private void toolStripOpen_Click(object sender, EventArgs e)
{
   mnuOpen_Click(sender, e);
}

Experiment: Add an text box to let the user change the greeting.

Experiment: After the font changes, or the greeting changes, or the size of the form changes, center the greeting.
After the picture changes, or when the form changes size, center the picture.