473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Late Binding a Method to a Timer

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.CreateInstance 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_DragDrop(object sender, DragEventArgs e)
{
NavBarItemLink link = GetItemLink(e.Data);
if (link != null && link.Item.Enabled)
{
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.Name;
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.CreateInstance(t, constructorArgs);
layoutPanel.Controls.Add((Control)o);

// This is where the newly created control needs to have
// a "RefreshControl" method attached to a timer.

}

Thanks,
Jason

Dec 15 '06 #1
5 1772
Well, you can do this via reflection... however, can I suggest
something better? Define an interface eith the RefreshData method, and
ensure your custom controls implement this interface; you should then
be able to do something like:

IRefreshControl control = Activator.CreateInstance(...) as
IRefreshControl;
if(control!=null) {
timer.Elapsed += delegate {
control.RefreshData();
};
}

Otherwise, you could relace control.RefreshData() with mi.Invoke();

Marc
Dec 15 '06 #2

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.Click += new System.EventHandler(this.button1_Click);
becomes this.TimerFired+= new system.EventHandler(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.CreateInstance 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_DragDrop(object sender, DragEventArgs e)
{
NavBarItemLink link = GetItemLink(e.Data);
if (link != null && link.Item.Enabled)
{
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.Name;
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.CreateInstance(t, constructorArgs);
layoutPanel.Controls.Add((Control)o);

// This is where the newly created control needs to have
// a "RefreshControl" method attached to a timer.

}

Thanks,
Jason
Dec 15 '06 #3
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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication3
{
public delegate void UpdateTextDelegate(object sender,
UpdateTextEventArgs e);

public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private System.Threading.Timer _timer = null;

public event UpdateTextDelegate UpdateText;

public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();

this.button1.Location = new System.Drawing.Point(8, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void TimerFired(object p)
{
if(null != UpdateText)
{
UpdateText(this,new UpdateTextEventArgs(DateTime.Now.ToString()));
}
}
private void button1_Click(object sender, System.EventArgs e)
{
_timer = new System.Threading.Timer(new
System.Threading.TimerCallback(this.TimerFired),nu ll,1000,1000);
ThingFactory.AddTextBox(this);
button1.Enabled = false;
}
}
public class UpdateTextEventArgs : EventArgs
{
public UpdateTextEventArgs(string pNewText)
{
NewText = pNewText;
}
public string NewText;
}
public class ThingFactory
{
private ThingFactory()
{
}
public static void AddTextBox(Form1 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.UpdateText+=new UpdateTextDelegate(t.SetText);
}
}
public class NuTextBox : System.Windows.Forms.TextBox
{
public NuTextBox()
{
}
public void SetText(object sender, UpdateTextEventArgs 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.Click += new System.EventHandler(this.button1_Click);
becomes this.TimerFired+= new system.EventHandler(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.CreateInstance 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_DragDrop(object sender, DragEventArgs e)
{
NavBarItemLink link = GetItemLink(e.Data);
if (link != null && link.Item.Enabled)
{
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.Name;
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.CreateInstance(t, constructorArgs);
layoutPanel.Controls.Add((Control)o);

// This is where the newly created control needs to have
// a "RefreshControl" method attached to a timer.

}

Thanks,
Jason
Dec 15 '06 #4
Marc,

Thanks for the advice. I had to make a few tweaks, but it works very
well (and with a lot let work).

Here's what I did to get it to work:

IRefreshControl control = Activator.CreateInstance(...) as
IRefreshControl;
if(control!=null)
{
timer.Elapsed += control.RefreshData;
}
Marc Gravell wrote:
Well, you can do this via reflection... however, can I suggest
something better? Define an interface eith the RefreshData method, and
ensure your custom controls implement this interface; you should then
be able to do something like:

IRefreshControl control = Activator.CreateInstance(...) as
IRefreshControl;
if(control!=null) {
timer.Elapsed += delegate {
control.RefreshData();
};
}

Otherwise, you could relace control.RefreshData() with mi.Invoke();

Marc
Dec 19 '06 #5
That'll do it. By using the inline delegate I was trying to avoid
having to have the (sender, args) signature on the method, but that
works fine too. Another approach (as suggested by another poster) is to
put the control into a field and have a form level event handler check
the field and invoke the method - same result, different approach.

Marc

Dec 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Composer | last post by:
I've read many postings about the problem of Access.References.IsBroken and the consensus seems to be that late binding is the cure-all. I have a very complex Access application that needs...
9
by: Zlatko Matić | last post by:
I was reading about late binding, but I'm not completely sure what is to be done in order to adjust code to late binding... For example, I'm not sure if this is correct: early binding: Dim ws...
5
by: Daniel Bass | last post by:
..Net is great for modulerising libraries, so that all you need do to access a DLL, is simply call Add Reference and wallah, it's as though the library were written in your project. But what...
9
by: Scott English | last post by:
I am writing an C# program. I call a method on a COM object that returns Object. I don't know the type of the object (and reflection just says its a __ComObject), but I know there is supposed to...
1
by: Jim | last post by:
Hey all I'm trying to late bind a VB6 object in Component Services using c#. I've been able to do tons of late binding, but now that I have a com+ object that doesn't have a method exposed...
30
by: lgbjr | last post by:
hi All, I've decided to use Options Strict ON in one of my apps and now I'm trying to fix a late binding issue. I have 5 integer arrays: dim IA1(500), IA2(500), IA3(500), IA4(500), IA5(500) as...
6
by: Tim Roberts | last post by:
I've been doing COM a long time, but I've just come across a behavior with late binding that surprises me. VB and VBS are not my normal milieux, so I'm hoping someone can point me to a document...
3
ADezii
by: ADezii | last post by:
The process of verifying that an Object exists and that a specified Property or Method is valid is called Binding. There are two times when this verification process can take place: during compile...
4
by: =?Utf-8?B?Y2xhcmE=?= | last post by:
Hi all, what is the difference between the late binding and reflection? clara -- thank you so much for your help
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.