Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transfering data from 1 form to another form

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
Hi
I am trying to transfer items from a listbox on form1 to a listbox on form2.

Using a command button i want to open form2 and display the items in a listbox that come from a listbox on form1.

So far i can only open form2 that conatins a listbox

Form2 Fnames = new Form2();
Fnames.Show();

I have had a look in the HELP, but not much joy. I am new the world of .NET and C# so any help would be Appreciated.

Thanks
 
If you're coming to C# from VB6, this is a common "gotcha". The big difference is that in .NET, forms are objects.

Once again, forms are objects. So you need to treat form variables (object variables that hold a reference to a form) like any other variable with regards to scoping and visibility.

So if form2 needs something from form1, form2 needs access to a variable that points to form1. Once it has that, it will be able to access any public property, method, etc. that is exposed by the form1 instance variable.

In your case, you need to make sure that the listbox on form1 is declared as public (almost always already is), and after creating your form2 instance, pass a copy of form1 to it:
Code:
Form1 MyForm1 = new Form1();
MyForm1.Show();  // Let Form1 load up

// do other stuff

Form2 MyForm2 = new Form2();
MyForm2.OtherForm = MyForm1;  // tell form2 about form1
                              // using the "OtherForm"
                              // property that accepts a Form
MyForm2.Show();  // Let form2 do it's thing
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top