|
-
Sep 12th, 2005, 11:47 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] delegate callback problem...stack overflow
I am coding a user control. Within the user control I am trying to define a callback. Here is what I have:
//class var (create delegate)
private delegate void sockDelegate(string s);
//in constuctor (init delegate variable)
frmMain fMain = new frmMain();
sockDelegate del = new sockDelegate(fMain.socketClicked);
//when the socket is clicked (button event handler). Call the callback function on frmMain
del("the socket was clicked");
The problem is in the constructor. I have to create an instance of frmMain, but it has an instance of this user control so:
frmMain loads and inits the user control which loads a frmMain which loads a user control whick loads a frmMain....etc....etc until the stack is full.
How can I assign the callback? I would like a code example of a function in the user control that will assign the callback, taking the function as a passed parameter. Something like:
public bool assignCallback(functionName, formInstance){...}
Please help me!
-
Sep 12th, 2005, 11:56 AM
#2
Frenzied Member
Re: delegate callback problem...stack overflow
How about if your user control raises an event, and the main form registers with the event? This way, the control doesn't have to know about the main form.
Mike
-
Sep 12th, 2005, 12:07 PM
#3
Thread Starter
Hyperactive Member
Re: delegate callback problem...stack overflow
As long as the control is totally generic. I don't want a user of it to have to edit the code at all.
How would I do this? Can you give me a generic example?
-
Sep 12th, 2005, 12:17 PM
#4
Re: delegate callback problem...stack overflow
look at this thread
http://www.vbforums.com/showthread.php?t=357956
seems like you want events
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Sep 13th, 2005, 05:34 PM
#5
Thread Starter
Hyperactive Member
Re: delegate callback problem...stack overflow
I was able to solve this by implementing an interface to the container form:
Code:
// Include this interface in your container form:
public interface ICallBack
{
void callBack(string socket);
}
// Then have your container form inherit the interface:
// public class frmMain : System.Windows.Forms.Form , ICallBack
{
...
}
// Now since you are using an interface, you must include the function "callBack" in your container form.
public void callBack(string socket)
{
Console.WriteLine(socket);
}
// Now in your event handler on the control:
private void sockClickHand(Object sender, System.EventArgs e)
{
ICallBack cb = this.Parent as ICallBack;
if(cb!=null)
cb.callBack(sender.ToString());
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|