Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing Data between forms

Passing Data between forms

Scheduled Pinned Locked Moved C#
csharptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeff Patterson
    wrote on last edited by
    #1

    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[^]

    N 1 Reply Last reply
    0
    • J Jeff Patterson

      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[^]

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • N Nick Parker

        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

        J Offline
        J Offline
        Jeff Patterson
        wrote on last edited by
        #3

        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[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups