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();
    }
  }


           
          


FormStartPosition.CenterScreen

   
 


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);
     }
}

    


Set ClientSize to change the form window size

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); } } [/csharp]

Set ResizeRedraw property

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class RandomClearResizeRedraw: Form
{
     public static void Main()      
     {
          Application.Run(new RandomClearResizeRedraw());
     }
     public RandomClearResizeRedraw()      
     {
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics graphics = pea.Graphics;
          Random   rand = new Random();
   
          graphics.Clear(Color.FromArgb(rand.Next(256),
                                    rand.Next(256),
                                    rand.Next(256)));
     }
}

    


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);
    }
}

    


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);
    }
}

    


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]