Zebra0.com

csharp forms


Text of video

In this video we create a new form to show each picture selected and show it nonmodal.
This allows us to have many froms open at once.

This is the code for FormPicture:

private void FormPicture_Resize(object sender, EventArgs e)
{
    // Make the picture box the size of the form, minus the width of the edges.
    pictureBox1.Width = this.Width - 20;
    pictureBox1.Height = this.Height - 40;
}
public PictureBox GetPictureBox() {
    // Make the picture box available to the main form.
    return pictureBox1;
}

This is the code for Form1:

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 != "")
  {
     FormPicture frm = new FormPicture();
     PictureBox pic = frm.GetPictureBox();
     // This lets us directly access the picture box on frm.
     frm.Text = openFileDialog1.FileName;
     pic.Load(openFileDialog1.FileName);
     frm.Width = pic.Width;
     frm.Height = pic.Height;
     frm.Show();
   }
}

To do: Finish the menu, add exit, add a tool strip to match the menu and add an about box.

End of lesson, Next lesson: