Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
i'll try to explain easily and quickly,
I have a Main Form (form1) loading automatically a Child Form (form2) (so i have two forms opened).

In Main Form i do some math things etc and WHEN only the algorythm is done working, i would need to return some values into a ListBox that is on form2.

I have been looking for solution over the web for like 4-5 hours, none were fitting my needs.

Since form2 is opened before the math job is done, i cant set the values while opening it. SO i guess i need to use some EventHandler.

Any idea on how to implement that please?

im adding some blank code for you to help me visually easier maybe

FORM1 (MAIN ONE/Parent)
C#
private string doMath() {
     //blabla
     return "formated:string:to:use:for:List";
}

displayList(doMath());  //HERE IS THE HOW? cant access function on form2


FORM2 (Child)
C#
private void displayList(string listItems) { //
     string[] delim = { ":" };
     string[] returndata = listItems.Split(delim, StringSplitOptions.RemoveEmptyEntries);
     listBox1.Items.Clear();
     for (var i = 0; i < listItems.Length; i++)
     {
        this.listBox1.Items.Add(listItems[i]);
     }
     if (this.listBox1.Items.Count > 0)
     {
         this.listBox1.SelectedIndex = 0;
     }
}



thanks in advance guys.
Posted
Comments
Sergey Alexandrovich Kryukov 23-Feb-15 16:56pm    
A "child form"? Do you mean it? Parent-child relationship between forms is effectively made defunct, unless you are talking about MDI which is best avoided by all means.
—SA

make form2 a class variable. add a method to form2 to update/add text to listbox.
when form 1 process is finished call that method
 
Share this answer
 
Comments
NaMaCk 23-Feb-15 16:47pm    
Can you be a little more precise please? i dont get what you mean by "variable class" nor "add a method"...

my form2 is declared as this, incase:

public partial class form2 : Form
{}
I don't think your forms are really child and parent. Without setting Forms.TopLevel to false (don't do it!), you would get an exception at attempt to set any form's Parent.

It looks like a popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900