Click here to Skip to main content
15,881,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am new at programming with C# so this could be rather a stupid question, but I was wondering if there is a way to connect two forms with each other.

I have 1 parent form with this code:
private void AToolStripMenuItem_Click(object sender, EventArgs e)
{
    FormA frm1 = new FormA();
    frm1.MdiParent = this;
    frm1.Show();
}

private void BToolStripMenuItem_Click(object sender, EventArgs e)
{
    FormB frm1 = new FormB();
    frm1.MdiParent = this;
    frm1.Show();
}


In FormA I have a list:
private void fillListBox()
{
    List<Docent> docenten = Docent.AllDocenten();

    listBox1.DataSource = docenten;
    listBox1.DisplayMember = "AllData";
    listBox1.ValueMember = "DocentID";
}


FormB has 3 buttons: The first one looks for the Docent in the list. The other two are used to alter the data in Docent or to delete it.
Now I would like to make it possible to select one item from the list in FormA and use it in FormB to delete or alter it. Because the way my program works right now, you have to insert the name of an item of the list before it can be altered or deleted.
That is rather difficult if you insert something like 'lkjomkj' in the list ;).

Thank you!
Posted

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

[EDIT]

Besides, it's a really bad idea to use MDI.

Indeed, 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. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA
 
Share this answer
 
v2
Comments
Maciej Los 27-Dec-13 11:35am    
Sergey, where is a comment about MDI?
+5!
Sergey Alexandrovich Kryukov 27-Dec-13 11:38am    
Thank you, I'll add it... :-)
—SA
Maciej Los 27-Dec-13 11:40am    
I'll give you double 5, if i could.
H.Brydon 28-Dec-13 13:34pm    
Additional one from me instead...
Sergey Alexandrovich Kryukov 28-Dec-13 17:41pm    
Thank you, Harvey.
—SA
If I understand what you want, you will need a public property in FormB

public string ItemToEdit { get; set; }


and say on the button click event and/or the list changed event in FormA you can set ItemToEdit with the value you want to change.

Of course you might need to declare the FormB frm1 outside the onclick event if you want to be able to change what you are editing after you click the button.
 
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