Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
3.86/5 (3 votes)
See more:
Hi All...

I am doing a simple project in c#, i want to display the form's in one panel, the forms added that panel from another form also.IN this Condition in VB.net then the code is very simple

VB
Public Class Form2

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form3.TopLevel = False
        Form3.TopMost = True
        Form1.Panel1.Controls.Add(Form3)
        Form3.Show()
    End Sub
End Class


But in c# this is not a simple,I know how to add the controls in panel from the same form but i am new for this case, So please Help me, How to add the Form in Panel from another Form in C# Windows Application.
Posted
Updated 18-Apr-20 8:18am
v2

This is a very poor way to do things: it will work just the same in C# as it does in VB, but...that doesn't mean it's a good solution.
If you translate your code into C#, it will work:
C#
private void button1_Click(object sender, EventArgs e)
    {
    frmOther fOther = new frmOther();
    fOther.TopLevel = false;
    fOther.TopMost = true;
    panel1.Controls.Add(fOther);
    fOther.Show();
    }
But... your code requires that Form2 knows about Form1 and Form3 which breaks the rules of OOPs and ties the forms together.
A much, much better solution would be to create a UserControl which you add to the panel (and to From3 if it needs to be shown independently) as this keeps things tidier. And Form2 should not access Form1 controls at all! It should instead request Form1 to do it itself, or you end up with any change you want to make to Form1 potentially breaking code all over the place!
 
Share this answer
 
Comments
RhishikeshLathe 6-Feb-14 6:37am    
thank you OriginalGriff sir,
here i got new thing, to know.
You are always helpfull to me,
Thanks a lot.
OriginalGriff 6-Feb-14 6:40am    
You're welcome!
Siva Hyderabad 6-Feb-14 8:18am    
do we write stored procedure with out "AS" and "BEGIN" keywords? please suggest me?
OriginalGriff 6-Feb-14 8:27am    
What does that have to do with WinForms within panels?
sv sathish 7-Feb-14 6:30am    
i know how to add the controls in panel in same form. my question is how to add the form in panel from another form. It's possible or not???
Hi...
My problem is solved the following code is clear my doubt,Thanks for Vulpes.
public class Form2 : Form
{  
  private void button1_Click(object, EventArgs e)
  {
      Form3 form3 = new Form3();
      form3.TopLevel = false;
      form3.TopMost = true;
      Form1 form1 = (Form1)Application.OpenForms["Form1"];
      Panel panel1 = (Panel)form1.Controls["panel1"];
      panel1.Controls.Add(form3); 
      form3.Show();
  }
} 


And Also i give the thanks for all Comments.
 
Share this answer
 
Comments
Member 14071977 23-Jan-19 12:47pm    
Thanks this worked for me.
To simply describe what you are doing in VB: When a Button is Clicked on an instance of Form2:

1. take a pre-existing instance of Form3

2. insert that instance of Form3 into a Panel Control in an instance of Form1

It is not a good idea to put a Form inside another Form, or inside a Control in another Form (unless you are using the old MDI architecture, which I suggest you avoid). OriginalGriff is right-on when he advises you to use a UserControl !

To help you get a better understanding of programming in WinForms, we need to know which Form is the "main" Form here: does Form1 create Form2, and Form3 ?

If that's the case, then we can say your goal is:

1. to give an instance of Form2 a way to communicate to Form1 that it needs to insert an instance of a UI into a Panel inside it.

So, we give Form1 a way to subscribe to the Button Click Event on Form2 like this:

In Form2: by creating a publicly acessible reference to its Button:
C#
public partial class Form2 : Form
{
    // create a public property of type 'Button
    public Button f2Button { set; get; }

    public Form2()
    {
        InitializeComponent();
        
        // set the Property so 'Form1 can see it
        f2Button = this.button1;
    }
}
In Form1: by subscribing to the Button's Click Event
C#
public partial class Form2 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
        
    Form2 f2 = new Form2();

    private void Form1_Load(object sender, EventArgs e)
    {
       // subscribe to Form2's 'button1 Click Event
       f2.f2Button.Click += f2Button_Click;
    }

    private void f2Button_Click(object sender, EventArgs e)
    {
       // the code to add some UI to the Panel on Form1 goes here
       MyUserControl uc = new MyUserControl;
       
       panel1.Controls.Add(uc);

       // set position, or Dock, or Anchor of the added UserControl as needed 
    }

}
By constraining what Form2 exposes to other Forms/Classes to only its Button, we are trying to keep the "coupling" (dependency) between Forms/Classes to a minimum.
 
Share this answer
 
v2
Comments
sv sathish 7-Feb-14 6:27am    
You test this code?? Your Comments not helpful me... please post any another ideas..
OriginalGriff 7-Feb-14 7:16am    
I don't think he is listening to us... :sigh:

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