Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have panel control on ParentForm, i opened the ChildForm on panl control and from ChildForm i want to open ThirdForm on panel control of ParentForm


i used this code on the button of ChildForm

C#
ParentForm panel = new ParentForm();
panel.panel_Main.Controls.Clear();
ThirdForm third = new ThirdForm();
third.TopLevel = false;
third.AutoScroll = true;
third.Dock = DockStyle.Fill;
panel.panel_Main.Controls.Add(third);
student.Show();
Posted
Comments
[no name] 6-Sep-14 18:36pm    
And did you have some sort of a question? Maybe you would like to describe a problem that you have with this code?
George Jonsson 7-Sep-14 1:14am    
It is very difficult to understand what you want to do, so please explain where you are stuck.

That isn't going to work, ever: the clue is in the new keyword. To access the form that is being displayed, you need to get the currently displayed instance, not a new instance - the new one will have nothing to do with the old. Think of it like a car: if you put your mobile phone in the glove box of your car, then go out an buy a new car would you expect it to have your mobile in it's glove box?

But even then, don't.
It's a poor idea, because it "locks" the design of the two forms: you can't make changes without considering the effects on the outside world, and you can't reuse your child form anywhere else.

Instead, your child form should ask the parent to do the job for it.
Have a look here: Transferring information between two forms, Part 2: Child to Parent[^] - it explains how to do it.
 
Share this answer
 
i used this instead of my last code and it works.

((System.Windows.Forms.Panel)Application.OpenForms["ParentForm"].Controls["panel_Main"]).Controls.Clear();
                    ThirdForm third = new ThirdForm();
                    third.TopLevel = false;
                    third.AutoScroll = true;
                    third.Dock = DockStyle.Fill;
                  ((System.Windows.Forms.Panel)Application.OpenForms["ParentForm"].Controls["panel_Main"]).Controls.Add(student);
                    third.Show();
 
Share this answer
 
v2

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