Click here to Skip to main content
15,887,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys !

I have 2 form and 3 UserControl(Campain_Overview , Campain_Post, Campain_Interact).

1 - MainForm contain panel2 hold UserControl name Campain_Overview, In this UserControl contain button call "Add Campain".

2 - When I click on "Add Campain" Button, It's show another form diaglog call "Add Campain".This form contain 2 button : "Campain Post" and "Campain Interact".

3 - So I want If I click on button "Campain Post", It's close "Add Campain" form, then load userControl Campain_Post into panel2 in Mainform.

4 - Similar to button "Campain Interact", Close "Add Campain" form, then load userControl Campain_Interact into panel2 in Mainform.

This is pic form my work :
http://prntscr.com/mfpt8p[^]


Can you guys help me ? And show me in detail with code, because I'm a baby in programing, and English is not my main language !

Thanks you

What I have tried:

This is in AddCampain.cs form. Even click generate by designer. But When I run this, it's said "Object Reference not set to an instance of an object" at this line :
pnl.panel2.Controls.Add(camPost);

pnl.panel2.Controls.Add(camInteract);

private void CampainPost_Click(object sender, EventArgs e) {


           MainForm pnl = (MainForm)ParentForm;

           Campain_Post camPost = new Campain_Post();
           pnl.panel2.Controls.Add(camPost);
           camPost.Dock = DockStyle.Fill;
           camPost.BringToFront();
           Close();

       }


<pre> private void CampainInteraction_Click(object sender, EventArgs e) {


            MainForm pnl = (MainForm)ParentForm;
            Campain_Interaction camInteract = new Campain_Interaction ();
            pnl.panel2.Controls.Add(camInteract);
            camInteract .Dock = DockStyle.Fill;
            camInteract .BringToFront();
            Close();

        }
Posted
Updated 2-Feb-19 6:24am
v2

1 solution

"How to add usercontrol to a panel on mainform from another form"
Don't do that!

Instead, your "Add Campain" dialog should simply return the type of campaign the user wants to add. It should be the MainFrm's responsibility to react to that and add the necessary user control to itself. It's not a good idea to expose the internals of objects (especially UI objects). Separate the concerns of the 2 UIs: the purpose of the dialog is to simply determine the type of campaign. It's up to MainFrm to handle this in any way it chooses.

I also recommend not using a dialog to determine the type of campaign. Instead, replace the "Add Campain" button with a menu button that has 2 options: Campain Post and Campain Interaction.

Lastly, it's "campaign", not "campain". :)

/ravi
 
Share this answer
 
Comments
Member 11745656 2-Feb-19 22:58pm    
can you give me some code for reference ?
Ok I understand that dialog for seperate tye of campaign, and menu button can hold it ! But I want to do it with popup window and choose type of campaign. Because it's not only the campaign, and also about other function, tools, etc,.... if it all in menu button, it's really hard to see; dialog is easy to get a clean view !
Ravi Bhavnani 4-Feb-19 13:27pm    
OK, then go ahead and use a dialog.

My point is, the purpose of the dialog is to indicate the user's choice to the calling form. The calling form should use that information to do whatever it needs to do to its own UI. The dialog shouldn't alter the contents of the calling form.

/ravi

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