Draw image based on the window size


   


  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;

  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.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      Bitmap bmp = new Bitmap("winter.jpg");

      Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
      g.DrawImage(bmp, this.ClientRectangle);
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
          


Change the background and text colors of a form using Color Dialog


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


   public class ShowColorsComplex : System.Windows.Forms.Form
   {
      private System.Windows.Forms.Button backgroundColorButton;
      private System.Windows.Forms.Button textColorButton;

      public ShowColorsComplex()
      {
         InitializeComponent();
      }

      private void InitializeComponent()
      {
         this.backgroundColorButton = new System.Windows.Forms.Button();
         this.textColorButton = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // backgroundColorButton
         // 
         this.backgroundColorButton.Location = new System.Drawing.Point(16, 16);
         this.backgroundColorButton.Name = "backgroundColorButton";
         this.backgroundColorButton.Size = new System.Drawing.Size(264, 32);
         this.backgroundColorButton.TabIndex = 0;
         this.backgroundColorButton.Text = "Change Background Color";
         this.backgroundColorButton.Click += new System.EventHandler(this.backgroundColorButton_Click);
         // 
         // textColorButton
         // 
         this.textColorButton.Location = new System.Drawing.Point(16, 64);
         this.textColorButton.Name = "textColorButton";
         this.textColorButton.Size = new System.Drawing.Size(264, 32);
         this.textColorButton.TabIndex = 1;
         this.textColorButton.Text = "Change Text Color";
         this.textColorButton.Click += new System.EventHandler(this.textColorButton_Click);
         // 
         // ShowColorsComplex
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(292, 109);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.textColorButton,
                                                                      this.backgroundColorButton});
         this.Name = "ShowColorsComplex";
         this.Text = "ShowColorsComplex";
         this.ResumeLayout(false);

      }
      static void Main() 
      {
         Application.Run( new ShowColorsComplex() );
      }

      private void textColorButton_Click(object sender, System.EventArgs e ){
         ColorDialog colorChooser = new ColorDialog();
         DialogResult result;

         result = colorChooser.ShowDialog();

         if ( result == DialogResult.Cancel )
            return;
         
         backgroundColorButton.ForeColor = colorChooser.Color;
         textColorButton.ForeColor = colorChooser.Color;

      }
      private void backgroundColorButton_Click(object sender, System.EventArgs e ){
         ColorDialog colorChooser = new ColorDialog();
         DialogResult result;

         colorChooser.FullOpen = true;
         result = colorChooser.ShowDialog();

         if ( result == DialogResult.Cancel )
            return;
         this.BackColor = colorChooser.Color;
      }
   }



           
          


Assign Form window default value


   

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

           
          


Create Graphics Object from form window handle


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.CheckBox checkBox1;
    private System.Windows.Forms.CheckBox checkBox2;
    private System.Windows.Forms.CheckBox checkBox3;
    private System.Windows.Forms.RadioButton radioButton1;
    private System.Windows.Forms.RadioButton radioButton2;
    private System.Windows.Forms.RadioButton radioButton3;
    private System.Windows.Forms.Button button1;

    private System.ComponentModel.Container components = null;

    public Form1() {
      InitializeComponent();
    }

    private void InitializeComponent() {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.checkBox2 = new System.Windows.Forms.CheckBox();
            this.checkBox3 = new System.Windows.Forms.CheckBox();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton3 = new System.Windows.Forms.RadioButton();
            this.button1 = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    this.radioButton1,
                                                                                    this.radioButton2,
                                                                                    this.radioButton3});
            this.groupBox1.Location = new System.Drawing.Point(8, 120);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(120, 144);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Color";
            this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter);
            // 
            // checkBox1
            // 
            this.checkBox1.Location = new System.Drawing.Point(8, 8);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.TabIndex = 1;
            this.checkBox1.Text = "Circle";
            // 
            // checkBox2
            // 
            this.checkBox2.Location = new System.Drawing.Point(8, 40);
            this.checkBox2.Name = "checkBox2";
            this.checkBox2.TabIndex = 2;
            this.checkBox2.Text = "Rectangle";
            // 
            // checkBox3
            // 
            this.checkBox3.Location = new System.Drawing.Point(8, 72);
            this.checkBox3.Name = "checkBox3";
            this.checkBox3.TabIndex = 3;
            this.checkBox3.Text = "String";
            // 
            // radioButton1
            // 
            this.radioButton1.Location = new System.Drawing.Point(8, 32);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.TabIndex = 4;
            this.radioButton1.Text = "Red";
            this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // radioButton2
            // 
            this.radioButton2.Location = new System.Drawing.Point(8, 64);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.TabIndex = 5;
            this.radioButton2.Text = "Green";
            // 
            // radioButton3
            // 
            this.radioButton3.Location = new System.Drawing.Point(8, 96);
            this.radioButton3.Name = "radioButton3";
            this.radioButton3.TabIndex = 6;
            this.radioButton3.Text = "Blue";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(8, 280);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(112, 32);
            this.button1.TabIndex = 4;
            this.button1.Text = "Draw";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(408, 317);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.button1,
                                                                          this.checkBox3,
                                                                          this.checkBox2,
                                                                          this.checkBox1,
                                                                          this.groupBox1});
            this.Name = "Form1";
            this.Text = "CheckBox and RadioButton Sample";
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void groupBox1_Enter(object sender, System.EventArgs e)
        {
           Console.WriteLine("group box enter event");
        }

        private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
        {
           Console.WriteLine("Radio Button checked changed event");
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            Graphics g = Graphics.FromHwnd(this.Handle);
            String str = "";
            Rectangle rc = new Rectangle(150, 50, 250, 250);
            
            if(radioButton1.Checked)
            {
                str = "red";
            }
            if(radioButton2.Checked)
            {
                str+="Green";
            }
            if(radioButton3.Checked)
            {
                str+="Blue";
            }

            if (checkBox1.Checked)
            {
                str+="Ellipse";
            }
            if (checkBox2.Checked)
            {
                str += "Rectangle";
            }
            if (checkBox3.Checked)
            {
                g.FillRectangle(new SolidBrush(Color.White), rc);
                g.DrawString(str, new Font("Verdana", 12), new SolidBrush(Color.Black), rc);
            }
            

        }
    }


           
          


Change Form window ownership


   

using System;
using System.Collections.Generic;
using System.ComponentModel;
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 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);
     }
}