Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Events
Message
From
14/11/2004 19:02:10
 
General information
Forum:
ASP.NET
Category:
Forms
Title:
Re: Events
Miscellaneous
Thread ID:
00961252
Message ID:
00961286
Views:
6
Stephane,

That certainly works, but another way to do it is to have the interface define the Form rather than the Controls. This is actually what I was thinking of when I mentioned using an Interface. Which way is better? Dunno, haven't given it a whole lot of thought at this point. But, here's what I mean:
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;

	namespace WindowsApplication1
	{
		public class _EditBox : System.Windows.Forms.TextBox
		{
			IDataAwareForm oForm = null;

			public _EditBox()
			{
				this.Multiline = true;
				this.TextChanged += new System.EventHandler(this._TextChanged);
				this.HandleCreated += new EventHandler(this._HandleCreated);
			}

			// There is no Load event, so this is the next best thing.
			// This code will not work if put in the constructor, because
			// the parent has not added it to it's control collection at that point.
			internal void _HandleCreated(object sender, EventArgs e)
			{
				Control oParent = this.Parent;
				while (oParent != null && !(oParent is IDataAwareForm))
				oParent = oParent.Parent;
				if (oParent is IDataAwareForm)
				this.oForm = (IDataAwareForm)oParent;
			}
			internal void _TextChanged(object sender, EventArgs e)
			{
				if (this.oForm != null)
					this.oForm.OnDataChange(this, new EventArgs());
			}

		}

		public interface IDataAwareForm
		{
			event EventHandler DataChange;
			void OnDataChange(object sender, EventArgs e);
		}

		public class Form6 : System.Windows.Forms.Form, IDataAwareForm
		{
			private _EditBox _EditBox1;
			private _EditBox _EditBox2;
			private Label _Label1;

			private System.ComponentModel.Container components = null;

			public Form6()
			{
				InitializeComponent();
			}

			protected override void Dispose( bool disposing )
			{
				if( disposing )
				{
					if(components != null)
					{
						components.Dispose();
					}
				}
				base.Dispose( disposing );
			}

			private void InitializeComponent()
			{
				this._EditBox1 = new _EditBox();
				this._EditBox2 = new _EditBox();
				this._Label1 = new Label();
				this.SuspendLayout();
				this._EditBox1.Location = new System.Drawing.Point(88, 40);
				this._EditBox1.Multiline = true;
				this._EditBox1.Name = "_EditBox1";
				this._EditBox1.TabIndex = 0;
				this._EditBox1.Text = "_EditBox1";
				this._EditBox2.Location = new System.Drawing.Point(88, 72);
				this._EditBox2.Multiline = true;
				this._EditBox2.Name = "_EditBox2";
				this._EditBox2.TabIndex = 1;
				this._EditBox2.Text = "_EditBox2";
				this._Label1.Location = new System.Drawing.Point(32, 120);
				this._Label1.Name = "_Label1";
				this._Label1.Size = new System.Drawing.Size(200, 96);
				this._Label1.TabIndex = 2;
				this._Label1.Text = "_Label1";
				this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
				this.ClientSize = new System.Drawing.Size(292, 266);
				this.Controls.Add(this._Label1);
				this.Controls.Add(this._EditBox2);
				this.Controls.Add(this._EditBox1);
				this.Name = "Form4";
				this.Text = "Form4";
				this.Load += new System.EventHandler(this.Form4_Load);
				this.ResumeLayout(false);

			}

			private void Form4_Load(object sender, System.EventArgs e)
			{
				this.DataChange += new EventHandler(DataChangeHandler);
			}
			public event System.EventHandler DataChange;

			public void OnDataChange(object sender, EventArgs e)
			{
				if (DataChange !=null)
					this.DataChange(sender, new EventArgs());
			}

			public void DataChangeHandler(object sender, EventArgs e)
			{
				this._Label1.Text = sender.ToString();
			}

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

		}
	}
This could probably be done differently as well ... there are many ways to skin a cat ... but like your code, this code also gets the job done.

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform