create a method to add any user control to panel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perdijc
    New Member
    • Sep 2006
    • 9

    create a method to add any user control to panel

    I have a form with a split container. On the left panel there are buttons where I click to show the user control on the right panel.

    I would like to create a method to assign in each button to add the user control to left panel. I tried the following code but it doesn't work.

    Could you help me?

    private void addUserControls (UserControl uc)
    {
    splitContainer1 .Panel2.Control s.Clear();
    uc = new UserControl();

    splitContainer1 .Panel2.Control s.Add((UserCont rol)uc);
    uc.Dock = DockStyle.Fill;
    }

    private void btnInfo_Click(o bject sender, EventArgs e)
    {
    addUserControls (frmCargo);
    }
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    I tried the following code but it doesn't work.
    What do you mean by does not work?

    Any errors? What was the expectation you have and what is the output?

    Regards
    Dheeraj Joshi

    Comment

    • ThatThatGuy
      Recognized Expert Contributor
      • Jul 2009
      • 453

      #3
      Have you tried this.Invalidate () or this.Refresh after adding the controls

      Comment

      • perdijc
        New Member
        • Sep 2006
        • 9

        #4
        The goal of this method is to pass the name of the uer control and add it to the panel.

        On the buton I run the following code:
        private void btnShip_Click(o bject sender, EventArgs e)
        {
        addUserControls (frmCargo);
        }

        When I run the method, it doesn't work I mean there is error.
        The error is the following
        'DB_Lift_CS.frm Cargo' is a 'type' but is used like a 'variable'.

        Could you help me how canI passa the name of a user control and add it t the panel?

        Thanks
        Rards,

        Jose

        Comment

        • perdijc
          New Member
          • Sep 2006
          • 9

          #5
          the goal of this method is to pass the name of the user control and add it to the panel (in this case, add t splct container panel2).

          Method:
          private void addUserControls (UserControl uc)
          {
          splitContainer1 .Panel2.Control s.Clear();
          uc = new UserControl();
          splitContainer1 .Panel2.Control s.Add(uc);
          uc.Dock = DockStyle.Fill;
          }


          On the buton I add the followin code:

          private void btnShip_Click(o bject sender, EventArgs e)
          {
          addUserControls (frmShip);
          }

          The method doesn't work, it shows the following error:
          'DB_Lift_CS.frm Cargo' is a 'type' but is used like a 'variable'

          Could you help me howcan I pass the name of the user control and add it to the panel?

          Thanks
          Regards

          Jose

          Comment

          • Christian Binder
            Recognized Expert New Member
            • Jan 2008
            • 218

            #6
            You cannot pass a typename! You have to pass an object of this type.
            E.g.
            Code:
            private void btnShip_Click(object sender, EventArgs e)
            {
              addUserControls(new frmShip());
            }

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              In addition to CHBinder's post...

              Code:
              private void addUserControls(UserControl uc)
              {
              splitContainer1.Panel2.Controls.Clear();
              uc = new UserControl();
              
              splitContainer1.Panel2.Controls.Add((UserControl)u c);
              uc.Dock = DockStyle.Fill;
              }
              
              private void btnInfo_Click(object sender, EventArgs e)
              {
              addUserControls(frmCargo);
              }
              A couple of things above. I'm guessing it's a typo but on line 6 you've got a space between u and c. If you're going to new a parameter variable, usually it means you want that object to come out of your method as well. If that's the case, you need to pass the parameter as a reference so that you can modify the reference object.

              For example...
              Code:
              public void MyMethod(ref MyObject obj) { obj = new MyObject(); }
              However in your case I'm not sure if that's what you want to accomplish. Are you trying to pass a type to the method and have that method add a new object of that type to the panel? If so, you'll need to actually pass the type you want... for example

              Code:
              private void addUserControls(Type typeToCreate)
              Then, inside your code you can do an if-check on that type and create the appropriate object, as an example...

              Code:
                      public class MyControlA : UserControl { ... }
                      public class MyControlB : UserControl { ... }
                      // etc ...
              Code:
                      public UserControl CreateControl(Type typeToCreate)
                      {
                          UserControl newControl = null;
              
                          if (typeToCreate == typeof(MyControlA))
                          {
                              newControl = new MyControlA();
                              Console.WriteLine("Creating new MyControlA object!");
                          }
                          else if (typeToCreate == typeof(MyControlB))
                          {
                              newControl = new MyControlB();
                              Console.WriteLine("Creating new MyControlB object!");
                          }
                          else
                          {
                              Console.WriteLine("Unknown type, could not create new control!");
                          }
              
                          return newControl;
                      }
              Code:
                          UserControl control1 = CreateControl(typeof(MyControlB));
                          UserControl control2 = CreateControl(typeof(MyControlA));
                          UserControl control3 = CreateControl(typeof(string));
              This would generate...

              Creating new MyControlB object!
              Creating new MyControlA object!
              Unknown type, could not create new control!
              If that's the kind of thing you're going for, hopefully that gives you some ideas :)

              *Edit: Oh, one more thing... and congrats if you've stuck with me this far! This kind of process is very related to plugins, and are very helpful. A while back I implemented plugins for a project I was working on. I used the following page as a reference to learn them. Great article! I don't know if it's exactly what you want, but it's a good read.

              Last edited by GaryTexmo; Mar 26 '10, 02:42 PM. Reason: Added plugin info

              Comment

              • perdijc
                New Member
                • Sep 2006
                • 9

                #8
                It works, this is what I want.

                Thans a lot,

                Jose

                Comment

                Working...