Click to See Complete Forum and Search --> : Passing Form Data Help/Tutorial needed


dconnell
September 22nd, 2009, 04:55 PM
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.

BigEd781
September 22nd, 2009, 05:11 PM
Your first inclination will be to pass references around, i.e.,


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.



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.

dannystommen
September 23rd, 2009, 02:19 AM
protected virtual void OnSomeEvent( EventArgs e )
{
SomeEvent( this, e );
}



Before raising the event, you always need to check if the event is not null


if (SomeEvent != null)
SomeEvent( this, e );

eclipsed4utoo
September 23rd, 2009, 10:26 AM
I did this tutorial using the constructor of the forms to pass data.

http://eclipsed4utoo.com/blog/passing-data-between-forms-in-c/

BigEd781
September 24th, 2009, 06:27 PM
Before raising the event, you always need to check if the event is not null


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.

dconnell
September 24th, 2009, 11:55 PM
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!