Csharp/C Sharp by API/System.Windows.Forms/Button
Содержание
Button.Anchor
using System;
using System.Drawing;
using System.Windows.Forms;
class TwoButtonsAnchor: Form
{
public static void Main()
{
Application.Run(new TwoButtonsAnchor());
}
public TwoButtonsAnchor()
{
ResizeRedraw = true;
int cxBtn = 5 * Font.Height;
int cyBtn = 2 * Font.Height;
int dxBtn = Font.Height;
Button btn = new Button();
btn.Parent = this;
btn.Text = "&Larger";
btn.Location = new Point(dxBtn, dxBtn);
btn.Size = new Size(cxBtn, cyBtn);
btn.Click += new EventHandler(ButtonLargerOnClick);
btn = new Button();
btn.Parent = this;
btn.Text = "&Smaller";
btn.Location = new Point(ClientSize.Width - cxBtn - dxBtn,
ClientSize.Height - cyBtn - dxBtn);
btn.Size = new Size(cxBtn, cyBtn);
btn.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
btn.Click += new EventHandler(ButtonSmallerOnClick);
}
void ButtonLargerOnClick(object obj, EventArgs ea)
{
Console.WriteLine("large");
}
void ButtonSmallerOnClick(object obj, EventArgs ea)
{
Console.WriteLine("small");
}
}
Button.Click
using System;
using System.Drawing;
using System.Windows.Forms;
public class BaseForm : System.Windows.Forms.Form
{
private Button btnClose;
private Button btnApp;
protected Label lbl;
public BaseForm()
{
btnClose = new Button();
btnClose.Location = new Point(25,100);
btnClose.Size = new Size(100,25);
btnClose.Text = "&Close";
btnClose.Click += new System.EventHandler(btnClose_Click);
btnApp = new Button();
btnApp.Location = new Point(200,100);
btnApp.Size = new Size(150,25);
btnApp.Text = "&Base Application";
btnApp.Click += new System.EventHandler(btnApp_Click);
lbl = new Label();
lbl.Location = new Point(25,25);
lbl.Size = new Size(100,25);
lbl.Text = "This label on BaseForm";
Controls.AddRange(new Control[]{lbl, btnClose, btnApp});
}
static void Main()
{
Application.Run(new BaseForm());
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnApp_Click(object sender, EventArgs e)
{
MessageBox.Show("This is the Base application.");
SomeMethod();
}
protected virtual void SomeMethod()
{
MessageBox.Show("This is SomeMethod called from BaseForm.");
}
}
public class InheritedForm : BaseForm
{
private Button btn;
public InheritedForm()
{
btn = new Button();
btn.Location = new Point(25,150);
btn.Size = new Size(125,25);
btn.Text = "C&lose on Inherited";
btn.Click += new System.EventHandler(btn_Click);
Controls.Add(btn);
lbl.Text = "Now from InheritedForm";
BackColor = Color.LightBlue;
}
static void Main()
{
Application.Run(new InheritedForm());
}
private void btn_Click(object sender, EventArgs e)
{
Application.Exit();
}
protected override void SomeMethod()
{
MessageBox.Show("This is the overridden SomeMethod called " +
"from InheritedForm.");
}
}
Button.DialogResult
using System;
using System.Drawing;
using System.Windows.Forms;
class SimplerDialog: Form
{
public static void Main()
{
Application.Run(new SimplerDialog());
}
public SimplerDialog()
{
Text = "Simpler Dialog";
Menu = new MainMenu();
Menu.MenuItems.Add("&Dialog!", new EventHandler(MenuOnClick));
}
void MenuOnClick(object obj, EventArgs ea)
{
SimplerDialogBox dlg = new SimplerDialogBox();
DialogResult dr = dlg.ShowDialog();
Console.WriteLine(dr);
}
}
class SimplerDialogBox: Form
{
public SimplerDialogBox()
{
Text = "Simpler 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.DialogResult = DialogResult.OK;
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;
}
}
Button.Dock
using System;
using System.Drawing;
using System.Windows.Forms;
class TwoButtonsDock: Form
{
public static void Main()
{
Application.Run(new TwoButtonsDock());
}
public TwoButtonsDock()
{
ResizeRedraw = true;
Button btn = new Button();
btn.Parent = this;
btn.Text = "&Larger";
btn.Height = 2 * Font.Height;
btn.Dock = DockStyle.Top;
btn.Click += new EventHandler(ButtonLargerOnClick);
btn = new Button();
btn.Parent = this;
btn.Text = "&Smaller";
btn.Height = 2 * Font.Height;
btn.Dock = DockStyle.Bottom;
btn.Click += new EventHandler(ButtonSmallerOnClick);
}
void ButtonLargerOnClick(object obj, EventArgs ea)
{
Console.WriteLine("large");
}
void ButtonSmallerOnClick(object obj, EventArgs ea)
{
Console.WriteLine("small");
}
}
Button.FlatStyle
using System;
using System.Drawing;
using System.Windows.Forms;
class ButtonStyles: Form
{
public static void Main()
{
Application.Run(new ButtonStyles());
}
public ButtonStyles()
{
int y = 0;
foreach (FlatStyle fs in Enum.GetValues(typeof(FlatStyle)))
{
Button btn = new Button();
btn.Parent = this;
btn.FlatStyle = fs;
btn.Text = fs.ToString();
btn.Location = new Point(50, y += 50);
}
}
}
Button.Font
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class ButtonForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnImage;
private System.Windows.Forms.Button btnStandard;
private System.Windows.Forms.Button btnPopup;
private System.Windows.Forms.Button btnFlat;
// Hold the current text alignment
ContentAlignment currAlignment = ContentAlignment.MiddleCenter;
int currEnumPos = 0;
public ButtonForm()
{
InitializeComponent();
// Set btnStandard as default accept.
this.AcceptButton = btnStandard;
CenterToScreen();
}
private void InitializeComponent()
{
this.btnStandard = new System.Windows.Forms.Button();
this.btnFlat = new System.Windows.Forms.Button();
this.btnImage = new System.Windows.Forms.Button();
this.btnPopup = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnStandard
//
this.btnStandard.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
this.btnStandard.ForeColor = System.Drawing.SystemColors.ControlText;
this.btnStandard.Location = new System.Drawing.Point(16, 80);
this.btnStandard.Name = "btnStandard";
this.btnStandard.Size = new System.Drawing.Size(312, 88);
this.btnStandard.TabIndex = 2;
this.btnStandard.Text = "I am a standard button";
this.btnStandard.Click += new System.EventHandler(this.btnStandard_Click);
//
// btnFlat
//
this.btnFlat.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnFlat.ForeColor = System.Drawing.Color.Blue;
this.btnFlat.Location = new System.Drawing.Point(16, 24);
this.btnFlat.Name = "btnFlat";
this.btnFlat.Size = new System.Drawing.Size(152, 32);
this.btnFlat.TabIndex = 0;
this.btnFlat.Text = "I am flat...";
//
// btnImage
//
this.btnImage.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Bold);
this.btnImage.Image = new Bitmap("winter.jpg");
this.btnImage.Location = new System.Drawing.Point(16, 192);
this.btnImage.Name = "btnImage";
this.btnImage.Size = new System.Drawing.Size(312, 72);
this.btnImage.TabIndex = 3;
this.btnImage.Text = "Image Button";
this.btnImage.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// btnPopup
//
this.btnPopup.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnPopup.ForeColor = System.Drawing.SystemColors.ControlText;
this.btnPopup.Location = new System.Drawing.Point(176, 24);
this.btnPopup.Name = "btnPopup";
this.btnPopup.Size = new System.Drawing.Size(152, 32);
this.btnPopup.TabIndex = 1;
this.btnPopup.Text = "I am a Popup!";
//
// ButtonForm
//
this.AcceptButton = this.btnStandard;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(340, 269);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnImage,
this.btnStandard,
this.btnPopup,
this.btnFlat});
this.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "ButtonForm";
this.Text = "Buttons";
this.ResumeLayout(false);
}
protected void btnStandard_Click (object sender, System.EventArgs e)
{
Array values = Enum.GetValues(currAlignment.GetType());
currEnumPos++;
if(currEnumPos >= values.Length)
currEnumPos = 0;
currAlignment = (ContentAlignment)ContentAlignment.Parse(currAlignment.GetType(),
values.GetValue(currEnumPos).ToString());
btnStandard.TextAlign = currAlignment;
btnStandard.Text = currAlignment.ToString();
}
public static void Main(string[] args)
{
Application.Run(new ButtonForm());
}
}
Button.Image
using System;
using System.Drawing;
using System.Windows.Forms;
class BitmapButtons: Form
{
int cxBtn, cyBtn, dxBtn;
Button btnLarger, btnSmaller;
public static void Main()
{
Application.Run(new BitmapButtons());
}
public BitmapButtons()
{
ResizeRedraw = true;
dxBtn = Font.Height;
btnLarger = new Button();
btnLarger.Parent = this;
btnLarger.Image = new Bitmap(GetType(), "LargerButton.bmp") ;
cxBtn = btnLarger.Image.Width + 8;
cyBtn = btnLarger.Image.Height + 8;
btnLarger.Size = new Size(cxBtn, cyBtn);
btnLarger.Click += new EventHandler(ButtonLargerOnClick);
btnSmaller = new Button();
btnSmaller.Parent = this;
btnSmaller.Image = new Bitmap(GetType(), "SmallerButton.bmp");
btnSmaller.Size = new Size(cxBtn, cyBtn);
btnSmaller.Click += new EventHandler(ButtonSmallerOnClick);
OnResize(EventArgs.Empty);
}
protected override void OnResize(EventArgs ea)
{
base.OnResize(ea);
btnLarger.Location = new Point(ClientSize.Width / 2 - cxBtn - dxBtn / 2,
(ClientSize.Height - cyBtn) / 2);
btnSmaller.Location = new Point(ClientSize.Width / 2 + dxBtn / 2,
(ClientSize.Height - cyBtn) / 2);
}
void ButtonLargerOnClick(object obj, EventArgs ea)
{
Left = 50;
Top = 50;
Width = 50;
Height = 50;
}
void ButtonSmallerOnClick(object obj, EventArgs ea)
{
Left = 200;
Top = 200;
Width = 20;
Height = 20;
}
}
Button.Location
using System;
using System.Drawing;
using System.Windows.Forms;
public class ControlDynamicSizeLocation : Form
{
private Button btnShow = new Button();
private Label lbl = new Label();
int xButtonSize, yButtonSize;
public ControlDynamicSizeLocation()
{
btnShow.Parent = this;
btnShow.Text = "Show Button Properties";
Size = new Size(400,400);
xButtonSize = (int)(Font.Height * .75) * btnShow.Text.Length;
yButtonSize = Font.Height * 2;
btnShow.Size = new Size(xButtonSize, yButtonSize);
btnShow.Click += new System.EventHandler(btnShow_Click);
lbl.Text = "Control Size and Location - Dynamic";
lbl.AutoSize = true;
lbl.Parent = this;
OnResize(EventArgs.Empty);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
int xPosition = (int)(this.ClientSize.Width / 2) - (int)(xButtonSize / 2);
int yPosition = (int)(this.ClientSize.Height / 2) - (int)(yButtonSize / 2);
btnShow.Location = new Point(xPosition, yPosition);
}
static void Main()
{
Application.Run(new ControlDynamicSizeLocation());
}
private void btnShow_Click(object sender, EventArgs e)
{
Console.WriteLine("Button Bottom:" + btnShow.Bottom.ToString());
Console.WriteLine("Button Top:" + btnShow.Top.ToString() );
Console.WriteLine("Button Left:" + btnShow.Left.ToString() );
Console.WriteLine("Button Right:" + btnShow.Right.ToString() );
Console.WriteLine("Button Location:" + btnShow.Location.ToString() );
Console.WriteLine("Button Width:" + btnShow.Width.ToString() );
Console.WriteLine("Button Height:" + btnShow.Height.ToString() );
Console.WriteLine("Button Size:" + btnShow.Size.ToString() );
Console.WriteLine("Button ClientSize:" + btnShow.ClientSize.ToString() );
Console.WriteLine("Font:" + btnShow.Font.ToString());
}
}
Button.MouseDown
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.GroupBox GroupBox1;
private System.Windows.Forms.Label Label4;
private System.Windows.Forms.Label Label1;
private System.Windows.Forms.PictureBox pic;
private System.Windows.Forms.TextBox txt;
private System.Windows.Forms.Button cmd;
private System.Windows.Forms.Label Label2;
private System.Windows.Forms.Label Label3;
private System.Windows.Forms.ListBox eventLogList;
public Form1() {
InitializeComponent();
}
private void Log(String data)
{
eventLogList.Items.Add(data);
int itemsPerPage = (int)(eventLogList.Height / eventLogList.ItemHeight);
eventLogList.TopIndex = eventLogList.Items.Count - itemsPerPage;
}
private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
Log("Key Press: " + e.KeyChar.ToString());
}
private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
}
private void txt_TextChanged(object sender, System.EventArgs e)
{
Log("Changed: " + " Text is: " + txt.Text);
}
private void pic_MouseEnter(object sender, System.EventArgs e)
{
Log("Mouse Enter");
}
private void pic_MouseHover(object sender, System.EventArgs e)
{
Log("Mouse Hover");
}
private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Down: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Up: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_Click(object sender, System.EventArgs e)
{
Log("Click");
}
private void pic_DoubleClick(object sender, System.EventArgs e)
{
Log("Double Click");
}
private void pic_MouseLeave(object sender, System.EventArgs e)
{
Log("Mouse Leave");
}
private void InitializeComponent()
{
this.GroupBox1 = new System.Windows.Forms.GroupBox();
this.Label4 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.pic = new System.Windows.Forms.PictureBox();
this.txt = new System.Windows.Forms.TextBox();
this.cmd = new System.Windows.Forms.Button();
this.Label2 = new System.Windows.Forms.Label();
this.Label3 = new System.Windows.Forms.Label();
this.eventLogList = new System.Windows.Forms.ListBox();
this.GroupBox1.SuspendLayout();
((System.ruponentModel.ISupportInitialize)(this.pic)).BeginInit();
this.SuspendLayout();
//
// GroupBox1
//
this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.GroupBox1.Controls.Add(this.Label4);
this.GroupBox1.Controls.Add(this.Label1);
this.GroupBox1.Controls.Add(this.pic);
this.GroupBox1.Controls.Add(this.txt);
this.GroupBox1.Controls.Add(this.cmd);
this.GroupBox1.Controls.Add(this.Label2);
this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.GroupBox1.Location = new System.Drawing.Point(7, 0);
this.GroupBox1.Name = "GroupBox1";
this.GroupBox1.Size = new System.Drawing.Size(384, 148);
this.GroupBox1.TabIndex = 12;
this.GroupBox1.TabStop = false;
//
// Label4
//
this.Label4.Location = new System.Drawing.Point(92, 108);
this.Label4.Name = "Label4";
this.Label4.Size = new System.Drawing.Size(56, 16);
this.Label4.TabIndex = 5;
this.Label4.Text = "And here:";
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(6, 24);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(144, 16);
this.Label1.TabIndex = 2;
this.Label1.Text = "Test keyboard events here:";
//
// pic
//
this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pic.Location = new System.Drawing.Point(156, 48);
this.pic.Name = "pic";
this.pic.Size = new System.Drawing.Size(192, 48);
this.pic.TabIndex = 3;
this.pic.TabStop = false;
this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
this.pic.Click += new System.EventHandler(this.pic_Click);
this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// txt
//
this.txt.Location = new System.Drawing.Point(156, 20);
this.txt.Name = "txt";
this.txt.Size = new System.Drawing.Size(192, 21);
this.txt.TabIndex = 1;
this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
//
// cmd
//
this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cmd.Location = new System.Drawing.Point(156, 100);
this.cmd.Name = "cmd";
this.cmd.Size = new System.Drawing.Size(88, 28);
this.cmd.TabIndex = 4;
this.cmd.Text = "Button1";
this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
this.cmd.Click += new System.EventHandler(this.pic_Click);
this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// Label2
//
this.Label2.Location = new System.Drawing.Point(20, 52);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(128, 16);
this.Label2.TabIndex = 2;
this.Label2.Text = "Test mouse events here:";
//
// Label3
//
this.Label3.Location = new System.Drawing.Point(23, 100);
this.Label3.Name = "Label3";
this.Label3.Size = new System.Drawing.Size(64, 24);
this.Label3.TabIndex = 11;
this.Label3.Text = "Label3";
//
// eventLogList
//
this.eventLogList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.eventLogList.FormattingEnabled = true;
this.eventLogList.IntegralHeight = false;
this.eventLogList.Location = new System.Drawing.Point(7, 156);
this.eventLogList.Name = "eventLogList";
this.eventLogList.Size = new System.Drawing.Size(384, 212);
this.eventLogList.TabIndex = 10;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(399, 374);
this.Controls.Add(this.GroupBox1);
this.Controls.Add(this.Label3);
this.Controls.Add(this.eventLogList);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "Form1";
this.Text = "Event Tracker";
this.GroupBox1.ResumeLayout(false);
this.GroupBox1.PerformLayout();
((System.ruponentModel.ISupportInitialize)(this.pic)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Button.MouseEnter
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.GroupBox GroupBox1;
private System.Windows.Forms.Label Label4;
private System.Windows.Forms.Label Label1;
private System.Windows.Forms.PictureBox pic;
private System.Windows.Forms.TextBox txt;
private System.Windows.Forms.Button cmd;
private System.Windows.Forms.Label Label2;
private System.Windows.Forms.Label Label3;
private System.Windows.Forms.ListBox eventLogList;
public Form1() {
InitializeComponent();
}
private void Log(String data)
{
eventLogList.Items.Add(data);
int itemsPerPage = (int)(eventLogList.Height / eventLogList.ItemHeight);
eventLogList.TopIndex = eventLogList.Items.Count - itemsPerPage;
}
private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
Log("Key Press: " + e.KeyChar.ToString());
}
private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
}
private void txt_TextChanged(object sender, System.EventArgs e)
{
Log("Changed: " + " Text is: " + txt.Text);
}
private void pic_MouseEnter(object sender, System.EventArgs e)
{
Log("Mouse Enter");
}
private void pic_MouseHover(object sender, System.EventArgs e)
{
Log("Mouse Hover");
}
private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Down: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Log("Mouse Up: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
}
private void pic_Click(object sender, System.EventArgs e)
{
Log("Click");
}
private void pic_DoubleClick(object sender, System.EventArgs e)
{
Log("Double Click");
}
private void pic_MouseLeave(object sender, System.EventArgs e)
{
Log("Mouse Leave");
}
private void InitializeComponent()
{
this.GroupBox1 = new System.Windows.Forms.GroupBox();
this.Label4 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.pic = new System.Windows.Forms.PictureBox();
this.txt = new System.Windows.Forms.TextBox();
this.cmd = new System.Windows.Forms.Button();
this.Label2 = new System.Windows.Forms.Label();
this.Label3 = new System.Windows.Forms.Label();
this.eventLogList = new System.Windows.Forms.ListBox();
this.GroupBox1.SuspendLayout();
((System.ruponentModel.ISupportInitialize)(this.pic)).BeginInit();
this.SuspendLayout();
//
// GroupBox1
//
this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.GroupBox1.Controls.Add(this.Label4);
this.GroupBox1.Controls.Add(this.Label1);
this.GroupBox1.Controls.Add(this.pic);
this.GroupBox1.Controls.Add(this.txt);
this.GroupBox1.Controls.Add(this.cmd);
this.GroupBox1.Controls.Add(this.Label2);
this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.GroupBox1.Location = new System.Drawing.Point(7, 0);
this.GroupBox1.Name = "GroupBox1";
this.GroupBox1.Size = new System.Drawing.Size(384, 148);
this.GroupBox1.TabIndex = 12;
this.GroupBox1.TabStop = false;
//
// Label4
//
this.Label4.Location = new System.Drawing.Point(92, 108);
this.Label4.Name = "Label4";
this.Label4.Size = new System.Drawing.Size(56, 16);
this.Label4.TabIndex = 5;
this.Label4.Text = "And here:";
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(6, 24);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(144, 16);
this.Label1.TabIndex = 2;
this.Label1.Text = "Test keyboard events here:";
//
// pic
//
this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pic.Location = new System.Drawing.Point(156, 48);
this.pic.Name = "pic";
this.pic.Size = new System.Drawing.Size(192, 48);
this.pic.TabIndex = 3;
this.pic.TabStop = false;
this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
this.pic.Click += new System.EventHandler(this.pic_Click);
this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// txt
//
this.txt.Location = new System.Drawing.Point(156, 20);
this.txt.Name = "txt";
this.txt.Size = new System.Drawing.Size(192, 21);
this.txt.TabIndex = 1;
this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
//
// cmd
//
this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cmd.Location = new System.Drawing.Point(156, 100);
this.cmd.Name = "cmd";
this.cmd.Size = new System.Drawing.Size(88, 28);
this.cmd.TabIndex = 4;
this.cmd.Text = "Button1";
this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
this.cmd.Click += new System.EventHandler(this.pic_Click);
this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
//
// Label2
//
this.Label2.Location = new System.Drawing.Point(20, 52);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(128, 16);
this.Label2.TabIndex = 2;
this.Label2.Text = "Test mouse events here:";
//
// Label3
//
this.Label3.Location = new System.Drawing.Point(23, 100);
this.Label3.Name = "Label3";
this.Label3.Size = new System.Drawing.Size(64, 24);
this.Label3.TabIndex = 11;
this.Label3.Text = "Label3";
//
// eventLogList
//
this.eventLogList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.eventLogList.FormattingEnabled = true;
this.eventLogList.IntegralHeight = false;
this.eventLogList.Location = new System.Drawing.Point(7, 156);
this.eventLogList.Name = "eventLogList";
this.eventLogList.Size = new System.Drawing.Size(384, 212);
this.eventLogList.TabIndex = 10;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(399, 374);
this.Controls.Add(this.GroupBox1);
this.Controls.Add(this.Label3);
this.Controls.Add(this.eventLogList);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "Form1";
this.Text = "Event Tracker";
this.GroupBox1.ResumeLayout(false);
this.GroupBox1.PerformLayout();
((System.ruponentModel.ISupportInitialize)(this.pic)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Button.Parent
using System;
using System.Drawing;
using System.Windows.Forms;
class TwoButtons: Form
{
readonly Button btnLarger, btnSmaller;
readonly int cxBtn, cyBtn, dxBtn;
public static void Main()
{
Application.Run(new TwoButtons());
}
public TwoButtons()
{
ResizeRedraw = true;
cxBtn = 200;
cyBtn = 100;
dxBtn = 12;
btnLarger = new Button();
btnLarger.Parent = this;
btnLarger.Text = "&Larger";
btnLarger.Size = new Size(cxBtn, cyBtn);
btnLarger.Click += new EventHandler(ButtonOnClick);
btnSmaller = new Button();
btnSmaller.Parent = this;
btnSmaller.Text = "&Smaller";
btnSmaller.Size = new Size(cxBtn, cyBtn);
btnSmaller.Click += new EventHandler(ButtonOnClick);
OnResize(EventArgs.Empty);
}
protected override void OnResize(EventArgs ea)
{
base.OnResize(ea);
btnLarger.Location =
new Point(ClientSize.Width / 2 - cxBtn - dxBtn / 2,
(ClientSize.Height - cyBtn) / 2);
btnSmaller.Location =
new Point(ClientSize.Width / 2 + dxBtn / 2,
(ClientSize.Height - cyBtn) / 2);
}
void ButtonOnClick(object obj, EventArgs ea)
{
Button btn = (Button) obj;
if (btn == btnLarger)
{
Left -= (int)(0.1 * Width);
Top -= (int)(0.1 * Height);
Width += (int)(0.1 * Width);
Height += (int)(0.1 * Height);
}
else
{
Left += (int)(Width / 22f);
Top += (int)(Height / 22f);
Width -= (int)(Width / 11f);
Height -= (int)(Height / 11f);
}
}
}
Button.PerformClick()
using System;
using System.Drawing;
using System.Windows.Forms;
public class ButtonEvent : Form
{
Button btn1;
Button btn2;
public ButtonEvent()
{
Size = new Size(200,100);
btn1 = new Button();
btn1.Parent = this;
btn1.Text = "Button1";
btn1.Location = new Point(10,10);
btn1.Click += new System.EventHandler(btn1_Click);
btn2 = new Button();
btn2.Parent = this;
btn2.Text = "Button2";
btn2.Location = new Point(100,10);
btn2.Click += new System.EventHandler(btn2_Click);
}
static void Main()
{
Application.Run(new ButtonEvent());
}
private void btn1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button1 clicked.");
btn2.PerformClick();
}
private void btn2_Click(object sender, EventArgs e)
{
MessageBox.Show("Button2 clicked.");
}
}
Button.Size
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.Button button1;
public Form1() {
InitializeComponent();
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Image = new Bitmap("winter.jpg");
this.button1.ImageAlign = System.Drawing.ContentAlignment.TopRight;
this.button1.Location = new System.Drawing.Point(12, 99);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(132, 74);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button1.UseVisualStyleBackColor = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(299, 271);
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 = "ImagesInCommonControls";
this.Text = "ImagesInCommonControls";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Button.Text
using System;
using System.Drawing;
using System.Windows.Forms;
public class MainForm : Form
{
public static void Main()
{
MainForm MyForm = new MainForm();
Application.Run(MyForm);
}
public MainForm()
{
Button MyButton = new Button();
Text = "Button Test";
MyButton.Location = new Point(25, 25);
MyButton.Text = "Click Me";
MyButton.Click += new EventHandler(MyButtonClicked);
Controls.Add(MyButton);
}
public void MyButtonClicked(object sender, EventArgs Arguments)
{
MessageBox.Show("The button has been clicked.");
}
}
Button.TextAlign
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class ButtonForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnImage;
private System.Windows.Forms.Button btnStandard;
private System.Windows.Forms.Button btnPopup;
private System.Windows.Forms.Button btnFlat;
// Hold the current text alignment
ContentAlignment currAlignment = ContentAlignment.MiddleCenter;
int currEnumPos = 0;
public ButtonForm()
{
InitializeComponent();
// Set btnStandard as default accept.
this.AcceptButton = btnStandard;
CenterToScreen();
}
private void InitializeComponent()
{
this.btnStandard = new System.Windows.Forms.Button();
this.btnFlat = new System.Windows.Forms.Button();
this.btnImage = new System.Windows.Forms.Button();
this.btnPopup = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnStandard
//
this.btnStandard.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
this.btnStandard.ForeColor = System.Drawing.SystemColors.ControlText;
this.btnStandard.Location = new System.Drawing.Point(16, 80);
this.btnStandard.Name = "btnStandard";
this.btnStandard.Size = new System.Drawing.Size(312, 88);
this.btnStandard.TabIndex = 2;
this.btnStandard.Text = "I am a standard button";
this.btnStandard.Click += new System.EventHandler(this.btnStandard_Click);
//
// btnFlat
//
this.btnFlat.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnFlat.ForeColor = System.Drawing.Color.Blue;
this.btnFlat.Location = new System.Drawing.Point(16, 24);
this.btnFlat.Name = "btnFlat";
this.btnFlat.Size = new System.Drawing.Size(152, 32);
this.btnFlat.TabIndex = 0;
this.btnFlat.Text = "I am flat...";
//
// btnImage
//
this.btnImage.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Bold);
this.btnImage.Image = new Bitmap("winter.jpg");
this.btnImage.Location = new System.Drawing.Point(16, 192);
this.btnImage.Name = "btnImage";
this.btnImage.Size = new System.Drawing.Size(312, 72);
this.btnImage.TabIndex = 3;
this.btnImage.Text = "Image Button";
this.btnImage.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// btnPopup
//
this.btnPopup.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnPopup.ForeColor = System.Drawing.SystemColors.ControlText;
this.btnPopup.Location = new System.Drawing.Point(176, 24);
this.btnPopup.Name = "btnPopup";
this.btnPopup.Size = new System.Drawing.Size(152, 32);
this.btnPopup.TabIndex = 1;
this.btnPopup.Text = "I am a Popup!";
//
// ButtonForm
//
this.AcceptButton = this.btnStandard;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(340, 269);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnImage,
this.btnStandard,
this.btnPopup,
this.btnFlat});
this.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "ButtonForm";
this.Text = "Buttons";
this.ResumeLayout(false);
}
protected void btnStandard_Click (object sender, System.EventArgs e)
{
Array values = Enum.GetValues(currAlignment.GetType());
currEnumPos++;
if(currEnumPos >= values.Length)
currEnumPos = 0;
currAlignment = (ContentAlignment)ContentAlignment.Parse(currAlignment.GetType(),
values.GetValue(currEnumPos).ToString());
btnStandard.TextAlign = currAlignment;
btnStandard.Text = currAlignment.ToString();
}
public static void Main(string[] args)
{
Application.Run(new ButtonForm());
}
}
Button.Top
using System;
using System.Windows.Forms;
using System.Drawing;
public class PushMe2 : Form {
Button pushMeButton;
public PushMe2() {
pushMeButton = new Button();
pushMeButton.Text = "Push Me";
pushMeButton.Height = 60;
pushMeButton.Width = 80;
pushMeButton.Top = 60;
pushMeButton.Left = 60;
pushMeButton.Click += new EventHandler(ButtonClicked);
this.Controls.Add(pushMeButton);
this.Height = 200;
this.Width = 200;
this.StartPosition = FormStartPosition.CenterScreen;
this.Visible = true;
}
public void ButtonClicked(object source, EventArgs e) {
Button b = (Button)source;
if ( b.Text == "Push Me" ) {
b.Text = "Ouch";
}
else {
b.Text = "Push Me";
}
}
static void Main() {
Application.Run(new PushMe2());
}
}
new Button()
using System;
using System.Drawing;
using System.Windows.Forms;
class FormHand : Form
{
private TextBox firstNameBox = new TextBox();
private Button btnShowControls = new Button();
public FormHand()
{
firstNameBox.Text = "Text";
firstNameBox.Size = new Size(150, 50);
firstNameBox.Location = new Point(10, 10);
this.Controls.Add(firstNameBox);
btnShowControls.Text = "Click Me";
btnShowControls.Size = new Size(90, 90);
btnShowControls.Location = new Point(10, 70);
btnShowControls.Click += new EventHandler(btnShowControls_Clicked);
this.Controls.Add(btnShowControls);
CenterToScreen();
}
protected void btnShowControls_Clicked(object sender, EventArgs e)
{
Control.ControlCollection coll = this.Controls;
foreach(Control c in coll)
{
if(c != null)
Console.WriteLine(string.Format("Index: {0}, Text: {1}\n", coll.GetChildIndex(c, false), c.Text));
}
MessageBox.Show("Message", "Index and Text values for each control");
}
public static int Main(string[] args)
{
Application.Run(new FormHand());
return 0;
}
}