Passing Data between forms
-
OK, I have spent hours trying to figure this out on my own. I think I just don't know what the correct terminology is. I am trying to call a form enter some data and pass that data back to the calling form. I am using a custom form and not a built in dialog. Can someone point me in the right direction. I can't seem to even find an example on CP, MSDN, or a mayrid of other websites as well as two C# books. Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]
-
OK, I have spent hours trying to figure this out on my own. I think I just don't know what the correct terminology is. I am trying to call a form enter some data and pass that data back to the calling form. I am using a custom form and not a built in dialog. Can someone point me in the right direction. I can't seem to even find an example on CP, MSDN, or a mayrid of other websites as well as two C# books. Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]
Jeff, You should check into delegates, events, and eventargs. You can derive a class from
EventArgs
from which you can specify whatever values you want to pass. Events allow you to register delegates which are held in some-what of a linked-list fashion which are then invoked when the event fires. There are several good articles available, some which are on CP (check out Chris Sell's article: .NET Delegates: A C# Bedtime Story[^]). Hope this is of some help. Here is just one example of how delegates and events work together:using System;
using System.Windows.Forms;namespace Test
{
class Class1 : System.Windows.Forms.Form
{
public Class1(){}public delegate void MyEventHandler(int i); public event MyEventHandler MyEvent; static void Main(string\[\] args) { Class1 cls = new Class1(); cls.MyEvent += new MyEventHandler(SomeMethod); cls.MyEvent += new MyEventHandler(AnotherMethod); if(cls.MyEvent != null) cls.MyEvent(6); cls.ShowDialog(); } public static void SomeMethod(int i) { MessageBox.Show("Hello from SomeMethod. " + i + " was passed in."); } public static void AnotherMethod(int i) { MessageBox.Show("Hello from AnotherMethod. " + i + " was passed in."); } }
}
-Nick Parker DeveloperNotes.com
-
Jeff, You should check into delegates, events, and eventargs. You can derive a class from
EventArgs
from which you can specify whatever values you want to pass. Events allow you to register delegates which are held in some-what of a linked-list fashion which are then invoked when the event fires. There are several good articles available, some which are on CP (check out Chris Sell's article: .NET Delegates: A C# Bedtime Story[^]). Hope this is of some help. Here is just one example of how delegates and events work together:using System;
using System.Windows.Forms;namespace Test
{
class Class1 : System.Windows.Forms.Form
{
public Class1(){}public delegate void MyEventHandler(int i); public event MyEventHandler MyEvent; static void Main(string\[\] args) { Class1 cls = new Class1(); cls.MyEvent += new MyEventHandler(SomeMethod); cls.MyEvent += new MyEventHandler(AnotherMethod); if(cls.MyEvent != null) cls.MyEvent(6); cls.ShowDialog(); } public static void SomeMethod(int i) { MessageBox.Show("Hello from SomeMethod. " + i + " was passed in."); } public static void AnotherMethod(int i) { MessageBox.Show("Hello from AnotherMethod. " + i + " was passed in."); } }
}
-Nick Parker DeveloperNotes.com
Thanks Nick, I will have to study this a little more indepthly. I read the "Bed Time Story" before I posted this message and it looked like it was more complicated that what I was trying to do. I guess not. Thanks Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]