473,804 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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

Dec 15 '06 #1
5 1805
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.Creat eInstance(...) as
IRefreshControl ;
if(control!=nul l) {
timer.Elapsed += delegate {
control.Refresh Data();
};
}

Otherwise, you could relace control.Refresh Data() 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.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
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.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
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.Creat eInstance(...) as
IRefreshControl ;
if(control!=nul l)
{
timer.Elapsed += control.Refresh Data;
}
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.Creat eInstance(...) as
IRefreshControl ;
if(control!=nul l) {
timer.Elapsed += delegate {
control.Refresh Data();
};
}

Otherwise, you could relace control.Refresh Data() 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
4312
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 hundreds of lines of code to format a Word document in a very specific way. Because my clients have various versions of Word, the problem of broken references comes up. I wish Microsoft had implemented a reasonable solution, so that VBA could do...
9
10431
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 As DAO.Workspace Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim rs As DAO.Recordset
5
1556
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 happens when i know that a library must have some method, say void StoreXML ( string XMLmsg ) but want to late bind, as in, only at run time, decide which library i wish to use.
9
387
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 be Controls property. How can I call the Controls property without knowing the type of the object? In VB.NET, you can just do this if Option Explicit is off by just writing SomeObject.Controls. The VB.NET runtime will handle the late binding...
1
2848
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 directly, I'm having tons of trouble. (lots of research, lots of newsgroups, but no success! I have an existing COM+ object "Company.Person" which has several interfaces. To load my object, I have to invoke a method that is "Imported" to "Person", the...
30
2840
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 integer The integers in these arrays are actually pointers to different columns of data in a text file.
6
1897
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 that describes this. Here's the setup. We have a COM server, written in Python. For completeness, here is the script: ----- testserver.py ----- import pythoncom
3
16075
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 time (Early Binding) or run time (Late Binding). When you declare an Object Variable as a specific Data Type, you are using Early Binding so the verification can take place during compile time. When you declare a Variable of the generic Object Data...
4
2616
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
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.