1 // Student: Pat Moss 2 // Instructor: Dave Hammer 3 // CIS162AD C# 5832 - Worksheet #8 - Form #1 4 // 04/09/2006 Rev. 01a 5 6 // III. Programming Exercises: Write source code for the following: 7 8 // Create the following program: 9 // 1. Create a Form that contains three Labels that hold famous 10 // quotes of your choice. When the program starts, the 11 // background color of the Form and each Label should be 12 // black. When the user passes a mouse over a Label, change 13 // its BackColor to white, revealing the text of the quote. 14 // Turn in your source code for this program. 15 16 using System; 17 using System.Drawing; 18 using System.Collections; 19 using System.ComponentModel; 20 using System.Windows.Forms; 21 using System.Data; 22 23 namespace form1 24 { 25 /// 26 /// Summary description for Form1 form 27 /// 28 public class Form1 : System.Windows.Forms.Form 29 { 30 private System.Windows.Forms.Button btnExit; 31 private System.Windows.Forms.Button btnHelp; 32 private System.Windows.Forms.Label label1; 33 private System.Windows.Forms.Label label2; 34 private System.Windows.Forms.Label label3; 35 private System.Windows.Forms.Label label4; 36 37 /// 38 /// Required designer variable 39 /// 40 private System.ComponentModel.Container components = null; 41 42 public Form1() 43 { 44 // 45 // Required for Windows Form Designer support 46 // 47 InitializeComponent(); 48 49 // 50 // TODO: Add any constructor code after InitializeComponent call 51 // 52 } 53 54 /// 55 /// Clean up any resources being used 56 /// 57 protected override void Dispose( bool disposing ) 58 { 59 if( disposing ) 60 { 61 if (components != null) 62 { 63 components.Dispose(); 64 } 65 } 66 base.Dispose( disposing ); 67 } 68 69 #region Windows Form Designer generated code 70 /// 71 /// Required method for Designer support - do not modify 72 /// the contents of this method with the code editor 73 /// 74 private void InitializeComponent() 75 { 76 this.btnExit = new System.Windows.Forms.Button(); 77 this.btnHelp = new System.Windows.Forms.Button(); 78 this.label1 = new System.Windows.Forms.Label(); 79 this.label2 = new System.Windows.Forms.Label(); 80 this.label3 = new System.Windows.Forms.Label(); 81 this.label4 = new System.Windows.Forms.Label(); 82 this.SuspendLayout(); 83 // 84 // btnExit 85 // 86 this.btnExit.Location = new System.Drawing.Point(240, 168); 87 this.btnExit.Name = "btnExit"; 88 this.btnExit.Size = new System.Drawing.Size(80, 24); 89 this.btnExit.TabIndex = 6; 90 this.btnExit.Text = "Exit"; 91 this.btnExit.Click += new System.EventHandler(this.btnExit_Click); 92 // 93 // btnHelp 94 // 95 this.btnHelp.Location = new System.Drawing.Point(136, 168); 96 this.btnHelp.Name = "btnHelp"; 97 this.btnHelp.Size = new System.Drawing.Size(80, 24); 98 this.btnHelp.TabIndex = 14; 99 this.btnHelp.Text = "Help"; 100 this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click); 101 // 102 // label1 103 // 104 this.label1.BackColor = System.Drawing.Color.Black; 105 this.label1.Location = new System.Drawing.Point(16, 8); 106 this.label1.Name = "label1"; 107 this.label1.Size = new System.Drawing.Size(136, 72); 108 this.label1.TabIndex = 15; 109 this.label1.MouseEnter += new System.EventHandler(this.label1_MouseEnter); 110 this.label1.MouseLeave += new System.EventHandler(this.label1_MouseLeave); 111 // 112 // label2 113 // 114 this.label2.BackColor = System.Drawing.Color.Black; 115 this.label2.Location = new System.Drawing.Point(168, 83); 116 this.label2.Name = "label2"; 117 this.label2.Size = new System.Drawing.Size(136, 72); 118 this.label2.TabIndex = 16; 119 this.label2.MouseEnter += new System.EventHandler(this.label2_MouseEnter); 120 this.label2.MouseLeave += new System.EventHandler(this.label2_MouseLeave); 121 // 122 // label3 123 // 124 this.label3.BackColor = System.Drawing.Color.Black; 125 this.label3.Location = new System.Drawing.Point(320, 8); 126 this.label3.Name = "label3"; 127 this.label3.Size = new System.Drawing.Size(136, 72); 128 this.label3.TabIndex = 17; 129 this.label3.MouseEnter += new System.EventHandler(this.label3_MouseEnter); 130 this.label3.MouseLeave += new System.EventHandler(this.label3_MouseLeave); 131 // 132 // label4 133 // 134 this.label4.Location = new System.Drawing.Point(120, 200); 135 this.label4.Name = "label4"; 136 this.label4.Size = new System.Drawing.Size(224, 32); 137 this.label4.TabIndex = 18; 138 this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 139 // 140 // Form1 141 // 142 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 143 this.BackColor = System.Drawing.Color.White; 144 this.ClientSize = new System.Drawing.Size(472, 238); 145 this.Controls.Add(this.label4); 146 this.Controls.Add(this.label3); 147 this.Controls.Add(this.label2); 148 this.Controls.Add(this.label1); 149 this.Controls.Add(this.btnHelp); 150 this.Controls.Add(this.btnExit); 151 this.Name = "Form1"; 152 this.Text = "Worksheet #8 - Form #1 - by Pat Moss"; 153 this.Load += new System.EventHandler(this.Form1_Load); 154 this.ResumeLayout(false); 155 156 } 157 #endregion 158 159 /// 160 /// The main entry point for the application 161 /// 162 [STAThread] 163 static void Main() 164 { 165 Application.Run(new Form1()); 166 } 167 168 // Form load event 169 // Initialize the screen controls 170 private void Form1_Load(object sender, System.EventArgs e) 171 { 172 string str1, str2, str3; 173 str1 = "Now is the time for all good men "; 174 str1 += "to come to the aid of their country."; 175 str2 = "The world's longest journey begins "; 176 str2 += "with the first step."; 177 str3 = "He who speaks does not know. "; 178 str3 += "He who knows does not speak."; 179 180 label1.Text = str1; 181 label2.Text = str2; 182 label3.Text = str3; 183 } 184 185 // "Help" button event 186 // The user has clicked on the "Help" button 187 // Display user help information 188 private void btnHelp_Click(object sender, System.EventArgs e) 189 { 190 string helpinfo = ""; 191 helpinfo += " User Instructions:\n\n"; 192 helpinfo += "1. The form contains three hidden labels.\n"; 193 helpinfo += "2. Each label contains a 'famous quote'.\n"; 194 helpinfo += "3. As you roll the mouse over each hidden label,\n"; 195 helpinfo += " its hidden quote will be displayed.\n"; 196 MessageBox.Show(helpinfo, "Form #1 Help", 197 MessageBoxButtons.OK, MessageBoxIcon.Information, 198 MessageBoxDefaultButton.Button1); 199 } 200 201 // "Exit" button event 202 // The user has clicked on the "Exit" button 203 // so exit from this application now. 204 private void btnExit_Click(object sender, System.EventArgs e) 205 { 206 this.Close(); 207 } 208 209 private void label1_MouseEnter(object sender, System.EventArgs e) 210 { 211 label1.BackColor = Color.White; 212 label4.Text = "The mouse now over label #1."; 213 } 214 215 private void label1_MouseLeave(object sender, System.EventArgs e) 216 { 217 label1.BackColor = Color.Black; 218 label4.Text = ""; 219 } 220 221 private void label2_MouseEnter(object sender, System.EventArgs e) 222 { 223 label2.BackColor = Color.White; 224 label4.Text = "The mouse is now over label #2."; 225 } 226 227 private void label2_MouseLeave(object sender, System.EventArgs e) 228 { 229 label2.BackColor = Color.Black; 230 label4.Text = ""; 231 } 232 233 private void label3_MouseEnter(object sender, System.EventArgs e) 234 { 235 label3.BackColor = Color.White; 236 label4.Text = "The mouse is now over label #3."; 237 } 238 239 private void label3_MouseLeave(object sender, System.EventArgs e) 240 { 241 label3.BackColor = Color.Black; 242 label4.Text = ""; 243 } 244 } 245 }