.NET Forms are CLASSES!

This one comes up so often I thought it would be a good idea to just illustrate it: "I can't access my other form from my new form". Forms are classes. You need an instance of the class to access its fields and methods.

This one comes up so often I thought it would be a good idea to just illustrate it:
"I can't access my other form from my new form". Forms are classes. You need an instance of the class to access its fields and methods.

Consider the following C# code:

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

namespace MultiForm
{

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(32, 208);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 32);
this.button1.TabIndex = 0;
this.button1.Text = "Access Form2";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.Location = new System.Drawing.Point(32, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(240, 40);
this.label1.TabIndex = 1;
this.label1.Text = "";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label1});
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}

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

private void button1_Click(object sender, System.EventArgs e)
{
// need object reference here to access the other class and its fields!
System.Windows.Forms.Form frm2 = new Form2();
frm2.Visible =true;
frm2.Controls[0].Text ="Got Me From Form1 !";

this.Visible=true; // keep Form1 visible also
}

private void Form1_Load(object sender, System.EventArgs e)
{

}
}

public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;

public Form2()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(32, 208);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 32);
this.button1.TabIndex = 0;
this.button1.Text = "Access Form1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.Location = new System.Drawing.Point(32, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(240, 40);
this.label1.TabIndex = 1;
this.label1.Text = "";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label1});
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1});
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
}

private void button1_Click(object sender, System.EventArgs e)
{
Form1.ActiveForm.Visible=true;
Form2.ActiveForm.Visible=false;
Form1.ActiveForm.Controls[0].Text="Got me from Form2 !";
}

private void Form2_Load(object sender, System.EventArgs e)
{
}
}
}


If you save the above code as "multiform.cs" and run CSC multiform.cs,
you'll get a .NET executable that illustrates the concept.



Submission Date:  9/23/2005 3:24:49 PM
Submitted By:  Peter Bromberg
My Home Page:  http://www.eggheadcafe.com


By Peter Bromberg   Popularity  (475 Views)