Results 1 to 9 of 9

Thread: [RESOLVED] Passing Data from form to form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    66

    Resolved [RESOLVED] Passing Data from form to form

    Ok, what i need to do is pass data between to forms. It will do somthing like this.

    form1 activates form2.

    Then on form2 text is entered in a textbox. When form2 is closed the text is passed back to form1. Then displayed in a messagebox. How could i do this?


    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing Data from form to form

    Forms are objects, just like anything else in .NET. To get data into an object you either set a property or pass a parameter to a method, which includes the constructor. To get data out of an object you either get a property or call a function and get the return value. You do this all the time with all objects. You get and set properties, you pass parameters to constructors, you call methods and pass parameters and get return values. Nothing changes because the objects are forms. This means that you have to write properties and/or methods in the Form2 class that will accept and return the data you're interested in. If this doesn't seem clear then I suggest that you follow the Start VB.NET link in my signature and read the OOP section.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    66

    Re: Passing Data from form to form

    I got it.

  4. #4
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: [RESOLVED] Passing Data from form to form

    Thats good to know.
    You would make the variables/functions static right?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Passing Data from form to form

    Quote Originally Posted by 2MuchRiceMakesMeSick
    Thats good to know.
    You would make the variables/functions static right?
    No, they should be instance members because you are dealing with an instance. Static members are sometimes used as a convenience so that people don't need to bother doing the right thing and passing references where they should. That is an abuse of static members though, and lazy programming. Static members should be used only when a member logically belongs to the class and not a specific instance.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Fanatic Member Slaine's Avatar
    Join Date
    Jul 2002
    Posts
    641

    Re: [RESOLVED] Passing Data from form to form

    I posted this in another forum (which oddly had a jmcilhinney too

    http://www.developerfusion.co.uk/forums/topic-33571

    --------------
    Hope it helps somewhat:

    A more fundamentally sound approach is to have a custom struct that can hold the values you wish to pass. Then in the called form have a public property that will accept this struct.

    Then you just ensure you have populated the structure, and passed it to the called form before you show it.

    eg:

    In Form1 (the calling form)

    Code:
       // Public structure used to pass the control values. 
       public struct FormData 
       { 
           public string Source; 
           public string Dest; 
           public string Initials; 
       } 
            
       public static void ShowForm2() 
       { 
           // Create a variable to hold the control values. 
           FormData myData = new FormData(); 
            
           // Populate it. 
           myData.Source = txtSource.Text; 
           myData.Dest = txtDest.Text; 
           myData.Initials = txtInitials.Text; 
            
           // Create an instance of Form2. 
           Form2 newForm = new Form2(); 
            
           // Provide the control values to Form2. 
           newForm.ParentData = myData; 
            
           // Show Form2. 
           DialogResult result = Form2.ShowDialog(); 
            
       }
    In Form2 (the called form)

    Code:
       // Local scope field variable to hold the parents control values. 
       private Form1.FormData parentData = new Form1.FormData(); 
        
       // Public property field to handle passing of the control values. 
       public FormData ParentData 
       { 
           get 
               { 
                   return parentData; 
               } 
           set 
               { 
                   parentData.Source = value.Source; 
                   parentData.Dest = value.Dest; 
                   parentData.Initials = value.Initials; 
               } 
       }
    Martin J Wallace (Slaine)

  7. #7
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: [RESOLVED] Passing Data from form to form

    can anybody please validate the code above cause i've tried it but it did not work..like
    Code:
    public FormData ParentData
    FormData has an error, it says
    Error 3 The type or namespace name 'FormData' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\daimous\My Documents\Visual Studio 2005\Projects\BCMD\WindowsApplication1\Notification.cs 40 16 RefNumGen
    is there something wrong in that code? Thanks in advance!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Passing Data from form to form

    If the FormData structure is declared in the Form1 class then outside of the Form1 class it must be referred to as Form1.FormData, as has been done on the line above.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: [RESOLVED] Passing Data from form to form

    Thanks dude...

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