|
-
Feb 8th, 2006, 02:25 PM
#1
Thread Starter
Lively Member
[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
-
Feb 8th, 2006, 06:21 PM
#2
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.
-
Feb 8th, 2006, 07:59 PM
#3
Thread Starter
Lively Member
Re: Passing Data from form to form
-
Feb 8th, 2006, 09:45 PM
#4
Lively Member
Re: [RESOLVED] Passing Data from form to form
Thats good to know.
You would make the variables/functions static right?
-
Feb 8th, 2006, 10:02 PM
#5
Re: [RESOLVED] Passing Data from form to form
 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.
-
Feb 9th, 2006, 06:33 AM
#6
Fanatic Member
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)
-
Oct 20th, 2006, 12:37 AM
#7
Fanatic Member
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!
-
Oct 20th, 2006, 01:08 AM
#8
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.
-
Oct 20th, 2006, 01:47 AM
#9
Fanatic Member
Re: [RESOLVED] Passing Data from form to form
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
|