using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace HelloWindows { static class Program { ////// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
using
directive is like import
in Java: it allows the programmer to use a type in the namespace
without having to preface the type with the namespace.
Example: System.Windows.Forms.Application
vs.
Application
Main()
is always the first method executed
Application
provides static methods and properties to manage
an application, such as methods to start and stop an application, to process
Windows messages, and properties to get information about an application
Application.Run()
begins running a standard application
message loop and makes the given Form
visible;
Run()
will not return until Form1
is closed
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace HelloWindows { public partial class Form1 : Form { public Form1() { InitializeComponent(); } } }
Form1
inherits from System.Windows.Forms.Form, a
class that represents a window or dialog box
Form1
is partial
because
part of Form1
is defined in another file (Form1.Designer.cs)
Form1
's constructor calls InitializeComponent()
which is defined in Form1.Designer.cs
namespace HelloWindows { partial class Form1 { ////// Required designer variable. /// private System.ComponentModel.IContainer components = null; ////// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code ////// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(84, 42); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(84, 13); this.label1.TabIndex = 0; this.label1.Text = "Hello, Windows!"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(284, 262); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; } }
Form1
is marked partial
because the
Form1
class is partially defined here and partially in Form1.cs
label1
at the end of the file)
InitializeComponent()
sets all the components'
properties (see the settings for label1
and the form
)
#region
is a
preprocessor directive
and with #endregion
can be used to create collapsible code
///
used to provide XML comments which VS uses
to produce documentation
label1
to say "You clicked the button!":
private void button1_Click(object sender, EventArgs e) { label1.Text = "You clicked the button!"; }
Form1.Designer.cs
defines a
System.Windows.Forms.Button object that has a Click
event. The
+= operator register's the Click
event's callback button1_Click()
:
this.button1.Click += new System.EventHandler(this.button1_Click);
button1_Click
private void button1_Click(object sender, EventArgs e) { // sender is either button1 or button2 if (btn == button1) label1.Text = "You pressed the first button!"; else label1.Text = "You pressed the second button!"; }
System.Diagnostics.Debug.WriteLine("debug message")
to display debug information in Visual Studio's Output pane