Csharp/C Sharp/Event/System Event
Get Event sender in event handler
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class TwoButtons: Form {
readonly Button btnLarger, btnSmaller; readonly int cxBtn, cyBtn, dxBtn; public static void Main() { Application.Run(new TwoButtons()); } public TwoButtons() { ResizeRedraw = true; cxBtn = 200; cyBtn = 100; dxBtn = 12; btnLarger = new Button(); btnLarger.Parent = this; btnLarger.Text = "&Larger"; btnLarger.Size = new Size(cxBtn, cyBtn); btnLarger.Click += new EventHandler(ButtonOnClick); btnSmaller = new Button(); btnSmaller.Parent = this; btnSmaller.Text = "&Smaller"; btnSmaller.Size = new Size(cxBtn, cyBtn); btnSmaller.Click += new EventHandler(ButtonOnClick); OnResize(EventArgs.Empty); } protected override void OnResize(EventArgs ea) { base.OnResize(ea); btnLarger.Location = new Point(ClientSize.Width / 2 - cxBtn - dxBtn / 2, (ClientSize.Height - cyBtn) / 2); btnSmaller.Location = new Point(ClientSize.Width / 2 + dxBtn / 2, (ClientSize.Height - cyBtn) / 2); } void ButtonOnClick(object obj, EventArgs ea) { Button btn = (Button) obj; if (btn == btnLarger) { Left -= (int)(0.1 * Width); Top -= (int)(0.1 * Height); Width += (int)(0.1 * Width); Height += (int)(0.1 * Height); } else { Left += (int)(Width / 22f); Top += (int)(Height / 22f); Width -= (int)(Width / 11f); Height -= (int)(Height / 11f); } }
}
</source>
System Events: User Preference Changed
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32; public class Form1 : Form {
private System.Windows.Forms.Label label2; public Form1() { this.Font = SystemFonts.IconTitleFont; this.AutoScaleDimensions = new System.Drawing.SizeF(6.0F, 13.0F); InitializeComponent(); SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged); } private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) { if (e.Category == UserPreferenceCategory.Window) { this.Font = SystemFonts.IconTitleFont; } } private void InitializeComponent() { this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label2 // this.label2.Location = new System.Drawing.Point(12, 9); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(220, 55); this.label2.TabIndex = 2; this.label2.Text = "Try changing the Small Fonts/Large Fonts setting for th" + "e computer."; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(244, 138); this.Controls.Add(this.label2); this.Text = "Form1"; this.ResumeLayout(false); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
}
</source>