locked
Add a User Control into a panel dynamically RRS feed

  • Question

  • Hello Guys
    I am developing a Windows Application.... My Main form is divided into two parts... The 1st part(left hand side) contain a series of buttons.
    And the right hand side part contain a panel.....When i click on a button, i want a usercontrol to load on the panel.....

    Av try it but in vain...... am more used in ASP.NET application but its different on the WInForm...

    Please Help me
    Wednesday, March 3, 2010 9:29 AM

Answers

  • UsrControl uc=new UsrControl();
    //populate with data
    uc.Dock=DockStyle.Fill;
    panel2.Controls.Add(uc);

    better use Additional class that will create UsrControl instance for you, because creating new instance of usrControl everytime will affect your performance. Remember unlike stateless ASP.NET controls that are created with each reguest, winforms controls stay there.

    public static class UIFactory{
        static UCLoginControl loginControl=null;
        public static UCLoginControl LoginControl{ 
           get { 
              if(loginControl==null){
                loginControl=new UCLoginControl ();
                uc.Dock=DockStyle.Fill;
              }
              return loginControl; 
           }
        }
    }
    
    
    
    //--------------somewhere in your Form1.cs-------------
    
    UCLoginControl uc=UIFactory.LoginControl;
    //populate with data
    panel2.Controls.Add(uc);
    




    .NET guy
    • Proposed as answer by tsadigov Wednesday, March 3, 2010 4:09 PM
    • Marked as answer by Helen Zhou Tuesday, March 9, 2010 9:43 AM
    Wednesday, March 3, 2010 4:08 PM

All replies

  • Panel.Controls.Add(your user control)
    Ganesh Ranganathan
    [Please mark the post as answer if it answers your question]
    blog.ganeshzone.net
    Wednesday, March 3, 2010 9:46 AM
  • Hello
    this doesnot work.........
    panel.Controls.Add(...)  --> How will the panel know which usercontrol to load... If the usercontrol has a name "ucGetDetails", how to add that........
    Wednesday, March 3, 2010 9:50 AM
  • Create an instance of your UserControl and pass it to the Add Method.

     

    UserControl1 u1 = new UserControl1();

    Panel1.Controls.Ad(u1);


    Ganesh Ranganathan
    [Please mark the post as answer if it answers your question]
    blog.ganeshzone.net
    Wednesday, March 3, 2010 10:23 AM
  • UsrControl uc=new UsrControl();
    //populate with data
    uc.Dock=DockStyle.Fill;
    panel2.Controls.Add(uc);

    better use Additional class that will create UsrControl instance for you, because creating new instance of usrControl everytime will affect your performance. Remember unlike stateless ASP.NET controls that are created with each reguest, winforms controls stay there.

    public static class UIFactory{
        static UCLoginControl loginControl=null;
        public static UCLoginControl LoginControl{ 
           get { 
              if(loginControl==null){
                loginControl=new UCLoginControl ();
                uc.Dock=DockStyle.Fill;
              }
              return loginControl; 
           }
        }
    }
    
    
    
    //--------------somewhere in your Form1.cs-------------
    
    UCLoginControl uc=UIFactory.LoginControl;
    //populate with data
    panel2.Controls.Add(uc);
    




    .NET guy
    • Proposed as answer by tsadigov Wednesday, March 3, 2010 4:09 PM
    • Marked as answer by Helen Zhou Tuesday, March 9, 2010 9:43 AM
    Wednesday, March 3, 2010 4:08 PM
  • create method:

    private void LoadControl(string controlName)

            {

                UserControl control = new UserControl(); 

                Panel1.Controls.Clear();   

                Type currentType = Type.GetType(controlName, true);

                control = (UserControl)Activator.CreateInstance(currentType);

                Panel1.Controls.Add(control);   

            }

     

    Call this Method:

    LoadControl("JPMS.UserManagement");

     

     

     

     

     


    Tuesday, March 22, 2011 10:37 AM