// Programmer: Janet Joy // Happy Birthday illustrates using menu, tool strip, status bar // and dialog boxes to select font, file and color. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace happybirthday { public partial class Form1 : Form { // These variables will be used to restore the original state. Font startFont; Color startColor; public Form1() { InitializeComponent(); // Store the current font and color. startFont = lblHappy.Font; startColor = this.BackColor; } private void mnuExit_Click(object sender, EventArgs e) { this.Close(); } private void mnuNew_Click(object sender, EventArgs e) { // Restore to values at form load. picHappy.Image = null; this.BackColor =startColor; lblHappy.Font = startFont; } private void timer1_Tick(object sender, EventArgs e) { statusTime.Text = System.DateTime.Now.ToString(); } 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); statusFile.Text = openFileDialog1.FileName; } } private void toolStripOpen_Click(object sender, EventArgs e) { mnuOpen_Click(sender, e); } private void mnuFont_Click(object sender, EventArgs e) { // show the font dialog modal. // Nothing else happens until the dialog closes. fontDialog1.ShowDialog(); // If the user clicks cancel, the font will be null. if(fontDialog1.Font != null) { // If not null change the font for lblHappy to selected font. lblHappy.Font = fontDialog1.Font; } } private void toolStripFont_Click(object sender, EventArgs e) { // Call the same mthod for the tool strip image as the menu item. mnuFont_Click(sender, e); } private void mnuColor_Click(object sender, EventArgs e) { // This is a different way to determine if the user clicked cancel. // Show the color dialog and store result (Ok or Cancel). DialogResult result = colorDialog1.ShowDialog(); // See if user pressed ok. if (result == DialogResult.OK) { // Set form background to the selected color. this.BackColor = colorDialog1.Color; } } private void toolStripColor_Click(object sender, EventArgs e) { mnuColor_Click(sender, e); } } }