Killing time, here's a complete little program that creates a derived
textbox for a form and wires it up to an event on the form that is
fired when a timer fires.
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace WindowsApplicat ion3
{
public delegate void UpdateTextDeleg ate(object sender,
UpdateTextEvent Args e);
public class Form1 : System.Windows. Forms.Form
{
private System.Componen tModel.Containe r components = null;
private System.Windows. Forms.Button button1;
private System.Threadin g.Timer _timer = null;
public event UpdateTextDeleg ate UpdateText;
public Form1()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
this.button1.Lo cation = new System.Drawing. Point(8, 8);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void TimerFired(obje ct p)
{
if(null != UpdateText)
{
UpdateText(this ,new UpdateTextEvent Args(DateTime.N ow.ToString())) ;
}
}
private void button1_Click(o bject sender, System.EventArg s e)
{
_timer = new System.Threadin g.Timer(new
System.Threadin g.TimerCallback (this.TimerFire d),null,1000,10 00);
ThingFactory.Ad dTextBox(this);
button1.Enabled = false;
}
}
public class UpdateTextEvent Args : EventArgs
{
public UpdateTextEvent Args(string pNewText)
{
NewText = pNewText;
}
public string NewText;
}
public class ThingFactory
{
private ThingFactory()
{
}
public static void AddTextBox(Form 1 pForm)
{
NuTextBox t = new NuTextBox();
t.Location = new System.Drawing. Point(184, 136);
t.Name = "textBox1";
t.TabIndex = 0;
t.Text = "";
t.Size = new System.Drawing. Size(152, 20);
pForm.Controls. Add(t);
pForm.UpdateTex t+=new UpdateTextDeleg ate(t.SetText);
}
}
public class NuTextBox : System.Windows. Forms.TextBox
{
public NuTextBox()
{
}
public void SetText(object sender, UpdateTextEvent Args e)
{
this.Text = e.NewText;
}
}
}
DeveloperX wrote:
How about Add a new Event to the form, hook the timer to a private
method on the form and have that raise the event. You can then hook it
up to the new control in the same way all the other controls are done
by the IDE
ie this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
becomes this.TimerFired += new system.EventHan dler(newControl .Fired);
Ronin wrote:
I need a little help trying to figure out the last piece of this
puzzle.
I've got a form with an associated toolbox that will allow a user to
drag a control off the toolbox and drop it onto the form. The form
instantiates the control using the Activator.Creat eInstance method.
What I then need to do (and the part I have not figured out) is how to
attach a method from that control (a custom data refresh method) to a
timer.elapsed eventhandler on the form. I have been unable to figure
out how to get visibility of the method I need to attach.
Here's the code I have so far.
private void layoutPanel_Dra gDrop(object sender, DragEventArgs e)
{
NavBarItemLink link = GetItemLink(e.D ata);
if (link != null && link.Item.Enabl ed)
{
String ctrlName = link.ItemName;
String className;
String methodName;
Type t = null;
MethodInfo m = null;
Assembly a = Assembly.Load(" JacobsControls" );
Type[] types = a.GetTypes();
foreach (Type definedType in types)
{
className = definedType.Nam e;
if (className == ctrlName)
{
t = definedType;
MethodInfo[] methods = t.GetMethods();
foreach (MethodInfo method in methods)
{
methodName = method.Name;
if (methodName == "RefreshControl ")
{
m = method;
break;
}
}
break;
}
}
Object[] constructorArgs = { (int)13, (int)1 };
Object o = Activator.Creat eInstance(t, constructorArgs );
layoutPanel.Con trols.Add((Cont rol)o);
// This is where the newly created control needs to have
// a "RefreshControl " method attached to a timer.
}
Thanks,
Jason