Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#

Application Framework to plug components - Part II - Notify Events

Rate me:
Please Sign up or sign in to vote.
2.00/5 (5 votes)
15 Jun 20053 min read 18.5K   548   17  
This article describes how to fire events to a framework from a component plugged into it.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using MathProxy;
namespace IntfDemo
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmIntfDemo : System.Windows.Forms.Form
	{
		IMath m_Math;
		MathEvents m_mathEvent;
		private System.Windows.Forms.TextBox txtValue1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtValue2;
		private System.Windows.Forms.Button btnDiv;
		private System.Windows.Forms.Button btnCreateIntf;
		public System.Windows.Forms.TextBox txtEventMsg;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmIntfDemo()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			m_mathEvent = new MathEvents();
			m_mathEvent.m_form = this;

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.txtValue1 = new System.Windows.Forms.TextBox();
			this.txtValue2 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.btnDiv = new System.Windows.Forms.Button();
			this.btnCreateIntf = new System.Windows.Forms.Button();
			this.txtEventMsg = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// txtValue1
			// 
			this.txtValue1.Location = new System.Drawing.Point(8, 56);
			this.txtValue1.Name = "txtValue1";
			this.txtValue1.Size = new System.Drawing.Size(56, 20);
			this.txtValue1.TabIndex = 0;
			this.txtValue1.Text = "";
			// 
			// txtValue2
			// 
			this.txtValue2.Location = new System.Drawing.Point(72, 56);
			this.txtValue2.Name = "txtValue2";
			this.txtValue2.Size = new System.Drawing.Size(56, 20);
			this.txtValue2.TabIndex = 1;
			this.txtValue2.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(72, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 4;
			this.label1.Text = "Value 2";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 16);
			this.label2.TabIndex = 5;
			this.label2.Text = "Value 1";
			// 
			// btnDiv
			// 
			this.btnDiv.Location = new System.Drawing.Point(136, 56);
			this.btnDiv.Name = "btnDiv";
			this.btnDiv.TabIndex = 6;
			this.btnDiv.Text = "Divide";
			this.btnDiv.Click += new System.EventHandler(this.btnDiv_Click);
			// 
			// btnCreateIntf
			// 
			this.btnCreateIntf.Location = new System.Drawing.Point(8, 0);
			this.btnCreateIntf.Name = "btnCreateIntf";
			this.btnCreateIntf.Size = new System.Drawing.Size(96, 23);
			this.btnCreateIntf.TabIndex = 7;
			this.btnCreateIntf.Text = "Create Interface";
			this.btnCreateIntf.Click += new System.EventHandler(this.btnCreateIntf_Click);
			// 
			// txtEventMsg
			// 
			this.txtEventMsg.Location = new System.Drawing.Point(8, 104);
			this.txtEventMsg.Name = "txtEventMsg";
			this.txtEventMsg.Size = new System.Drawing.Size(200, 20);
			this.txtEventMsg.TabIndex = 8;
			this.txtEventMsg.Text = "";
			// 
			// frmIntfDemo
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(224, 134);
			this.Controls.Add(this.txtEventMsg);
			this.Controls.Add(this.btnCreateIntf);
			this.Controls.Add(this.btnDiv);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.txtValue2);
			this.Controls.Add(this.txtValue1);
			this.Name = "frmIntfDemo";
			this.Text = "Interface Demo";
			this.Load += new System.EventHandler(this.frmIntfDemo_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmIntfDemo());
		}

		private static MethodInfo GetMethodInfo(string reflection)
		{
			MethodInfo mi=null;
			string[] info=reflection.Split(new char[] {'/'});
			Assembly mainAssembly=Assembly.LoadFrom(info[0]);
			Type type=mainAssembly.GetType(info[1]);
			mi=type.GetMethod(info[2]);
			return mi;
		}

		private void frmIntfDemo_Load(object sender, System.EventArgs e)
		{
			txtValue1.Text = "50";
			txtValue2.Text = "30";
		}

		private void btnDiv_Click(object sender, System.EventArgs e)
		{
			if (m_Math!=null)
			{
				int iRet;
				iRet = m_Math.CalcValues(int.Parse(txtValue1.Text.ToString()),int.Parse(txtValue2.Text.ToString()));
				MessageBox.Show( iRet.ToString() );
			}
			else
			{
				MessageBox.Show( "First create the interface" );
			}
		}

		private void btnCreateIntf_Click(object sender, System.EventArgs e)
		{
			if (m_Math==null)
			{
				MethodInfo mi=GetMethodInfo("MathEvents.dll/MathEvents.CreateIntf/CreateEx");
				object[] parms=new object[] {m_mathEvent};
				m_Math = (IMath)mi.Invoke(null, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Static, null, parms, null);
			}
			else
			{
				MessageBox.Show( "Interface Already Create" );
			}
		}
	}
	public class MathEvents : IMathEvent
	{
		public frmIntfDemo m_form;
		void IMathEvent.CalcErrorEvent(string s)
		{
			m_form.txtEventMsg.Text = s;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions