CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2007
    Posts
    9

    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.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Passing Form Data Help/Tutorial needed

    Quote Originally Posted by BigEd781 View Post
    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 );

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    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

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Passing Form Data Help/Tutorial needed

    Quote Originally Posted by dannystommen View Post
    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.

  6. #6
    Join Date
    Nov 2007
    Posts
    9

    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
  •  





Click Here to Expand Forum to Full Width

Featured