Csharp/C Sharp by API/System.Windows.Forms/Form — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Содержание
- 1 extends Form
- 2 Form.AcceptButton
- 3 Form.Activated
- 4 Form.ActiveForm.Location
- 5 Form.AddOwnedForm
- 6 Form.AutoScaleBaseSize
- 7 Form.AutoScroll
- 8 Form.AutoScrollMinSize
- 9 Form.BackColor
- 10 Form.CancelButton
- 11 Form.CenterToScreen()
- 12 Form.ClientRectangle
- 13 Form.ClientSize
- 14 Form.Closed
- 15 Form.Closing
- 16 Form.ContextMenu
- 17 Form.ControlBox
- 18 Form.Controls
- 19 Form.Cursor
- 20 Form.DesktopBounds
- 21 Form.DialogResult
- 22 Form.DisplayRectangle
- 23 Form.Dispose
- 24 Form.ForeColor
- 25 Form.FormBorderStyle
- 26 Form.GotFocus
- 27 Form.Height
- 28 Form.Hide()
- 29 Form.Invalidate
- 30 Form.IsMdiContainer
- 31 Form.KeyPress
- 32 Form.KeyPreview
- 33 Form.KeyUp
- 34 Form.Load
- 35 Form.Location
- 36 Form.MaximizeBox
- 37 Form.MaximumSize
- 38 Form.MdiParent
- 39 Form.Menu
- 40 Form.MenuComplete
- 41 Form.MinimizeBox
- 42 Form.MinimumSize
- 43 Form.MouseDown
- 44 Form.MouseLeave(All other events)
- 45 Form.MouseUp
- 46 Form.OnDragDrop
- 47 Form.OnDragOver
- 48 Form.OnInputLanguageChanged
- 49 Form.OnKeyDown
- 50 Form.OnKeyPress
- 51 Form.OnMouseEnter
- 52 Form.OnMouseHover
- 53 Form.OnMouseLeave
- 54 Form.OnMouseMove
- 55 Form.OnMouseWheel
- 56 Form.OnPaint(PaintEventArgs e)
- 57 Form.OnResize
- 58 Form.Opacity
- 59 Form.Paint
- 60 Form.Region
- 61 Form.Resize
- 62 Form.ResizeRedraw
- 63 Form.SetStyle
- 64 Form.Show
- 65 Form.ShowInTaskbar
- 66 Form.Size
- 67 Form.SuspendLayout
- 68 Form.Text
- 69 Form.Top
- 70 Form.TransparencyKey
- 71 Form.Visible
- 72 Form.Width
- 73 new Form()
extends Form
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class RendereringOrigin : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public RendereringOrigin()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(408, 273);
this.Name = "RendereringOrigin";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "GDI+ Coordinate";
this.Resize += new System.EventHandler(this.OnResize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
}
[STAThread]
static void Main()
{
Application.Run(new RendereringOrigin());
}
protected void OnPaint (object sender, System.Windows.Forms.PaintEventArgs e)
{
GraphicsUnit gUnit = GraphicsUnit.Pixel;
Point renderingOrgPt = new Point(0,0);
renderingOrgPt.X = 100;
renderingOrgPt.Y = 100;
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PageUnit = gUnit;
g.TranslateTransform(renderingOrgPt.X,renderingOrgPt.Y);
g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100);
this.Text = string.Format("PageUnit: {0}, Origin: {1}", gUnit, renderingOrgPt.ToString());
}
protected void OnResize (object sender, System.EventArgs e)
{
Invalidate();
}
}
Form.AcceptButton
using System;
using System.Drawing;
using System.Windows.Forms;
public class EnterPrice : Form {
private Button enter = new Button();
private Label answer = new Label();
private TextBox text = new TextBox( );
public EnterPrice( ) {
enter.Text = "Enter Price";
text.Text = "";
answer.Text = "";
Size = new Size(300,200);
answer.Size = new Size(200,50);
enter.Location = new Point(30 + enter.Width, 30);
text.Location = new Point (40 + enter.Width + enter.Width, 30);
answer.Location = new Point(20, 60);
AcceptButton = enter;
Controls.Add(text);
Controls.Add(answer);
Controls.Add(enter);
enter.Click += new EventHandler(Enter_Click);
}
protected void Enter_Click(Object sender, EventArgs e) {
try{
Console.WriteLine(Double.Parse(text.Text));
}catch(Exception){
}
text.Text = "";
text.Focus();
}
static void Main() {
Application.Run(new EnterPrice());
}
}
Form.Activated
using System;
using System.Windows.Forms;
using System.ruponentModel;
public class MainWindow : Form {
private string lifeTimeInfo;
public MainWindow() {
this.Closing += new CancelEventHandler(MainForm_Closing);
this.Load += new EventHandler(MainForm_Load);
this.Closed += new EventHandler(MainForm_Closed);
this.Activated += new EventHandler(MainForm_Activated);
this.Deactivate += new EventHandler(MainForm_Deactivate);
}
protected void MainForm_Closing(object sender, CancelEventArgs e) {
DialogResult dr = MessageBox.Show("Do you REALLY want to close this app?",
"Closing event!", MessageBoxButtons.YesNo);
if (dr == DialogResult.No)
e.Cancel = true;
else
e.Cancel = false;
}
protected void MainForm_Load(object sender, System.EventArgs e) {
lifeTimeInfo += "Load event\n";
}
protected void MainForm_Activated(object sender, System.EventArgs e) {
lifeTimeInfo += "Activate event\n";
}
protected void MainForm_Deactivate(object sender, System.EventArgs e) {
lifeTimeInfo += "Deactivate event\n";
}
protected void MainForm_Closed(object sender, System.EventArgs e) {
lifeTimeInfo += "Closed event\n";
MessageBox.Show(lifeTimeInfo);
}
public static void Main(string[] args) {
Application.Run(new MainWindow());
}
}
Form.ActiveForm.Location
using System;
using System.Drawing;
using System.Windows.Forms;
class BetterDialog: Form
{
public static void Main()
{
Application.Run(new BetterDialog());
}
public BetterDialog()
{
Menu = new MainMenu();
Menu.MenuItems.Add("&Dialog!", new EventHandler(MenuOnClick));
}
void MenuOnClick(object obj, EventArgs ea)
{
BetterDialogBox dlg = new BetterDialogBox();
DialogResult dr = dlg.ShowDialog();
Console.WriteLine(dr);
}
}
class BetterDialogBox: Form
{
public BetterDialogBox()
{
Text = "Better Dialog Box";
FormBorderStyle = FormBorderStyle.FixedDialog;
ControlBox = false;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
Location = ActiveForm.Location +
SystemInformation.CaptionButtonSize +
SystemInformation.FrameBorderSize;
Button btn = new Button();
btn.Parent = this;
btn.Text = "OK";
btn.Location = new Point(50, 50);
btn.Size = new Size (10 * Font.Height, 2 * Font.Height);
btn.DialogResult = DialogResult.OK;
AcceptButton = btn;
btn = new Button();
btn.Parent = this;
btn.Text = "Cancel";
btn.Location = new Point(50, 100);
btn.Size = new Size (10 * Font.Height, 2 * Font.Height);
btn.DialogResult = DialogResult.Cancel;
CancelButton = btn;
}
}
Form.AddOwnedForm
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class ChildForm : Form
{
public System.Windows.Forms.Label lblState;
public ChildForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.lblState = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lblState
//
this.lblState.Location = new System.Drawing.Point(26, 24);
this.lblState.Name = "lblState";
this.lblState.Size = new System.Drawing.Size(184, 56);
this.lblState.TabIndex = 2;
//
// ChildForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(236, 104);
this.Controls.Add(this.lblState);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "ChildForm";
this.Text = "ChildForm";
this.ResumeLayout(false);
}
}
public class ParentForm : Form{
private System.Windows.Forms.Button cmdReleaseOwnership;
private System.Windows.Forms.Button cmdAddOwnership;
public ParentForm()
{
InitializeComponent();
frmOwned.Show();
}
private ChildForm frmOwned = new ChildForm();
private void cmdAddOwnership_Click(object sender, System.EventArgs e)
{
this.AddOwnedForm(frmOwned);
frmOwned.lblState.Text = "Owned. Minimize me to see the difference.";
}
private void cmdReleaseOwnership_Click(object sender, System.EventArgs e)
{
this.RemoveOwnedForm(frmOwned);
frmOwned.lblState.Text = "Not owned! Minimize me to see the difference.";
}
private void InitializeComponent()
{
this.cmdReleaseOwnership = new System.Windows.Forms.Button();
this.cmdAddOwnership = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cmdReleaseOwnership
//
this.cmdReleaseOwnership.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cmdReleaseOwnership.Location = new System.Drawing.Point(150, 197);
this.cmdReleaseOwnership.Name = "cmdReleaseOwnership";
this.cmdReleaseOwnership.Size = new System.Drawing.Size(128, 32);
this.cmdReleaseOwnership.TabIndex = 6;
this.cmdReleaseOwnership.Text = "Remove Ownership";
this.cmdReleaseOwnership.Click += new System.EventHandler(this.cmdReleaseOwnership_Click);
//
// cmdAddOwnership
//
this.cmdAddOwnership.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cmdAddOwnership.Location = new System.Drawing.Point(14, 197);
this.cmdAddOwnership.Name = "cmdAddOwnership";
this.cmdAddOwnership.Size = new System.Drawing.Size(120, 32);
this.cmdAddOwnership.TabIndex = 5;
this.cmdAddOwnership.Text = "Set Ownership";
this.cmdAddOwnership.Click += new System.EventHandler(this.cmdAddOwnership_Click);
//
// ParentForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.cmdReleaseOwnership);
this.Controls.Add(this.cmdAddOwnership);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "ParentForm";
this.Text = "Owner";
// this.Load += new System.EventHandler(this.ParentForm_Load);
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ParentForm());
}
}
Form.AutoScaleBaseSize
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class SetFormCaption : System.Windows.Forms.Form
{
private System.ruponentModel.Container components;
public SetFormCaption()
{
InitializeComponent();
Text = "www.nfex.ru";
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "DataGridExample";
this.Resize += new System.EventHandler(this.SetFormCaption_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.SetFormCaption_Paint);
}
[STAThread]
static void Main()
{
Application.Run(new SetFormCaption());
}
private void SetFormCaption_Resize(object sender, System.EventArgs e)
{
Invalidate();
Console.WriteLine("Resize");
}
private void SetFormCaption_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("Text",
new Font("Times New Roman", 20),
new SolidBrush(Color.Black),
this.DisplayRectangle);
}
}
Form.AutoScroll
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.ruponentModel.Container components;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F);
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Size = new System.Drawing.Size(376, 224);
this.label1.TabIndex = 0;
this.label1.Text = "Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.AutoScroll = true;
this.AutoScrollMinSize = new System.Drawing.Size(300, 300);
this.ClientSize = new System.Drawing.Size(403, 256);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.label1});
this.Text = "Form1";
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
Form.AutoScrollMinSize
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class ScrollForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.ruponentModel.Container components = null;
public ScrollForm()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F);
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(376, 224);
this.label1.TabIndex = 0;
this.label1.Text = "Scrollbars of course!";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.AutoScroll = true;
this.AutoScrollMinSize = new System.Drawing.Size(300, 300);
this.ClientSize = new System.Drawing.Size(319, 136);
this.Controls.Add(this.label1);
this.Name = "ScrollForm";
this.Text = "ScrollForm";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new ScrollForm());
}
}
Form.BackColor
using System;
using System.Drawing;
using System.Windows.Forms;
class PaintHello
{
public static void Main()
{
Form form = new Form();
form.Text = "Paint Hello";
form.BackColor = Color.White;
form.Paint += new PaintEventHandler(MyPaintHandler);
Application.Run(form);
}
static void MyPaintHandler(object objSender, PaintEventArgs pea)
{
Form form = (Form)objSender;
Graphics graphics = pea.Graphics;
graphics.DrawString("Hello, world!", form.Font, Brushes.Black, 0, 0);
}
}
Form.CancelButton
using System;
using System.Drawing;
using System.Windows.Forms;
class AboutDialogBox: Form
{
public static void Main()
{
AboutDialogBox dlg = new AboutDialogBox();
dlg.ShowDialog();
}
public AboutDialogBox()
{
Text = "About AboutBox";
StartPosition = FormStartPosition.CenterParent;
FormBorderStyle = FormBorderStyle.FixedDialog;
ControlBox = false;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
Label label1 = new Label();
label1.Parent = this;
label1.Text = " AboutBox Version 1.0 ";
label1.Font = new Font(FontFamily.GenericSerif, 24,
FontStyle.Italic);
label1.AutoSize = true;
label1.TextAlign = ContentAlignment.MiddleCenter;
Icon icon = new Icon(GetType(), "AboutBox.ico");
PictureBox picbox = new PictureBox();
picbox.Parent = this;
picbox.Image = icon.ToBitmap();
picbox.SizeMode = PictureBoxSizeMode.AutoSize;
picbox.Location = new Point(label1.Font.Height / 2,
label1.Font.Height / 2);
label1.Location = new Point(picbox.Right,label1.Font.Height / 2);
int iClientWidth = label1.Right;
Label label2 = new Label();
label2.Parent = this;
label2.Text = "\x00A9 ";
label2.Font = new Font(FontFamily.GenericSerif, 16);
label2.Location = new Point(0, label1.Bottom +
label2.Font.Height);
label2.Size = new Size(iClientWidth, label2.Font.Height);
label2.TextAlign = ContentAlignment.MiddleCenter;
Button button = new Button();
button.Parent = this;
button.Text = "OK";
button.Size = new Size(4 * button.Font.Height,
2 * button.Font.Height);
button.Location = new Point((iClientWidth - button.Size.Width) / 2,
label2.Bottom + 2 * button.Font.Height);
button.DialogResult = DialogResult.OK;
CancelButton = button;
AcceptButton = button;
ClientSize = new Size(iClientWidth,
button.Bottom + 2 * button.Font.Height);
}
}
Form.CenterToScreen()
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
private Button myButton;
public Form1()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Closing+=new CancelEventHandler(Form_Closing);
myButton = new Button();
myButton.Text = "www.nfex.ru";
myButton.Location = new System.Drawing.Point(64, 32);
myButton.Size = new System.Drawing.Size(150, 50);
Controls.Add(myButton);
CenterToScreen();
}
public void Form_Closing(object sender,CancelEventArgs cArgs) {
if(sender==this){
MessageBox.Show("Form Closing Event....");
if(sender!=this) {
cArgs.Cancel=true;
}
}
}
static void Main()
{
Application.Run(new Form1());
}
}
Form.ClientRectangle
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Pen p = new Pen(Color.Black, 10);
p.StartCap = LineCap.Round;
p.EndCap = LineCap.ArrowAnchor;
g.DrawLine(p, 30, 30, 80, 30);
p.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
Form.ClientSize
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
class PrintableForm: Form
{
public static void Main()
{
Application.Run(new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Point[] apt = {new Point(0, 0),
new Point(cx - 1, 0),
new Point(cx - 1, cy - 1),
new Point(0, cy - 1),
new Point(0, 0)};
grfx.DrawLines(new Pen(clr), apt);
}
}
Form.Closed
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormLifeTimeEvent : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public FormLifeTimeEvent()
{
InitializeComponent();
this.Closing += new System.ruponentModel.CancelEventHandler(this.FormLifeTimeEvent_Closing);
this.Load += new System.EventHandler(this.FormLifeTimeEvent_Load);
this.Closed += new System.EventHandler(this.FormLifeTimeEvent_Closed);
this.Activated += new System.EventHandler(this.FormLifeTimeEvent_Activated);
this.Deactivate += new System.EventHandler(this.FormLifeTimeEvent_Deactivate);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(280, 177);
}
[STAThread]
static void Main()
{
Application.Run(new FormLifeTimeEvent());
}
private void FormLifeTimeEvent_Closing(object sender, System.ruponentModel.CancelEventArgs e)
{
Console.WriteLine("Closing event");
}
private void FormLifeTimeEvent_Load(object sender, System.EventArgs e) {
Console.WriteLine("Load event");
}
private void FormLifeTimeEvent_Activated(object sender, System.EventArgs e) {
Console.WriteLine("Activate event");
}
private void FormLifeTimeEvent_Deactivate(object sender, System.EventArgs e) {
Console.WriteLine("Deactivate event");
}
private void FormLifeTimeEvent_Closed(object sender, System.EventArgs e) {
Console.WriteLine("Closed event");
}
}
Form.Closing
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormClosingEventCancle : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public FormClosingEventCancle()
{
InitializeComponent();
this.Closing += new System.ruponentModel.CancelEventHandler(this.FormClosingEventCancle_Closing);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(280, 177);
}
[STAThread]
static void Main()
{
Application.Run(new FormClosingEventCancle());
}
private void FormClosingEventCancle_Closing(object sender, System.ruponentModel.CancelEventArgs e)
{
DialogResult dr = MessageBox.Show("Do you REALLY want to close this app?",
"Closing event!", MessageBoxButtons.YesNo);
if(dr == DialogResult.No)
e.Cancel = true;
else
e.Cancel = false;
}
}
Form.ContextMenu
using System;
using System.Drawing;
using System.Windows.Forms;
class ContextMenuDemo: Form
{
MenuItem miColor;
public static void Main()
{
Application.Run(new ContextMenuDemo());
}
public ContextMenuDemo()
{
EventHandler eh = new EventHandler(MenuColorOnClick);
MenuItem[] ami = { new MenuItem("Black", eh),
new MenuItem("Blue", eh),
new MenuItem("Green", eh),
new MenuItem("White", eh) };
foreach (MenuItem mi in ami)
mi.RadioCheck = true;
miColor = ami[3];
miColor.Checked = true;
BackColor = Color.FromName(miColor.Text);
ContextMenu = new ContextMenu(ami);
}
void MenuColorOnClick(object obj, EventArgs ea)
{
miColor.Checked = false;
miColor = (MenuItem) obj;
miColor.Checked = true;
BackColor = Color.FromName(miColor.Text);
}
}
Form.ControlBox
using System;
using System.Drawing;
using System.Windows.Forms;
class BetterDialog: Form
{
public static void Main()
{
Application.Run(new BetterDialog());
}
public BetterDialog()
{
Menu = new MainMenu();
Menu.MenuItems.Add("&Dialog!", new EventHandler(MenuOnClick));
}
void MenuOnClick(object obj, EventArgs ea)
{
BetterDialogBox dlg = new BetterDialogBox();
DialogResult dr = dlg.ShowDialog();
Console.WriteLine(dr);
}
}
class BetterDialogBox: Form
{
public BetterDialogBox()
{
Text = "Better Dialog Box";
FormBorderStyle = FormBorderStyle.FixedDialog;
ControlBox = false;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
Location = ActiveForm.Location +
SystemInformation.CaptionButtonSize +
SystemInformation.FrameBorderSize;
Button btn = new Button();
btn.Parent = this;
btn.Text = "OK";
btn.Location = new Point(50, 50);
btn.Size = new Size (10 * Font.Height, 2 * Font.Height);
btn.DialogResult = DialogResult.OK;
AcceptButton = btn;
btn = new Button();
btn.Parent = this;
btn.Text = "Cancel";
btn.Location = new Point(50, 100);
btn.Size = new Size (10 * Font.Height, 2 * Font.Height);
btn.DialogResult = DialogResult.Cancel;
CancelButton = btn;
}
}
Form.Controls
using System;
using System.Windows.Forms;
using System.Drawing;
class FormExit : Form {
Button StopButton;
public FormExit() {
Text = "Adding a Stop Button";
StopButton = new Button();
StopButton.Text = "Stop";
StopButton.Location = new Point(100, 100);
StopButton.Click += StopButtonClick;
Controls.Add(StopButton);
}
[STAThread]
public static void Main() {
FormExit skel = new FormExit();
Application.Run(skel);
}
protected void StopButtonClick(object who, EventArgs e) {
Application.Exit();
}
}
Form.Cursor
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormCursorSetting : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public FormCursorSetting()
{
InitializeComponent();
Cursor = Cursors.WaitCursor;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Form1";
}
[STAThread]
static void Main()
{
Application.Run(new FormCursorSetting());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("String...", new Font("Times New Roman", 20), new SolidBrush(Color.Black), 40, 10);
}
}
Form.DesktopBounds
using System;
using System.Drawing;
using System.Windows.Forms;
class FormSize: Form
{
public static void Main()
{
Application.Run(new FormSize());
}
public FormSize()
{
BackColor = Color.White;
}
protected override void OnMove(EventArgs ea)
{
Invalidate();
}
protected override void OnResize(EventArgs ea)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics graphics = pea.Graphics;
string str = "Location: " + Location + "\n" +
"Size: " + Size + "\n" +
"Bounds: " + Bounds + "\n" +
"Width: " + Width + "\n" +
"Height: " + Height + "\n" +
"Left: " + Left + "\n" +
"Top: " + Top + "\n" +
"Right: " + Right + "\n" +
"Bottom: " + Bottom + "\n\n" +
"DesktopLocation: " + DesktopLocation + "\n" +
"DesktopBounds: " + DesktopBounds + "\n\n" +
"ClientSize: " + ClientSize + "\n" +
"ClientRectangle: " + ClientRectangle;
graphics.DrawString(str, Font, Brushes.Black, 0, 0);
}
}
Form.DialogResult
using System;
using System.Drawing;
using System.Windows.Forms;
class SimpleDialog: Form
{
public static void Main()
{
Application.Run(new SimpleDialog());
}
public SimpleDialog()
{
Menu = new MainMenu();
Menu.MenuItems.Add("&Dialog!", new EventHandler(MenuOnClick));
}
void MenuOnClick(object obj, EventArgs ea)
{
SimpleDialogBox dlg = new SimpleDialogBox();
dlg.ShowDialog();
Console.WriteLine(dlg.DialogResult);
}
}
class SimpleDialogBox: Form
{
public SimpleDialogBox()
{
Text = "Simple Dialog Box";
FormBorderStyle = FormBorderStyle.FixedDialog;
ControlBox = false;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
Button btn = new Button();
btn.Parent = this;
btn.Text = "OK";
btn.Location = new Point(50, 50);
btn.Size = new Size (10 * Font.Height, 2 * Font.Height);
btn.Click += new EventHandler(ButtonOkOnClick);
btn = new Button();
btn.Parent = this;
btn.Text = "Cancel";
btn.Location = new Point(50, 100);
btn.Size = new Size (10 * Font.Height, 2 * Font.Height);
btn.Click += new EventHandler(ButtonCancelOnClick);
}
void ButtonOkOnClick(object obj, EventArgs ea)
{
DialogResult = DialogResult.OK;
}
void ButtonCancelOnClick(object obj, EventArgs ea)
{
DialogResult = DialogResult.Cancel;
}
}
Form.DisplayRectangle
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class CenterForm : System.Windows.Forms.Form
{
private System.ruponentModel.Container components;
public CenterForm()
{
InitializeComponent();
CenterToScreen();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "DataGridExample";
this.Resize += new System.EventHandler(this.CenterForm_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.CenterForm_Paint);
}
[STAThread]
static void Main()
{
Application.Run(new CenterForm());
}
private void CenterForm_Resize(object sender, System.EventArgs e)
{
Invalidate();
Console.WriteLine("Resize");
}
private void CenterForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("Text",
new Font("Times New Roman", 20),
new SolidBrush(Color.Black),
this.DisplayRectangle);
}
}
Form.Dispose
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormDispose : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public FormDispose()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
MessageBox.Show("Disposing this Form");
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
[STAThread]
static void Main()
{
Application.Run(new FormDispose());
}
}
Form.ForeColor
using System;
using System.Windows.Forms;
using System.Drawing;
public class ButtonClickEvent : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
public ButtonClickEvent()
{
Text = "Test WinForm";
ForeColor = System.Drawing.Color.Yellow;
button1 = new System.Windows.Forms.Button();
textBox1 = new System.Windows.Forms.TextBox();
// button control and its properties
button1.Location = new System.Drawing.Point(8, 32);
button1.Name = "button1";
button1.Size = new System.Drawing.Size(104, 32);
button1.TabIndex = 0;
button1.Text = "Click Me";
// text box control and its properties
textBox1.Location = new System.Drawing.Point(24, 104);
textBox1.Name = "textBox1";
textBox1.Size = new System.Drawing.Size(184, 20);
textBox1.TabIndex = 1;
textBox1.Text = "textBox1";
// Adding controls to the fomr
Controls.AddRange(new System.Windows.Forms.Control[]{textBox1, button1} );
button1.Click += new System.EventHandler(button1_Click);
}
private void button1_Click(object sender,System.EventArgs e)
{
textBox1.Text = "Button is clicked";
MessageBox.Show("Button is clicked");
}
public static int Main()
{
Application.Run(new ButtonClickEvent());
return 0;
}
}
Form.FormBorderStyle
using System;
using System.Drawing;
using System.Windows.Forms;
class AutoScaleDemo: Form
{
public static void Main()
{
Application.Run(new AutoScaleDemo());
}
public AutoScaleDemo()
{
Font = new Font("Arial", 12);
FormBorderStyle = FormBorderStyle.FixedSingle;
int[] aiPointSize = { 8, 12, 16, 24, 32 };
for (int i = 0; i < aiPointSize.Length; i++)
{
Button btn = new Button();
btn.Parent = this;
btn.Text = "Use " + aiPointSize[i] + "-point font";
btn.Tag = aiPointSize[i];
btn.Location = new Point(4, 16 + 24 * i);
btn.Size = new Size(80, 16);
btn.Click += new EventHandler(ButtonOnClick);
}
ClientSize = new Size(88, 16 + 24 * aiPointSize.Length);
AutoScaleBaseSize = new Size(4, 8);
}
protected override void OnPaint(PaintEventArgs pea)
{
pea.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), 0, 0);
}
void ButtonOnClick(object obj, EventArgs ea)
{
Button btn = (Button) obj;
SizeF sizefOld = GetAutoScaleSize(Font);
Font = new Font(Font.FontFamily, (int) btn.Tag);
SizeF sizefNew = GetAutoScaleSize(Font);
Scale(sizefNew.Width / sizefOld.Width,sizefNew.Height / sizefOld.Height);
}
}
Form.GotFocus
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FocusForm : System.Windows.Forms.Form {
private System.Windows.Forms.TextBox txtFocusForm;
private System.Windows.Forms.Button btFocusForm;
public FocusForm() {
this.txtFocusForm = new System.Windows.Forms.TextBox();
this.btFocusForm = new System.Windows.Forms.Button();
this.SuspendLayout();
this.txtFocusForm.Location = new System.Drawing.Point(8, 8);
this.txtFocusForm.Size = new System.Drawing.Size(336, 20);
this.txtFocusForm.LostFocus += new System.EventHandler(this.txtFocusForm_LostFocus);
this.txtFocusForm.GotFocus += new System.EventHandler(this.txtFocusForm_GotFocus);
this.btFocusForm.Location = new System.Drawing.Point(8, 40);
this.btFocusForm.Size = new System.Drawing.Size(336, 23);
this.btFocusForm.Click += new System.EventHandler(this.btFocusForm_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(352, 70);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btFocusForm,
this.txtFocusForm});
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new FocusForm());
}
protected void txtFocusForm_LostFocus(object sender, EventArgs e) {
Console.WriteLine("Goodbye!");
}
protected void txtFocusForm_GotFocus(object sender, EventArgs e) {
Console.WriteLine("Hello!");
}
private void btFocusForm_Click(object sender, System.EventArgs e) {
bool canFocus = txtFocusForm.CanFocus;
bool containsFocus = this.ContainsFocus;
bool focused = txtFocusForm.Focused;
Console.WriteLine("Textbox can focus: " + canFocus +
"\nForm children contain focus: " + containsFocus +
"\nTextbox has focus: " + focused);
txtFocusForm.Focus();
}
}
Form.Height
using System.Drawing;
using System.Windows.Forms;
class FormProperties
{
public static void Main()
{
Form form = new Form();
form.Height /= 2;
Application.Run(form);
}
}
Form.Hide()
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form2 : Form
{
private System.Windows.Forms.Button Button3;
private System.Windows.Forms.Button Button2;
private System.Windows.Forms.Button Button1;
public Form2()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Button3 = new System.Windows.Forms.Button();
this.Button2 = new System.Windows.Forms.Button();
this.Button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// Button3
//
this.Button3.Location = new System.Drawing.Point(120, 56);
this.Button3.Name = "Button3";
this.Button3.Size = new System.Drawing.Size(88, 32);
this.Button3.TabIndex = 8;
this.Button3.Text = "Become Child of Parent2";
this.Button3.Click += new System.EventHandler(this.Button3_Click);
//
// Button2
//
this.Button2.Location = new System.Drawing.Point(12, 56);
this.Button2.Name = "Button2";
this.Button2.Size = new System.Drawing.Size(88, 32);
this.Button2.TabIndex = 7;
this.Button2.Text = "Become Child of Parent1";
this.Button2.Click += new System.EventHandler(this.Button2_Click);
//
// Button1
//
this.Button1.Location = new System.Drawing.Point(12, 12);
this.Button1.Name = "Button1";
this.Button1.Size = new System.Drawing.Size(88, 32);
this.Button1.TabIndex = 6;
this.Button1.Text = "Become Parent";
this.Button1.Click += new System.EventHandler(this.Button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 154);
this.Controls.Add(this.Button3);
this.Controls.Add(this.Button2);
this.Controls.Add(this.Button1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
private void Button1_Click(object sender, System.EventArgs e)
{
this.Hide();
this.MdiParent = null;
this.IsMdiContainer = true;
this.Show();
}
private void Button2_Click(object sender, System.EventArgs e)
{
this.Hide();
this.IsMdiContainer = false;
this.MdiParent = Program.Main2;
this.Show();
}
private void Button3_Click(object sender, System.EventArgs e)
{
this.Hide();
this.IsMdiContainer = false;
this.MdiParent = Program.Main1;
this.Show();
}
}
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(422, 351);
this.IsMdiContainer = true;
this.Name = "Form1";
this.Text = "Form1";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.ResumeLayout(false);
}
}
static class Program
{
public static Form1 Main1 = new Form1();
public static Form1 Main2 = new Form1();
public static Form2 Child = new Form2();
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Main1.Text = "Parent 2";
Main2.Text = "Parent 1";
Main1.Show();
Main2.Show();
Child.MdiParent = Main2;
Child.Show();
System.Windows.Forms.Application.Run();
}
}
Form.Invalidate
using System;
using System.Drawing;
using System.Windows.Forms;
public class PaintInvalidate : Form
{
private Button btn;
public PaintInvalidate()
{
Size = new Size(300,200);
btn = new Button();
btn.Parent = this;
btn.Location = new Point(25,25);
btn.Text = "Update";
btn.Click += new System.EventHandler(btn_Click);
}
static void Main()
{
Application.Run(new PaintInvalidate());
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
String str = "\nThe time is " + DateTime.Now.ToLongTimeString();
g.DrawString(str, Font, Brushes.Black, 50, 75);
}
private void btn_Click(object sender, EventArgs e)
{
Invalidate();
}
}
Form.IsMdiContainer
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form2 : Form
{
private System.Windows.Forms.Button Button3;
private System.Windows.Forms.Button Button2;
private System.Windows.Forms.Button Button1;
public Form2()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Button3 = new System.Windows.Forms.Button();
this.Button2 = new System.Windows.Forms.Button();
this.Button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// Button3
//
this.Button3.Location = new System.Drawing.Point(120, 56);
this.Button3.Name = "Button3";
this.Button3.Size = new System.Drawing.Size(88, 32);
this.Button3.TabIndex = 8;
this.Button3.Text = "Become Child of Parent2";
this.Button3.Click += new System.EventHandler(this.Button3_Click);
//
// Button2
//
this.Button2.Location = new System.Drawing.Point(12, 56);
this.Button2.Name = "Button2";
this.Button2.Size = new System.Drawing.Size(88, 32);
this.Button2.TabIndex = 7;
this.Button2.Text = "Become Child of Parent1";
this.Button2.Click += new System.EventHandler(this.Button2_Click);
//
// Button1
//
this.Button1.Location = new System.Drawing.Point(12, 12);
this.Button1.Name = "Button1";
this.Button1.Size = new System.Drawing.Size(88, 32);
this.Button1.TabIndex = 6;
this.Button1.Text = "Become Parent";
this.Button1.Click += new System.EventHandler(this.Button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 154);
this.Controls.Add(this.Button3);
this.Controls.Add(this.Button2);
this.Controls.Add(this.Button1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
private void Button1_Click(object sender, System.EventArgs e)
{
this.Hide();
this.MdiParent = null;
this.IsMdiContainer = true;
this.Show();
}
private void Button2_Click(object sender, System.EventArgs e)
{
this.Hide();
this.IsMdiContainer = false;
this.MdiParent = Program.Main2;
this.Show();
}
private void Button3_Click(object sender, System.EventArgs e)
{
this.Hide();
this.IsMdiContainer = false;
this.MdiParent = Program.Main1;
this.Show();
}
}
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(422, 351);
this.IsMdiContainer = true;
this.Name = "Form1";
this.Text = "Form1";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.ResumeLayout(false);
}
}
static class Program
{
public static Form1 Main1 = new Form1();
public static Form1 Main2 = new Form1();
public static Form2 Child = new Form2();
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Main1.Text = "Parent 2";
Main2.Text = "Parent 1";
Main1.Show();
Main2.Show();
Child.MdiParent = Main2;
Child.Show();
System.Windows.Forms.Application.Run();
}
}
Form.KeyPress
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class KeyReader : System.Windows.Forms.Form {
private System.Windows.Forms.Label lblPress;
private System.Windows.Forms.Label lblDown;
private System.Windows.Forms.Label label1;
public KeyReader() {
InitializeComponent();
}
private void InitializeComponent() {
this.lblPress = new System.Windows.Forms.Label();
this.lblDown = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.lblPress.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPress.Location = new System.Drawing.Point(8, 190);
this.lblPress.Name = "lblPress";
this.lblPress.Size = new System.Drawing.Size(408, 48);
this.lblPress.TabIndex = 0;
this.lblPress.Text = "Press:";
this.lblPress.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lblDown.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblDown.Location = new System.Drawing.Point(8, 254);
this.lblDown.Name = "lblDown";
this.lblDown.Size = new System.Drawing.Size(408, 48);
this.lblDown.TabIndex = 0;
this.lblDown.Text = "Down:";
this.lblDown.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(424, 365);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.lblDown,
this.lblPress});
this.KeyPreview = true;
this.Name = "KeyReader";
this.Text = "KeyReader";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyReader_KeyDown);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.KeyReader_KeyPress);
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new KeyReader());
}
private void KeyReader_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
lblPress.Text = "Press: " + Convert.ToString(e.KeyChar);
}
private void KeyReader_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
lblDown.Text = "Down: " + Convert.ToString(e.KeyCode);
if (e.KeyCode == Keys.ShiftKey){
MessageBox.Show("That is one shifty character");
}
}
}
Form.KeyPreview
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace KeyPress
{
/// <summary>
/// Summary description for KeyPress.
/// </summary>
public class KeyPress : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public KeyPress()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(184, 104);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(176, 192);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 32);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
//
// KeyPress
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1});
this.Name = "KeyPress";
this.Text = "KeyPress";
this.Load += new System.EventHandler(this.KeyPress_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new KeyPress());
}
private void KeyPress_Load(object sender, System.EventArgs e)
{
this.KeyPreview=true;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
MessageBox.Show("Key Pressed = " + e.KeyChar.ToString());
}
}
}
Form.KeyUp
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormMouseKeyEvent : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public FormMouseKeyEvent()
{
this.KeyUp += new KeyEventHandler(OnKeyUp);
InitializeComponent();
CenterToScreen();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
[STAThread]
static void Main()
{
Application.Run(new FormMouseKeyEvent());
}
public void OnKeyUp(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString(), "Key Pressed!");
}
}
Form.Load
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ComboBoxes : Form
{
ComboBox cmb;
Button btnDisplay;
Button btnInsert;
Button btnSelect;
Label lblEdit;
TextBox txtDisplay;
Boolean boolChange = false;
Boolean boolProcessed = false;
public ComboBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
cmb = new ComboBox();
cmb.Parent = this;
cmb.Location = new Point(10,10);
cmb.Size = new Size(ClientSize.Width / 2, Height - 200);
cmb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
cmb.DropDownStyle = ComboBoxStyle.DropDown;
cmb.DropDownStyle = ComboBoxStyle.Simple;
cmb.DropDownWidth = (int)(cmb.Width * 1.5);
cmb.MaxDropDownItems = 12;
cmb.MaxLength = 20;
cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);
cmb.Leave += new EventHandler(cmb_Leave);
btnInsert = new Button();
btnInsert.Parent = this;
btnInsert.Text = "&Insert Item";
btnInsert.Size = new Size((int)(Font.Height * .75) * btnInsert.Text.Length, cmb.Height);
btnInsert.Location = new Point(cmb.Right + 10, cmb.Top);
btnInsert.Click += new System.EventHandler(btnInsert_Click);
lblEdit = new Label();
lblEdit.Parent = this;
lblEdit.BorderStyle = BorderStyle.Fixed3D;
lblEdit.Location = new Point(cmb.Left, cmb.Bottom + 10);
lblEdit.BackColor = Color.LightGray;
lblEdit.Text = "";
lblEdit.Size = new Size(cmb.DropDownWidth, Font.Height * 2);
btnDisplay = new Button();
btnDisplay.Parent = this;
btnDisplay.Text = "&Display Items";
btnDisplay.Size = new Size((int)(Font.Height * .75) * btnDisplay.Text.Length, cmb.Height);
btnDisplay.Location = new Point(lblEdit.Left, lblEdit.Bottom + 10);
btnDisplay.Click += new System.EventHandler(btnDisplay_Click);
txtDisplay = new TextBox();
txtDisplay.Parent = this;
txtDisplay.Location = new Point(btnDisplay.Left, btnDisplay.Bottom + 10);
txtDisplay.Multiline = true;
txtDisplay.ReadOnly = true;
txtDisplay.BackColor = Color.LightGray;
txtDisplay.ScrollBars = ScrollBars.Vertical;
txtDisplay.Text = "";
txtDisplay.Size = new Size(cmb.DropDownWidth, 200);
btnSelect = new Button();
btnSelect.Parent = this;
btnSelect.Text = "&Select 4";
btnSelect.Size = new Size((int)(Font.Height * .75) * btnSelect.Text.Length, cmb.Height);
btnSelect.Location = new Point(btnDisplay.Right + 10, btnDisplay.Top);
btnSelect.Click += new System.EventHandler(btnSelect_Click);
cmb.Items.Add("A");
cmb.Items.Add("B");
cmb.Items.Add("C");
cmb.Items.Add("D");
cmb.Items.Add("E");
cmb.SelectedIndex = 0;
}
static void Main()
{
Application.Run(new ComboBoxes());
}
private void this_Load(object sender, EventArgs e)
{
cmb.TextChanged += new EventHandler(cmb_TextChanged);
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
}
private void cmb_TextChanged(object sender, EventArgs e)
{
if (!boolProcessed)
lblEdit.Text = cmb.Text;
boolChange = true;
}
private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
if (boolChange)
{
boolChange = false;
boolProcessed = false;
}
}
private void cmb_SelectionChangeCommitted(object sender, EventArgs e)
{
if (boolChange)
ProcessChange();
}
private void cmb_Leave(object sender, EventArgs e)
{
if (boolChange)
{
ProcessChange();
boolChange = false;
}
}
private void ProcessChange()
{
lblEdit.Text = "Edited: " + cmb.Text;
boolProcessed = true;
}
private void btnDisplay_Click(object sender, EventArgs e)
{
string str = DateTime.Now.ToString() + "\r\n";
foreach (object item in cmb.Items)
{
str += item.ToString() + "\r\n";
}
txtDisplay.Text = str;
}
private void btnSelect_Click(object sender, EventArgs e)
{
cmb.Select(1,2);
}
private void btnInsert_Click(object sender, EventArgs e)
{
if (cmb.FindStringExact(cmb.Text) != -1)
{
MessageBox.Show(""" + cmb.Text + "" already exists in the list.\r\n" +
"Will not be added again.",
"Already Exists!");
}
else if (cmb.Text == "")
{
MessageBox.Show("There is nothing to add.","Nothing There");
}
else
{
cmb.Items.Add(cmb.Text);
}
}
}
Form.Location
using System;
using System.Drawing;
using System.Windows.Forms;
public class FormMoveDemo : Form
{
private bool dragging;
private Point pointClicked;
public FormMoveDemo()
{
InitializeComponent();
}
private void lblDrag_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
dragging = true;
pointClicked = new Point(e.X, e.Y);
}
else
{
dragging = false;
}
}
private void lblDrag_MouseMove(object sender, MouseEventArgs e)
{
if (dragging){
Point pointMoveTo;
pointMoveTo = this.PointToScreen(new Point(e.X, e.Y));
pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);
this.Location = pointMoveTo;
}
}
private void lblDrag_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
}
private void cmdClose_Click(object sender, EventArgs e)
{
this.Close();
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new FormMoveDemo());
}
private System.Windows.Forms.Button cmdClose= new System.Windows.Forms.Button();
private System.Windows.Forms.Label lblDrag = new System.Windows.Forms.Label();
private System.ruponentModel.IContainer components = null;
private void InitializeComponent()
{
this.SuspendLayout();
this.cmdClose.Location = new System.Drawing.Point(102, 215);
this.cmdClose.Name = "cmdClose";
this.cmdClose.Size = new System.Drawing.Size(76, 20);
this.cmdClose.TabIndex = 5;
this.cmdClose.Text = "Close";
this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);
this.lblDrag.BackColor = System.Drawing.Color.Navy;
this.lblDrag.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblDrag.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblDrag.ForeColor = System.Drawing.Color.White;
this.lblDrag.Location = new System.Drawing.Point(94, 167);
this.lblDrag.Name = "lblDrag";
this.lblDrag.Size = new System.Drawing.Size(96, 36);
this.lblDrag.TabIndex = 4;
this.lblDrag.Text = "Click here to move the form!";
this.lblDrag.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblDrag_MouseUp);
this.lblDrag.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lblDrag_MouseMove);
this.lblDrag.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lblDrag_MouseDown);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.ControlBox = false;
this.Controls.Add(this.cmdClose);
this.Controls.Add(this.lblDrag);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ResumeLayout(false);
}
}
Form.MaximizeBox
using System.Drawing;
using System.Windows.Forms;
class FormProperties
{
public static void Main()
{
Form form = new Form();
form.Text = "Form Properties";
form.BackColor = Color.BlanchedAlmond;
form.Width *= 2;
form.Height /= 2;
form.FormBorderStyle = FormBorderStyle.FixedSingle;
form.MaximizeBox = false;
form.Cursor = Cursors.Hand;
form.StartPosition = FormStartPosition.CenterScreen;
Application.Run(form);
}
}
Form.MaximumSize
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
private Button myButton;
public Form1()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.MaximumSize=new Size(300,300);
this.MinimumSize=new Size(300,300);
myButton = new Button();
myButton.Text = "www.nfex.ru";
myButton.Location = new System.Drawing.Point(64, 32);
myButton.Size = new System.Drawing.Size(150, 50);
Controls.Add(myButton);
}
static void Main()
{
Application.Run(new Form1());
}
}
Form.MdiParent
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace MdiBasic
{
public class frmContainer : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public frmContainer()
{
InitializeComponent();
MdiBasic.frmChild child = new MdiBasic.frmChild(this);
child.Show();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.IsMdiContainer = true;
this.Name = "frmContainer";
this.Text = "MDI Basic";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
[STAThread]
static void Main()
{
Application.Run(new frmContainer());
}
}
public class frmChild : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public frmChild(MdiBasic.frmContainer parent)
{
InitializeComponent();
this.MdiParent = parent;
}
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "frmChild";
}
}
}
Form.Menu
using System;
using System.Windows.Forms;
class AddMenuForm : Form {
MainMenu MyMenu;
public AddMenuForm() {
Text = "Adding a Main Menu";
MyMenu = new MainMenu();
MenuItem m1 = new MenuItem("File");
MyMenu.MenuItems.Add(m1);
MenuItem m2 = new MenuItem("Tools");
MyMenu.MenuItems.Add(m2);
MenuItem subm1 = new MenuItem("Open");
m1.MenuItems.Add(subm1);
MenuItem subm2 = new MenuItem("Close");
m1.MenuItems.Add(subm2);
MenuItem subm3 = new MenuItem("Exit");
m1.MenuItems.Add(subm3);
MenuItem subm4 = new MenuItem("Coordinates");
m2.MenuItems.Add(subm4);
MenuItem subm5 = new MenuItem("Change Size");
m2.MenuItems.Add(subm5);
MenuItem subm6 = new MenuItem("Restore");
m2.MenuItems.Add(subm6);
subm1.Click += MMOpenClick;
subm2.Click += MMCloseClick;
subm3.Click += MMExitClick;
subm4.Click += MMCoordClick;
subm5.Click += MMChangeClick;
subm6.Click += MMRestoreClick;
Menu = MyMenu;
}
[STAThread]
public static void Main() {
AddMenuForm skel = new AddMenuForm();
Application.Run(skel);
}
protected void MMCoordClick(object who, EventArgs e) {
}
protected void MMChangeClick(object who, EventArgs e) {
}
protected void MMRestoreClick(object who, EventArgs e) {
}
protected void MMOpenClick(object who, EventArgs e) {
Console.WriteLine("MMOpenClick");
}
protected void MMCloseClick(object who, EventArgs e) {
Console.WriteLine("MMCloseClick");
}
protected void MMExitClick(object who, EventArgs e) {
Console.WriteLine("Exit");
}
}
Form.MenuComplete
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
internal struct TheFontSize
{
public static int Huge = 30;
public static int Normal = 20;
public static int Tiny = 8;
}
public class mainForm : System.Windows.Forms.Form
{
Color currColor = Color.MistyRose;
private int currFontSize = TheFontSize.Normal;
private StatusBarPanel sbPnlPrompt = new StatusBarPanel();
private StatusBarPanel sbPnlTime = new StatusBarPanel();
private MainMenu mainMenu = new MainMenu();
private MenuItem currentCheckedItem;
private MenuItem checkedHuge;
private MenuItem checkedNormal;
private MenuItem checkedTiny;
public mainForm()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.MenuComplete += new EventHandler(StatusForm_MenuDone);
BuildMenuSystem();
BuildStatBar();
}
static void Main()
{
Application.Run(new mainForm());
}
private void FileExit_Clicked(object sender, EventArgs e)
{
Console.WriteLine("File | Exit Menu item handler");
this.Close();
}
private void FileSave_Clicked(object sender, EventArgs e)
{
Console.WriteLine("File | Save Menu item handler");
}
private void ColorItem_Clicked(object sender, EventArgs e)
{
MenuItem miClicked = (MenuItem)sender;
string color = miClicked.Text.Remove(0,1);
this.BackColor = Color.FromName(color);
currColor = this.BackColor;
}
private void PopUp_Clicked(object sender, EventArgs e)
{
currentCheckedItem.Checked = false;
MenuItem miClicked = (MenuItem)sender;
string item = miClicked.Text;
if(item == "Huge") {
currFontSize = TheFontSize.Huge;
currentCheckedItem = checkedHuge;
}else if(item == "Normal") {
currFontSize = TheFontSize.Normal;
currentCheckedItem = checkedNormal;
}else if(item == "Tiny") {
currFontSize = TheFontSize.Tiny;
currentCheckedItem = checkedTiny;
}
currentCheckedItem.Checked = true;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("www.nfex.ru",
new Font("Times New Roman", (float)currFontSize),
new SolidBrush(Color.Black),
this.DisplayRectangle);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Invalidate();
}
private void HelpAbout_Clicked(object sender, EventArgs e)
{
Console.WriteLine("The amazing final app...", "About...");
}
private void FileMenuItem_Selected(object sender, EventArgs e)
{
MenuItem miClicked = (MenuItem)sender;
string item = miClicked.Text.Remove(0,1);
if(item == "Save..."){
sbPnlPrompt.Text = "Save current settings.";
}else{
sbPnlPrompt.Text = "Terminates this app.";
}
}
private void ColorMenuItem_Selected(object sender, EventArgs e)
{
MenuItem miClicked = (MenuItem)sender;
string item = miClicked.Text.Remove(0,1);
sbPnlPrompt.Text = "Select " + item;
}
private void HelpAbout_Selected(object sender, EventArgs e)
{
sbPnlPrompt.Text = "Displays app info";
}
private void StatusForm_MenuDone(object sender, EventArgs e)
{
sbPnlPrompt.Text = "Ready";
}
private void timer1_Tick(object sender, EventArgs e)
{
DateTime t = DateTime.Now;
string s = t.ToLongTimeString() ;
sbPnlTime.Text = s ;
}
private void BuildMenuSystem()
{
MenuItem miFile = mainMenu.MenuItems.Add("&File");
miFile.MenuItems.Add(new MenuItem("&Save...", new EventHandler(this.FileSave_Clicked), Shortcut.CtrlS));
miFile.MenuItems.Add(new MenuItem("E&xit", new EventHandler(this.FileExit_Clicked), Shortcut.CtrlX));
miFile.MenuItems[0].Select += new EventHandler(FileMenuItem_Selected);
miFile.MenuItems[1].Select += new EventHandler(FileMenuItem_Selected);
MenuItem miColor = mainMenu.MenuItems.Add("&Background Color");
miColor.MenuItems.Add("&DarkGoldenrod", new EventHandler(ColorItem_Clicked));
miColor.MenuItems.Add("&GreenYellow", new EventHandler(ColorItem_Clicked));
for(int i = 0; i < miColor.MenuItems.Count; i++){
miColor.MenuItems[i].Select += new EventHandler(ColorMenuItem_Selected);
}
MenuItem miHelp = mainMenu.MenuItems.Add("Help");
miHelp.MenuItems.Add(new MenuItem("&About", new EventHandler(this.HelpAbout_Clicked), Shortcut.CtrlA));
miHelp.MenuItems[0].Select += new EventHandler(HelpAbout_Selected);
this.Menu = mainMenu;
ContextMenu popUpMenu = new ContextMenu();
popUpMenu.MenuItems.Add("Huge", new EventHandler(PopUp_Clicked));
popUpMenu.MenuItems.Add("Normal", new EventHandler(PopUp_Clicked));
popUpMenu.MenuItems.Add("Tiny", new EventHandler(PopUp_Clicked));
this.ContextMenu = popUpMenu;
checkedHuge = this.ContextMenu.MenuItems[0];
checkedNormal = this.ContextMenu.MenuItems[1];
checkedTiny = this.ContextMenu.MenuItems[2];
if(currFontSize == TheFontSize.Huge)
currentCheckedItem = checkedHuge;
else if(currFontSize == TheFontSize.Normal)
currentCheckedItem = checkedNormal;
else
currentCheckedItem = checkedTiny;
currentCheckedItem.Checked = true;
}
private void BuildStatBar()
{
Timer timer1 = new Timer();
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
StatusBar statusBar = new StatusBar();
statusBar.ShowPanels = true;
statusBar.Panels.AddRange((StatusBarPanel[])new StatusBarPanel[] {sbPnlPrompt, sbPnlTime});
sbPnlPrompt.BorderStyle = StatusBarPanelBorderStyle.None;
sbPnlPrompt.AutoSize = StatusBarPanelAutoSize.Spring;
sbPnlPrompt.Width = 62;
sbPnlPrompt.Text = "Ready";
sbPnlTime.Alignment = System.Windows.Forms.HorizontalAlignment.Right;
sbPnlTime.Width = 76;
try
{
Icon i = new Icon("icon1.ico");
sbPnlPrompt.Icon = i;
} catch(Exception e) {
MessageBox.Show(e.Message);
}
this.Controls.Add(statusBar);
}
}
Form.MinimizeBox
using System;
using System.Drawing;
using System.Windows.Forms;
class BetterDialog: Form
{
public static void Main()
{
Application.Run(new BetterDialog());
}
public BetterDialog()
{
Menu = new MainMenu();
Menu.MenuItems.Add("&Dialog!", new EventHandler(MenuOnClick));
}
void MenuOnClick(object obj, EventArgs ea)
{
BetterDialogBox dlg = new BetterDialogBox();
DialogResult dr = dlg.ShowDialog();
Console.WriteLine(dr);
}
}
class BetterDialogBox: Form
{
public BetterDialogBox()
{
Text = "Better Dialog Box";
FormBorderStyle = FormBorderStyle.FixedDialog;
ControlBox = false;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
Location = ActiveForm.Location +
SystemInformation.CaptionButtonSize +
SystemInformation.FrameBorderSize;
Button btn = new Button();
btn.Parent = this;
btn.Text = "OK";
btn.Location = new Point(50, 50);
btn.Size = new Size (10 * Font.Height, 2 * Font.Height);
btn.DialogResult = DialogResult.OK;
AcceptButton = btn;
btn = new Button();
btn.Parent = this;
btn.Text = "Cancel";
btn.Location = new Point(50, 100);
btn.Size = new Size (10 * Font.Height, 2 * Font.Height);
btn.DialogResult = DialogResult.Cancel;
CancelButton = btn;
}
}
Form.MinimumSize
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
private Button myButton;
public Form1()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.MaximumSize=new Size(300,300);
this.MinimumSize=new Size(300,300);
myButton = new Button();
myButton.Text = "www.nfex.ru";
myButton.Location = new System.Drawing.Point(64, 32);
myButton.Size = new System.Drawing.Size(150, 50);
Controls.Add(myButton);
}
static void Main()
{
Application.Run(new Form1());
}
}
Form.MouseDown
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form{
public Form1() {
InitializeComponent();
}
private void InitializeComponent() {
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
}
static void Main() {
Application.Run(new Form1());
}
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
Console.WriteLine("Mouse down");
}
}
Form.MouseLeave(All other events)
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Activated(object sender, EventArgs e)
{
Console.WriteLine("Activated");
}
private void Form1_AutoSizeChanged(object sender, EventArgs e)
{
Console.WriteLine("Autosizechanged");
}
private void Form1_AutoValidateChanged(object sender, EventArgs e)
{
Console.WriteLine("AutoValidateChanged");
}
private void Form1_BackColorChanged(object sender, EventArgs e)
{
Console.WriteLine("BackColorChanged");
}
private void Form1_BackgroundImageChanged(object sender, EventArgs e)
{
Console.WriteLine("BackgroundImageChanged");
}
private void Form1_BackgroundImageLayoutChanged(object sender, EventArgs e)
{
Console.WriteLine("BackgroundImageLayoutChanged");
}
private void Form1_BindingContextChanged(object sender, EventArgs e)
{
Console.WriteLine("BindingContextChanged");
}
private void Form1_CausesValidationChanged(object sender, EventArgs e)
{
Console.WriteLine("CausesValidationChanged");
}
private void Form1_ChangeUICues(object sender, UICuesEventArgs e)
{
Console.WriteLine("ChangeUICues");
}
private void Form1_Click(object sender, EventArgs e)
{
Console.WriteLine("Click");
}
private void Form1_ContextMenuStripChanged(object sender, EventArgs e)
{
Console.WriteLine("ContextMenuStripChanged");
}
private void Form1_ControlAdded(object sender, ControlEventArgs e)
{
Console.WriteLine("ControlAdded");
}
private void Form1_ControlRemoved(object sender, ControlEventArgs e)
{
Console.WriteLine("ControlRemoved");
}
private void Form1_CursorChanged(object sender, EventArgs e)
{
Console.WriteLine("CursorChanged");
}
private void Form1_Deactivate(object sender, EventArgs e)
{
Console.WriteLine("Deactivate");
}
private void Form1_DockChanged(object sender, EventArgs e)
{
Console.WriteLine("DockChanged");
}
private void Form1_DoubleClick(object sender, EventArgs e)
{
Console.WriteLine("DoubleClick");
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
Console.WriteLine("DragDrop");
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
Console.WriteLine("DragEnter");
}
private void Form1_DragLeave(object sender, EventArgs e)
{
Console.WriteLine("DragLeave");
}
private void Form1_DragOver(object sender, DragEventArgs e)
{
Console.WriteLine("DragOver");
}
private void Form1_EnabledChanged(object sender, EventArgs e)
{
Console.WriteLine("EnabledChanged");
}
private void Form1_Enter(object sender, EventArgs e)
{
Console.WriteLine("Enter");
}
private void Form1_FontChanged(object sender, EventArgs e)
{
Console.WriteLine("FontChanged");
}
private void Form1_ForeColorChanged(object sender, EventArgs e)
{
Console.WriteLine("ForeColorChanged");
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Console.WriteLine("FormClosed");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Console.WriteLine("FormClosing");
}
private void Form1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
Console.WriteLine("GiveFeedback");
}
private void Form1_HelpButtonClicked(object sender, CancelEventArgs e)
{
Console.WriteLine("HelpButtonClicked");
}
private void Form1_HelpRequested(object sender, HelpEventArgs hlpevent)
{
Console.WriteLine("HelpRequested");
}
private void Form1_ImeModeChanged(object sender, EventArgs e)
{
Console.WriteLine("ImeModeChanged");
}
private void Form1_InputLanguageChanged(object sender, InputLanguageChangedEventArgs e)
{
Console.WriteLine("InputLanguageChanged");
}
private void Form1_InputLanguageChanging(object sender, InputLanguageChangingEventArgs e)
{
Console.WriteLine("InputLanguageChanging");
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("KeyDown");
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
Console.WriteLine("KeyPress");
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
Console.WriteLine("KeyUp");
}
private void Form1_Layout(object sender, LayoutEventArgs e)
{
Console.WriteLine("Layout");
}
private void Form1_Leave(object sender, EventArgs e)
{
Console.WriteLine("Leave");
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Load");
}
private void Form1_LocationChanged(object sender, EventArgs e)
{
Console.WriteLine("LocationChanged");
}
private void Form1_MarginChanged(object sender, EventArgs e)
{
Console.WriteLine("MarginChanged");
}
private void Form1_MaximizedBoundsChanged(object sender, EventArgs e)
{
Console.WriteLine("MaximizedBoundsChanged");
}
private void Form1_MaximumSizeChanged(object sender, EventArgs e)
{
Console.WriteLine("MAximumSizeChanged");
}
private void Form1_MdiChildActivate(object sender, EventArgs e)
{
Console.WriteLine("MdiChildActivate");
}
private void Form1_MenuComplete(object sender, EventArgs e)
{
Console.WriteLine("MenuComplete");
}
private void Form1_MenuStart(object sender, EventArgs e)
{
Console.WriteLine("MenuStart");
}
private void Form1_MinimumSizeChanged(object sender, EventArgs e)
{
Console.WriteLine("MinimumSizeChanged");
}
private void Form1_MouseCaptureChanged(object sender, EventArgs e)
{
Console.WriteLine("MouseCaptureChanged");
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseClick");
}
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseDoubleClick");
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseDown");
}
private void Form1_MouseEnter(object sender, EventArgs e)
{
Console.WriteLine("MouseEnter");
}
private void Form1_MouseHover(object sender, EventArgs e)
{
Console.WriteLine("MouseHover");
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
Console.WriteLine("MouseLeave");
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseMove");
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseUp");
}
private void Form1_Move(object sender, EventArgs e)
{
Console.WriteLine("Move");
}
private void Form1_PaddingChanged(object sender, EventArgs e)
{
Console.WriteLine("PaddingChanged");
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Console.WriteLine("Paint");
}
private void Form1_ParentChanged(object sender, EventArgs e)
{
Console.WriteLine("ParentChanged");
}
private void Form1_QueryAccessibilityHelp(object sender, QueryAccessibilityHelpEventArgs e)
{
Console.WriteLine("QueryAccessibilityHelp");
}
private void Form1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
Console.WriteLine("QueryContinueDrag");
}
private void Form1_RegionChanged(object sender, EventArgs e)
{
Console.WriteLine("RegionChanged");
}
private void Form1_Resize(object sender, EventArgs e)
{
Console.WriteLine("Resize");
}
private void Form1_ResizeBegin(object sender, EventArgs e)
{
Console.WriteLine("ResizeBegin");
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
Console.WriteLine("ResizeEnd");
}
private void Form1_RightToLeftChanged(object sender, EventArgs e)
{
Console.WriteLine("RightToLeftChanged");
}
private void Form1_RightToLeftLayoutChanged(object sender, EventArgs e)
{
Console.WriteLine("RightToLeftLayoutChanged");
}
private void Form1_Scroll(object sender, ScrollEventArgs e)
{
Console.WriteLine("Scroll");
}
private void Form1_Shown(object sender, EventArgs e)
{
Console.WriteLine("Shown");
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
Console.WriteLine("SizeChanged");
}
private void Form1_StyleChanged(object sender, EventArgs e)
{
Console.WriteLine("StyleChanged");
}
private void Form1_SystemColorsChanged(object sender, EventArgs e)
{
Console.WriteLine("SystemColorsChanged");
}
private void Form1_TextChanged(object sender, EventArgs e)
{
Console.WriteLine("TextChanged");
}
private void Form1_Validated(object sender, EventArgs e)
{
Console.WriteLine("Validated");
}
private void Form1_Validating(object sender, CancelEventArgs e)
{
Console.WriteLine("Validating");
}
private void Form1_VisibleChanged(object sender, EventArgs e)
{
Console.WriteLine("VisibleChanged");
}
}
partial class Form1
{
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(87, 79);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.CursorChanged += new System.EventHandler(this.Form1_CursorChanged);
this.RightToLeftLayoutChanged += new System.EventHandler(this.Form1_RightToLeftLayoutChanged);
this.QueryContinueDrag += new System.Windows.Forms.QueryContinueDragEventHandler(this.Form1_QueryContinueDrag);
this.Deactivate += new System.EventHandler(this.Form1_Deactivate);
this.Load += new System.EventHandler(this.Form1_Load);
this.BackgroundImageLayoutChanged += new System.EventHandler(this.Form1_BackgroundImageLayoutChanged);
this.RightToLeftChanged += new System.EventHandler(this.Form1_RightToLeftChanged);
this.DragLeave += new System.EventHandler(this.Form1_DragLeave);
this.InputLanguageChanged += new System.Windows.Forms.InputLanguageChangedEventHandler(this.Form1_InputLanguageChanged);
this.Validating += new System.ruponentModel.CancelEventHandler(this.Form1_Validating);
this.BackgroundImageChanged += new System.EventHandler(this.Form1_BackgroundImageChanged);
this.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDoubleClick);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
this.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.Form1_ControlAdded);
this.FontChanged += new System.EventHandler(this.Form1_FontChanged);
this.MaximizedBoundsChanged += new System.EventHandler(this.Form1_MaximizedBoundsChanged);
this.AutoSizeChanged += new System.EventHandler(this.Form1_AutoSizeChanged);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.VisibleChanged += new System.EventHandler(this.Form1_VisibleChanged);
this.BindingContextChanged += new System.EventHandler(this.Form1_BindingContextChanged);
this.HelpButtonClicked += new System.ruponentModel.CancelEventHandler(this.Form1_HelpButtonClicked);
this.EnabledChanged += new System.EventHandler(this.Form1_EnabledChanged);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.ContextMenuStripChanged += new System.EventHandler(this.Form1_ContextMenuStripChanged);
this.Scroll += new System.Windows.Forms.ScrollEventHandler(this.Form1_Scroll);
this.MouseLeave += new System.EventHandler(this.Form1_MouseLeave);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
this.Validated += new System.EventHandler(this.Form1_Validated);
this.ParentChanged += new System.EventHandler(this.Form1_ParentChanged);
this.Resize += new System.EventHandler(this.Form1_Resize);
this.ControlRemoved += new System.Windows.Forms.ControlEventHandler(this.Form1_ControlRemoved);
this.Shown += new System.EventHandler(this.Form1_Shown);
this.AutoValidateChanged += new System.EventHandler(this.Form1_AutoValidateChanged);
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.DoubleClick += new System.EventHandler(this.Form1_DoubleClick);
this.Activated += new System.EventHandler(this.Form1_Activated);
this.Enter += new System.EventHandler(this.Form1_Enter);
this.Layout += new System.Windows.Forms.LayoutEventHandler(this.Form1_Layout);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.StyleChanged += new System.EventHandler(this.Form1_StyleChanged);
this.ForeColorChanged += new System.EventHandler(this.Form1_ForeColorChanged);
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
this.MouseEnter += new System.EventHandler(this.Form1_MouseEnter);
this.MdiChildActivate += new System.EventHandler(this.Form1_MdiChildActivate);
this.Leave += new System.EventHandler(this.Form1_Leave);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.MinimumSizeChanged += new System.EventHandler(this.Form1_MinimumSizeChanged);
this.Move += new System.EventHandler(this.Form1_Move);
this.MouseCaptureChanged += new System.EventHandler(this.Form1_MouseCaptureChanged);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.PaddingChanged += new System.EventHandler(this.Form1_PaddingChanged);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.ChangeUICues += new System.Windows.Forms.UICuesEventHandler(this.Form1_ChangeUICues);
this.DockChanged += new System.EventHandler(this.Form1_DockChanged);
this.GiveFeedback += new System.Windows.Forms.GiveFeedbackEventHandler(this.Form1_GiveFeedback);
this.ImeModeChanged += new System.EventHandler(this.Form1_ImeModeChanged);
this.Click += new System.EventHandler(this.Form1_Click);
this.SystemColorsChanged += new System.EventHandler(this.Form1_SystemColorsChanged);
this.QueryAccessibilityHelp += new System.Windows.Forms.QueryAccessibilityHelpEventHandler(this.Form1_QueryAccessibilityHelp);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.RegionChanged += new System.EventHandler(this.Form1_RegionChanged);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
this.MarginChanged += new System.EventHandler(this.Form1_MarginChanged);
this.TextChanged += new System.EventHandler(this.Form1_TextChanged);
this.ResizeBegin += new System.EventHandler(this.Form1_ResizeBegin);
this.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.Form1_HelpRequested);
this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.BackColorChanged += new System.EventHandler(this.Form1_BackColorChanged);
this.InputLanguageChanging += new System.Windows.Forms.InputLanguageChangingEventHandler(this.Form1_InputLanguageChanging);
this.MenuStart += new System.EventHandler(this.Form1_MenuStart);
this.MouseHover += new System.EventHandler(this.Form1_MouseHover);
this.ResizeEnd += new System.EventHandler(this.Form1_ResizeEnd);
this.DragOver += new System.Windows.Forms.DragEventHandler(this.Form1_DragOver);
this.CausesValidationChanged += new System.EventHandler(this.Form1_CausesValidationChanged);
this.MenuComplete += new System.EventHandler(this.Form1_MenuComplete);
this.MaximumSizeChanged += new System.EventHandler(this.Form1_MaximumSizeChanged);
this.ResumeLayout(false);
}
private System.Windows.Forms.Button button1;
}
public class FormEventAll
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
MessageBox.Show("Click OK to finish");
}
}
Form.MouseUp
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormMouseUpEvent : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public FormMouseUpEvent()
{
this.MouseUp += new MouseEventHandler(OnMouseUp);
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
[STAThread]
static void Main()
{
Application.Run(new FormMouseUpEvent());
}
protected void OnMouseUp(object sender, MouseEventArgs e)
{
// Which mouse button was clicked?
if(e.Button == MouseButtons.Left)
MessageBox.Show("Left click!");
if(e.Button == MouseButtons.Right)
MessageBox.Show("Right click!");
if(e.Button == MouseButtons.Middle)
MessageBox.Show("Middle click!");
}
}
Form.OnDragDrop
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
class ImageDrop : Form {
bool bIsTarget;
Image image;
public static void Main() {
Application.Run(new ImageDrop());
}
public ImageDrop() {
AllowDrop = true;
}
protected override void OnDragOver(DragEventArgs dea) {
if (dea.Data.GetDataPresent(DataFormats.FileDrop) || dea.Data.GetDataPresent(typeof(Metafile)) || dea.Data.GetDataPresent(typeof(Bitmap))) {
if ((dea.AllowedEffect & DragDropEffects.Move) != 0)
dea.Effect = DragDropEffects.Move;
if (((dea.AllowedEffect & DragDropEffects.Copy) != 0) && ((dea.KeyState & 0x08) != 0)) // Ctrl key
dea.Effect = DragDropEffects.Copy;
}
}
protected override void OnDragDrop(DragEventArgs dea) {
if (dea.Data.GetDataPresent(DataFormats.FileDrop)) {
string[] astr = (string[])dea.Data.GetData(DataFormats.FileDrop);
image = Image.FromFile(astr[0]);
Invalidate();
} else {
if (dea.Data.GetDataPresent(typeof(Metafile)))
image = (Image)dea.Data.GetData(typeof(Metafile));
else if (dea.Data.GetDataPresent(typeof(Bitmap)))
image = (Image)dea.Data.GetData(typeof(Bitmap));
bIsTarget = true;
Invalidate();
}
}
protected override void OnMouseDown(MouseEventArgs mea) {
if (image != null) {
bIsTarget = false;
DragDropEffects dde = DoDragDrop(image,DragDropEffects.Copy | DragDropEffects.Move);
if (dde == DragDropEffects.Move && !bIsTarget)
image = null;
}
}
}
Form.OnDragOver
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
class ImageDrop : Form {
bool bIsTarget;
Image image;
public static void Main() {
Application.Run(new ImageDrop());
}
public ImageDrop() {
AllowDrop = true;
}
protected override void OnDragOver(DragEventArgs dea) {
if (dea.Data.GetDataPresent(DataFormats.FileDrop) || dea.Data.GetDataPresent(typeof(Metafile)) || dea.Data.GetDataPresent(typeof(Bitmap))) {
if ((dea.AllowedEffect & DragDropEffects.Move) != 0)
dea.Effect = DragDropEffects.Move;
if (((dea.AllowedEffect & DragDropEffects.Copy) != 0) && ((dea.KeyState & 0x08) != 0)) // Ctrl key
dea.Effect = DragDropEffects.Copy;
}
}
protected override void OnDragDrop(DragEventArgs dea) {
if (dea.Data.GetDataPresent(DataFormats.FileDrop)) {
string[] astr = (string[])dea.Data.GetData(DataFormats.FileDrop);
image = Image.FromFile(astr[0]);
Invalidate();
} else {
if (dea.Data.GetDataPresent(typeof(Metafile)))
image = (Image)dea.Data.GetData(typeof(Metafile));
else if (dea.Data.GetDataPresent(typeof(Bitmap)))
image = (Image)dea.Data.GetData(typeof(Bitmap));
bIsTarget = true;
Invalidate();
}
}
protected override void OnMouseDown(MouseEventArgs mea) {
if (image != null) {
bIsTarget = false;
DragDropEffects dde = DoDragDrop(image,DragDropEffects.Copy | DragDropEffects.Move);
if (dde == DragDropEffects.Move && !bIsTarget)
image = null;
}
}
}
Form.OnInputLanguageChanged
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class InternationalText : System.Windows.Forms.Form {
internal System.Windows.Forms.Label lblInternationalText;
internal System.Windows.Forms.Label lblCharCode;
private System.Windows.Forms.TextBox textBox1;
public InternationalText() {
this.lblInternationalText = new System.Windows.Forms.Label();
this.lblCharCode = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.lblInternationalText.Location = new System.Drawing.Point(8, 64);
this.lblInternationalText.Size = new System.Drawing.Size(288, 23);
this.lblCharCode.Location = new System.Drawing.Point(8, 96);
this.lblCharCode.Size = new System.Drawing.Size(88, 23);
this.textBox1.Location = new System.Drawing.Point(8, 24);
this.textBox1.Size = new System.Drawing.Size(288, 20);
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(304, 134);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.lblCharCode,
this.lblInternationalText});
this.MaximizeBox = false;
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new InternationalText());
}
protected override void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
MessageBox.Show(e.InputLanguage.Culture.Name);
}
protected override void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
MessageBox.Show(e.InputLanguage.Culture.Name);
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
lblInternationalText.Text += e.KeyChar.ToString();
lblCharCode.Text = ((int)e.KeyChar).ToString();
}
}
Form.OnKeyDown
using System;
using System.Drawing;
using System.Windows.Forms;
class ExitOnX: Form
{
public static void Main()
{
Application.Run(new ExitOnX());
}
public ExitOnX()
{
Text = "Exit on X";
}
protected override void OnKeyDown(KeyEventArgs kea)
{
if (kea.KeyCode == Keys.X)
Close();
}
}
Form.OnKeyPress
using System.Drawing;
using System;
using System.Windows.Forms;
public class TryKey : Form {
private char theKey = "d";
public TryKey() {
Size = new Size(300,200);
BackColor = Color.White;
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.DrawString(theKey.ToString(), new Font("Arial", 36, FontStyle.Bold), Brushes.Red, 100, 50);
base.OnPaint(e);
}
protected override void OnKeyDown(KeyEventArgs e){
if (e.Control){
Console.WriteLine("Control");
}
if (e.KeyCode == Keys.Right){
Console.WriteLine("Right");
}
else if (e.KeyCode == Keys.Left){
Console.WriteLine("Left");
}
Invalidate();
base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyEventArgs e) {
Console.WriteLine("Key Up");
base.OnKeyUp(e);
}
protected override void OnKeyPress(KeyPressEventArgs e) {
if (char.IsLetterOrDigit(e.KeyChar)){
theKey = e.KeyChar;
}
Invalidate();
base.OnKeyPress(e);
}
public static void Main() {
Application.Run(new TryKey());
}
}
Form.OnMouseEnter
using System;
using System.Drawing;
using System.Windows.Forms;
class EnterLeave: Form
{
bool bInside = false;
public static void Main()
{
Application.Run(new EnterLeave());
}
public EnterLeave()
{
}
protected override void OnMouseEnter(EventArgs ea)
{
bInside = true;
Invalidate();
}
protected override void OnMouseLeave(EventArgs ea)
{
bInside = false;
Invalidate();
}
protected override void OnMouseHover(EventArgs ea)
{
Graphics grfx = CreateGraphics();
grfx.Clear(Color.Red);
System.Threading.Thread.Sleep(500);
grfx.Clear(Color.Green);
grfx.Dispose();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
grfx.Clear(bInside ? Color.Green : BackColor);
}
}
Form.OnMouseHover
using System;
using System.Drawing;
using System.Windows.Forms;
class EnterLeave: Form
{
bool bInside = false;
public static void Main()
{
Application.Run(new EnterLeave());
}
public EnterLeave()
{
}
protected override void OnMouseEnter(EventArgs ea)
{
bInside = true;
Invalidate();
}
protected override void OnMouseLeave(EventArgs ea)
{
bInside = false;
Invalidate();
}
protected override void OnMouseHover(EventArgs ea)
{
Graphics grfx = CreateGraphics();
grfx.Clear(Color.Red);
System.Threading.Thread.Sleep(500);
grfx.Clear(Color.Green);
grfx.Dispose();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
grfx.Clear(bInside ? Color.Green : BackColor);
}
}
Form.OnMouseLeave
using System;
using System.Drawing;
using System.Windows.Forms;
class EnterLeave: Form
{
bool bInside = false;
public static void Main()
{
Application.Run(new EnterLeave());
}
public EnterLeave()
{
}
protected override void OnMouseEnter(EventArgs ea)
{
bInside = true;
Invalidate();
}
protected override void OnMouseLeave(EventArgs ea)
{
bInside = false;
Invalidate();
}
protected override void OnMouseHover(EventArgs ea)
{
Graphics grfx = CreateGraphics();
grfx.Clear(Color.Red);
System.Threading.Thread.Sleep(500);
grfx.Clear(Color.Green);
grfx.Dispose();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
grfx.Clear(bInside ? Color.Green : BackColor);
}
}
Form.OnMouseMove
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormMouseMoveEvent : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public FormMouseMoveEvent()
{
this.MouseMove += new MouseEventHandler(OnMouseMove);
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
[STAThread]
static void Main()
{
Application.Run(new FormMouseMoveEvent());
}
protected void OnMouseMove(object sender, MouseEventArgs e)
{
this.Text = "Current Pos: (" + e.X + ", " + e.Y + ")";
}
}
Form.OnMouseWheel
using System;
using System.Drawing;
using System.Windows.Forms;
class PoePoem: Form
{
public static void Main()
{
if (!SystemInformation.MouseWheelPresent)
{
Console.WriteLine("Program needs a mouse with a mouse wheel!");
return;
}
Application.Run(new PoePoem());
}
public PoePoem()
{
}
protected override void OnMouseWheel(MouseEventArgs mea)
{
Console.WriteLine(mea.Delta);
}
}
Form.OnPaint(PaintEventArgs e)
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Font f = new Font("Times New Roman", 24);
g.DrawString("Translation", f, Brushes.Black, 0, 0);
g.TranslateTransform(150, 75);
g.DrawString("Translation", f, Brushes.Black, 0, 0);
}
public static void Main() {
Application.Run(new Form1());
}
}
Form.OnResize
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class GraphicUnitDocument : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public GraphicUnitDocument()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(408, 273);
this.Name = "GraphicUnitDocument";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "GDI+ Coordinate";
this.Resize += new System.EventHandler(this.OnResize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
}
[STAThread]
static void Main()
{
Application.Run(new GraphicUnitDocument());
}
protected void OnPaint (object sender, System.Windows.Forms.PaintEventArgs e)
{
GraphicsUnit gUnit = GraphicsUnit.Pixel;
Point renderingOrgPt = new Point(0,0);
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
gUnit = GraphicsUnit.Document;
g.PageUnit = gUnit;
g.TranslateTransform(renderingOrgPt.X,renderingOrgPt.Y);
g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100);
this.Text = string.Format("PageUnit: {0}, Origin: {1}", gUnit, renderingOrgPt.ToString());
}
protected void OnResize (object sender, System.EventArgs e)
{
Invalidate();
}
}
Form.Opacity
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormOpacitySetting : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public FormOpacitySetting()
{
InitializeComponent();
Opacity = 0.5d;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Form1";
}
[STAThread]
static void Main()
{
Application.Run(new FormOpacitySetting());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("String...", new Font("Times New Roman", 20), new SolidBrush(Color.Black), 40, 10);
}
}
Form.Paint
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class DrawLineDemo : System.Windows.Forms.Form
{
public DrawLineDemo()
{
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(400, 400);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawLineDemo_Paint);
}
static void Main()
{
Application.Run(new DrawLineDemo());
}
private void DrawLineDemo_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 10);
g.DrawLine(p, 25, 25, 375, 375);
}
}
Form.Region
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class IrregularForm : Form
{
public IrregularForm()
{
this.cmdClose = new System.Windows.Forms.Button();
this.SuspendLayout();
this.cmdClose.Location = new System.Drawing.Point(94, 231);
this.cmdClose.Size = new System.Drawing.Size(75, 23);
this.cmdClose.Text = "Close";
this.cmdClose.UseVisualStyleBackColor = true;
this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(295, 270);
this.Controls.Add(this.cmdClose);
this.Text = "Irregular Form";
this.Load += new System.EventHandler(this.IrregularForm_Load);
this.ResumeLayout(false);
}
private void IrregularForm_Load(object sender, EventArgs e)
{
GraphicsPath path = new GraphicsPath();
Point[] pointsA = new Point[]{new Point(0, 0),new Point(40, 60), new Point(this.Width - 100, 10)};
path.AddCurve(pointsA);
Point[] pointsB = new Point[]{
new Point(this.Width - 40, this.Height - 60),
new Point(this.Width, this.Height),
new Point(10, this.Height)
};
path.AddCurve(pointsB);
path.CloseAllFigures();
this.Region = new Region(path);
}
private void cmdClose_Click(object sender, EventArgs e)
{
this.Close();
}
private System.Windows.Forms.Button cmdClose;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new IrregularForm());
}
}
Form.Resize
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class SetBackgroundForm : System.Windows.Forms.Form
{
private System.ruponentModel.Container components;
public SetBackgroundForm()
{
InitializeComponent();
BackColor = Color.LemonChiffon;
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Resize += new System.EventHandler(this.SetBackgroundForm_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.SetBackgroundForm_Paint);
}
[STAThread]
static void Main()
{
Application.Run(new SetBackgroundForm());
}
private void SetBackgroundForm_Resize(object sender, System.EventArgs e)
{
Invalidate();
Console.WriteLine("Resize");
}
private void SetBackgroundForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("Text",
new Font("Times New Roman", 20),
new SolidBrush(Color.Black),
this.DisplayRectangle);
}
}
Form.ResizeRedraw
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
class PrintableForm: Form
{
public static void Main()
{
Application.Run(new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Pen pen = new Pen(clr);
grfx.DrawLine(pen, 0, 0, cx - 1, cy - 1);
grfx.DrawLine(pen, cx - 1, 0, 0, cy - 1);
}
}
Form.SetStyle
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
private System.ruponentModel.Container components = null;
private Image theImage;
public Form1() {
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
theImage = new Bitmap("Winter.jpg");
}
protected override void OnPaint(PaintEventArgs e){
Graphics g = e.Graphics;
g.DrawImage(theImage, ClientRectangle);
}
private void InitializeComponent() {
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
static void Main() {
Application.Run(new Form1());
}
}
Form.Show
using System.Windows.Forms;
class ShowForm
{
public static void Main()
{
Form form = new Form();
form.Show();
}
}
Form.ShowInTaskbar
using System;
using System.Drawing;
using System.Windows.Forms;
class BetterDialog: Form
{
public static void Main()
{
Application.Run(new BetterDialog());
}
public BetterDialog()
{
Menu = new MainMenu();
Menu.MenuItems.Add("&Dialog!", new EventHandler(MenuOnClick));
}
void MenuOnClick(object obj, EventArgs ea)
{
BetterDialogBox dlg = new BetterDialogBox();
DialogResult dr = dlg.ShowDialog();
Console.WriteLine(dr);
}
}
class BetterDialogBox: Form
{
public BetterDialogBox()
{
Text = "Better Dialog Box";
FormBorderStyle = FormBorderStyle.FixedDialog;
ControlBox = false;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
Location = ActiveForm.Location +
SystemInformation.CaptionButtonSize +
SystemInformation.FrameBorderSize;
Button btn = new Button();
btn.Parent = this;
btn.Text = "OK";
btn.Location = new Point(50, 50);
btn.Size = new Size (10 * Font.Height, 2 * Font.Height);
btn.DialogResult = DialogResult.OK;
AcceptButton = btn;
btn = new Button();
btn.Parent = this;
btn.Text = "Cancel";
btn.Location = new Point(50, 100);
btn.Size = new Size (10 * Font.Height, 2 * Font.Height);
btn.DialogResult = DialogResult.Cancel;
CancelButton = btn;
}
}
Form.Size
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class MyFirstWindow : System.Windows.Forms.Form
{
public MyFirstWindow()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Size = new System.Drawing.Size(300,300);
this.Text = "MyFirstWindow";
}
public static void Main(string[] args)
{
Application.Run(new MyFirstWindow());
}
}
Form.SuspendLayout
using System;
using System.Windows.Forms;
class MainForm : Form
{
private Label label1;
private TextBox textBox1;
private Button button1;
public MainForm()
{
this.label1 = new Label();
this.textBox1 = new TextBox();
this.button1 = new Button();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(16, 36);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(128, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Please enter your name:";
this.textBox1.Location = new System.Drawing.Point(152, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
this.button1.Location = new System.Drawing.Point(109, 80);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "Enter";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.ClientSize = new System.Drawing.Size(292, 126);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "form1";
this.Text = "Visual C#";
this.ResumeLayout(false);
}
private void button1_Click(object sender, System.EventArgs e)
{
System.Console.WriteLine("User entered: " + textBox1.Text);
MessageBox.Show("Welcome, " + textBox1.Text, "Visual C#");
}
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}
Form.Text
using System;
using System.Drawing;
using System.Windows.Forms;
class Scribble: Form
{
bool bTracking;
Point ptLast;
public static void Main()
{
Application.Run(new Scribble());
}
public Scribble()
{
Text = "Scribble";
}
protected override void OnMouseDown(MouseEventArgs mea)
{
if (mea.Button != MouseButtons.Left)
return;
ptLast = new Point(mea.X, mea.Y);
bTracking = true;
}
protected override void OnMouseMove(MouseEventArgs mea)
{
if (!bTracking)
return;
Point ptNew = new Point(mea.X, mea.Y);
Graphics grfx = CreateGraphics();
grfx.DrawLine(new Pen(ForeColor), ptLast, ptNew);
grfx.Dispose();
ptLast = ptNew;
}
protected override void OnMouseUp(MouseEventArgs mea)
{
bTracking = false;
}
}
Form.Top
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FormBoundsSetting : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public FormBoundsSetting()
{
Top = 100;
Left = 75;
Height = 100;
Width = 500;
MessageBox.Show(Bounds.ToString(), "Current rect");
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
[STAThread]
static void Main()
{
Application.Run(new FormBoundsSetting());
}
}
Form.TransparencyKey
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
private System.Windows.Forms.PictureBox PictureBox4;
private System.Windows.Forms.PictureBox PictureBox2;
private System.Windows.Forms.PictureBox PictureBox1;
private System.Windows.Forms.PictureBox PictureBox3;
public Form1() {
InitializeComponent();
}
private void InitializeComponent()
{
this.PictureBox4 = new System.Windows.Forms.PictureBox();
this.PictureBox2 = new System.Windows.Forms.PictureBox();
this.PictureBox1 = new System.Windows.Forms.PictureBox();
this.PictureBox3 = new System.Windows.Forms.PictureBox();
((System.ruponentModel.ISupportInitialize)(this.PictureBox4)).BeginInit();
((System.ruponentModel.ISupportInitialize)(this.PictureBox2)).BeginInit();
((System.ruponentModel.ISupportInitialize)(this.PictureBox1)).BeginInit();
((System.ruponentModel.ISupportInitialize)(this.PictureBox3)).BeginInit();
this.SuspendLayout();
//
// PictureBox4
//
this.PictureBox4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
this.PictureBox4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.PictureBox4.Location = new System.Drawing.Point(156, 141);
this.PictureBox4.Name = "PictureBox4";
this.PictureBox4.Size = new System.Drawing.Size(76, 76);
this.PictureBox4.TabIndex = 14;
this.PictureBox4.TabStop = false;
//
// PictureBox2
//
this.PictureBox2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
this.PictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.PictureBox2.Location = new System.Drawing.Point(60, 141);
this.PictureBox2.Name = "PictureBox2";
this.PictureBox2.Size = new System.Drawing.Size(76, 76);
this.PictureBox2.TabIndex = 13;
this.PictureBox2.TabStop = false;
//
// PictureBox1
//
this.PictureBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
this.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.PictureBox1.Location = new System.Drawing.Point(156, 49);
this.PictureBox1.Name = "PictureBox1";
this.PictureBox1.Size = new System.Drawing.Size(76, 76);
this.PictureBox1.TabIndex = 12;
this.PictureBox1.TabStop = false;
//
// PictureBox3
//
this.PictureBox3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
this.PictureBox3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.PictureBox3.Location = new System.Drawing.Point(60, 49);
this.PictureBox3.Name = "PictureBox3";
this.PictureBox3.Size = new System.Drawing.Size(76, 76);
this.PictureBox3.TabIndex = 11;
this.PictureBox3.TabStop = false;
//
// Holes
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.PictureBox4);
this.Controls.Add(this.PictureBox2);
this.Controls.Add(this.PictureBox1);
this.Controls.Add(this.PictureBox3);
this.Name = "Holes";
this.Text = "Holes";
this.TransparencyKey = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
((System.ruponentModel.ISupportInitialize)(this.PictureBox4)).EndInit();
((System.ruponentModel.ISupportInitialize)(this.PictureBox2)).EndInit();
((System.ruponentModel.ISupportInitialize)(this.PictureBox1)).EndInit();
((System.ruponentModel.ISupportInitialize)(this.PictureBox3)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Form.Visible
using System.Windows.Forms;
class RunFormBadly
{
public static void Main()
{
Form form = new Form();
form.Text = "Not a Good Idea...";
form.Visible = true;
Application.Run();
}
}
Form.Width
using System.Drawing;
using System.Windows.Forms;
class FormProperties
{
public static void Main()
{
Form form = new Form();
form.Width *= 2;
Application.Run(form);
}
}
new Form()
using System;
using System.Drawing;
using System.Windows.Forms;
class InstantiateHelloWorld
{
public static void Main()
{
Form form = new Form();
form.Text = "Instantiate " + form.Text;
form.Paint += new PaintEventHandler(MyPaintHandler);
Application.Run(form);
}
static void MyPaintHandler(object objSender, PaintEventArgs pea)
{
Form form = (Form)objSender;
Graphics graphics = pea.Graphics;
graphics.DrawString("Hello from InstantiateHelloWorld!",
form.Font, Brushes.Black, 0, 100);
}
}