Scenario
I have Form1 consisting of a listView control
I
have Form2 consisting of a chunk of code, that reads items from a
folder and lists them in the listView control, belonging to 'Form1';
only i can not get it to work.
On Form1, when i click 'Show Form2' i have:
Form2 Child= new Form2();
Child.Parent = this;
Child.Show();
On
the Child form, when it pops up, i want to add items to the ListView
control on Form1. how on earth do i do this I can't seem to use.
Form1.ListView1 etc...
or
this.Owner etc...
Am
i missing something i tried setting the listView in question to
'Public' in the Form1.Designer code with no luck. PLEASE help me out.
Cheers.

Passing data between multiple forms? urgent help needed
James Fielding
B.Huard
Form2 Child= new Form2();
Child.Parent = this;
Child.Show();
Because you want to show a modeless form, you need to make it a member variable, so you can access it after creating it.
Form1.ListView1 etc...
For starters, you should not make your control public. You should also give it a sensible name.
this.Parent will give you the control, as you've set it to be this. You need to then cast it to an instance of your specific form ( which is called Form1 right now, but, again, you should give these things meaningful names ). Then what you should be doing is creating a method in Form1 which takes the data in question and passes it to the list.
The other ( better IMO ) approach is to use a delegate, which is defined in Form1 and then linked to a method in Form2, which then allows Form2 to call Form1 directly with the data in a more decoupled way ( i.e. in a way that doesn't mean the code is written so Form2 cannot exist without Form1 ). Realistically, it's unlikely you'll reuse either form, but it's a good OO practice in any case.
cubexsystems
Right, i'm off to implement this new knowledge in my groovy program
Thanks again,
Jonathan.
runcy
1. The names are meaningless, because i'm testing this out quickly before putting the code in my final program.
2. You use some terminology/method i'm not familiar with. I know it's a lot to expect, but do you either know of a good clear example of what i'm trying to do, or would it be too much for you to code this up quickly for me - Would be much appreciated.
If thats too much, maybe just small bits of code, showing the changes i need and where they would go, based on my naming above
Thanks either way,
Jonathan.
MartinGleeson
A member variable is defined within the class, but not within a method. It's then visible to everything within the class. As a result, you can also avoid a memory leak. You should do something like this
if ( form2 !=null && !form.Visible) form2.Dispose();
form2 = new Form2();
form2.Parent = this;
form2.Show();
If you do this:
class Form2
{
public delegate void AddRow(ListViewItem item); // creates a delegate
public AddRow OnAddRow = null;
then, within the code that decides there is data to add
if (OnAddRow != null)
{
foreach(ListViewItem item in items) // I assume you don't have an array of listviewitems, but however you do it...
OnAddRow(item); // this calls a method in Form1
}
class Form1
{
Form2 form2; // - creates a private member variable
then in your load method, after the call to initializecomponent
form2.OnAddItem += this.AddItem;
finally...
void AddItem(ListViewItem item)
{
listView1.Items.Add(item);
}
Note : this code is only supposed to show you how to create a delegate, all the listview stuff is from memory. The main point is, a delegate is a definition of a possible function, which can be in any class. So, we create a delegate, and an instance of it in the class that does the calling, and hook it up to a method in the class we want to call. Now, Form2 is able to tell Form1 to add an item, in a way that could be reused if Form1 changes to another class, it just hooks up the event.
You still need more code to really deal with your modeless dialog, not least is an IDisposable implimentation to cleans it up if it exists on close.
You should google for delegate C# and read up on delegates, they are very useful things.