Click here to Skip to main content
15,888,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have an mdi parent form and two child forms.

on button click from form1, form 2 should be opened and on a radiobutton selection, i need to retrieve that radio button text to my first form text box.

i send the data and displayed it on form1 using delegates

but i used a new object of the form 1, what i want.... is to retrieve data to the already opened form1 , where other text boxes are already filled....

how can i solve this

my code in form 1 is:

C#
delegate void MyDelegate(string str);
public void PassingParameters(string item)
        {
            if (txtScheme.InvokeRequired)
            {
                MyDelegate d = new MyDelegate(PassingParameters);
                txtScheme.Invoke(d, new object[] { item });
            }
            else
            txtScheme.Text = item;
        }


and in form 2 event is :
C#
string  Scheme = rb.Text;
admission ad= new admission();
ad.PassingParameters(Scheme);
ad.MdiParent = this.ParentForm;
ad.Show();
this.Close();
Posted

1 solution

This is the 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[^].

Also, better don't use MDI. Who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. I can explain what to do instead. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

Please also see my past answers:
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA
 
Share this answer
 
Comments
Sushil Mate 30-Oct-12 2:52am    
He asked simple solution & you suggesting to thrash the MDI :)
No wonder he must be down voted.
For me, nice explanation & links my 5

[updated]ohh he accepted.. someone didn't liked it
Sergey Alexandrovich Kryukov 30-Oct-12 9:58am    
Thank you, Sushil.
I don't think it was OP, who actually accepted the answer.

As to the "simple solutions": apparently, some inquirers did yet realize that their "I want" is not what their mama is supposed to bring to them, unless they would cry. I cannot help get amazed again and again by questions of one on typical kind: "When I compile my code, I got error message is "AAA is not allowed". How to do AAA?". :-)

By the way, form collaboration is indeed simple, not more complicated them any other objects, and it could be as simple as exposing some method or property (with "internal") and passing the form reference to the instance of another form, without delegates, interfaces, events, etc. In simple applications, this is quite acceptable. For big and complex code, though, it's good to avoid giving more access than it's really needed, that's all.
--SA
Sushil Mate 30-Oct-12 12:08pm    
Agreed.

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