|
-
September 22nd, 2009, 04:55 PM
#1
Passing Form Data Help/Tutorial needed
I have Visual Studio 2008 and use C#. I'm a semi-beginner and have just learned how to create Modal Dialog Boxes. I am now trying to learn how to pass data between two forms, but everything I've seen is a bit confusing to me. Screenshots and listings of my current project can be found at www.heoo.info. Any help with my project or the location of a good tutorial that would help me with my project would be appriciated. Thank you.
-
September 22nd, 2009, 05:11 PM
#2
Re: Passing Form Data Help/Tutorial needed
Your first inclination will be to pass references around, i.e.,
Code:
class Form1 : Form
{
public void DoSomething( )
{
MessageBox.Show( "Form1 : DoSomething called" );
}
}
class Form2 : Form
{
private Form1 m_form;
public Form2( Form1 form )
{
// hold a reference to form1
m_form = form;
}
public void DoSomethingWithForm1( )
{
// calls Form1 method "DoSomething"
m_form.DoSomething( );
}
}
However simple that may seem, don't do it. You have now made your Form2 class dependent on the implementation of Form1, and you will soon start making the controls on Form1 public so that they can be accessed by Form2. This is a bad design choice and should be avoided.
Instead, use events. If you don't know how to use them, learn. Form2 can raise events that will be handled by Form1. Form1 can update itself as needed when the event in Form2 is raised.
Code:
class Form1 : Form
{
void ShowForm2( )
{
using ( Form2 form = new Form2( ) )
{
form.SomeEvent += DoSomething;
form.ShowDialog( );
}
}
public void DoSomething( object sender, EventArgs e )
{
MessageBox.Show( "Event raised in Form2" );
}
}
class Form2 : Form
{
public event EventHandler SomeEvent = delegate { };
protected virtual void OnSomeEvent( EventArgs, e )
{
SomeEvent( this, e );
}
private void CalcSomething( )
{
// perform some calculation
// raise the event to signal that the calculation is complete
OnSomeEvent( EventArgs.Empty );
}
}
You can create your own classes that inherit from EventArgs so that you can pass data down to all listeners if need be.
-
September 23rd, 2009, 02:19 AM
#3
Re: Passing Form Data Help/Tutorial needed
 Originally Posted by BigEd781
Code:
protected virtual void OnSomeEvent( EventArgs e )
{
SomeEvent( this, e );
}
Before raising the event, you always need to check if the event is not null
Code:
if (SomeEvent != null)
SomeEvent( this, e );
-
September 23rd, 2009, 10:26 AM
#4
Re: Passing Form Data Help/Tutorial needed
I did this tutorial using the constructor of the forms to pass data.
http://eclipsed4utoo.com/blog/passin...en-forms-in-c/
===============================
My Blog
-
September 24th, 2009, 06:27 PM
#5
Re: Passing Form Data Help/Tutorial needed
 Originally Posted by dannystommen
Before raising the event, you always need to check if the event is not null
Code:
if (SomeEvent != null)
SomeEvent( this, e );
Not if you assign an empty delegate to the event as I did when I declared it. That guarantees that the event will never be null as outside code cannot set the event.
-
September 24th, 2009, 11:55 PM
#6
Re: Passing Form Data Help/Tutorial needed
Thank you all for your help! As things turn out, the Tutorial was just what I needed. It was short, simple, and it worked. I am now going to try to modify the project so that child form sends data back to the parent. Assuming that I accomplish that, I'll attempt my ultimate project of sending multiple data items between forms. If I can't get things done, I'll be back. Thanks again all!
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
|