Change Form window ownership : Form Frame Window « GUI Windows Form « C# / C Sharp






Change Form window ownership

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

}


           
       








Related examples in the same category

1.Print out Form size and position related information
2.Use Font from Form to paint string on a form
3.Set ResizeRedraw property
4.Set ClientSize to change the form window size
5.FormStartPosition.CenterScreen
6.Form hideForm hide
7.Create Graphics Object from form window handleCreate Graphics Object from form window handle
8.Center form windowCenter form window
9.Assign Form window default valueAssign Form window default value
10.Change the background and text colors of a form using Color DialogChange the background and text colors of a form using Color Dialog
11.Draw image based on the window sizeDraw image based on the window size
12.Auto scroll form windowAuto scroll form window
13.Set Form window title and center the Form window on the desktopSet Form window title and center the Form window on the desktop
14.Add control to a form windowAdd control to a form window
15.Change Form window backgroundChange Form window background
16.Simplest form code: set window titleSimplest form code: set window title
17.Login fromLogin from
18.makes a new window out of a graphics path that has been traced on this forms spacemakes a new window out of a graphics path that has been traced on this forms space
19.Windows Forms Getting StartedWindows Forms Getting Started
20.A form-based Windows SkeletonA form-based Windows Skeleton
21.Simple formSimple form
22.Add controls to a formAdd controls to a form
23.Form for data inputForm for data input
24.Demonstrates creating a form in a console programDemonstrates creating a form in a console program
25.AutoScrollPosition