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); } }
Author: coder
Form hide
using System; using System.Collections.Generic; using System.ComponentModel; 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 Key Press action
using System; using System.Drawing; using System.Collections; using System.ComponentModel; 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.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress); } static void Main() { Application.Run(new Form1()); } private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { Console.WriteLine("Key Press"); } }
Form Paint event
using System; using System.Drawing; using System.Windows.Forms; class PaintEvent { public static void Main() { Form form = new Form(); form.Text = "Paint Event"; form.Paint += new PaintEventHandler(MyPaintHandler); Application.Run(form); } static void MyPaintHandler(object objSender, PaintEventArgs pea) { Graphics graphics = pea.Graphics; graphics.Clear(Color.Chocolate); } }
Scroll Shapes
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
private Point rectangleTopLeft = new Point(0, 0);
private Size rectangleSize = new Size(200, 200);
private Point ellipseTopLeft = new Point(50, 200);
private Size ellipseSize = new Size(200, 150);
private Pen bluePen = new Pen(Color.Blue, 3);
private Pen redPen = new Pen(Color.Red, 2);
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
Graphics dc = e.Graphics;
Size scrollOffset = new Size(this.AutoScrollPosition);
if (e.ClipRectangle.Top + scrollOffset.Width < 350 ||
e.ClipRectangle.Left + scrollOffset.Height < 250) {
Rectangle rectangleArea = new Rectangle
(rectangleTopLeft + scrollOffset, rectangleSize);
Rectangle ellipseArea = new Rectangle
(ellipseTopLeft + scrollOffset, ellipseSize);
dc.DrawRectangle(bluePen, rectangleArea);
dc.DrawEllipse(redPen, ellipseArea);
}
}
public Form1() {
this.BackColor = Color.White;
this.Size = new System.Drawing.Size(300, 300);
this.AutoScrollMinSize = new Size(250, 350);
}
public static void Main() {
Application.Run(new Form1());
}
}
[/csharp]
Print out Form size and position related information
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 + " " + "Size: " + Size + " " + "Bounds: " + Bounds + " " + "Width: " + Width + " " + "Height: " + Height + " " + "Left: " + Left + " " + "Top: " + Top + " " + "Right: " + Right + " " + "Bottom: " + Bottom + " " + "DesktopLocation: " + DesktopLocation + " " + "DesktopBounds: " + DesktopBounds + " " + "ClientSize: " + ClientSize + " " + "ClientRectangle: " + ClientRectangle; graphics.DrawString(str, Font, Brushes.Black, 0, 0); } }
Use Font from Form to paint string on a form
using System; using System.Drawing; using System.Windows.Forms; class InheritHelloWorld : Form { public static void Main() { Application.Run(new InheritHelloWorld()); } public InheritHelloWorld() { Text = "Inherit " + Text; } protected override void OnPaint(PaintEventArgs pea) { Graphics graphics = pea.Graphics; graphics.DrawString("Hello from InheritHelloWorld!", Font, Brushes.Black, 0, 100); } }