Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dynamically Loading Custom Control

Dynamically Loading Custom Control

Scheduled Pinned Locked Moved C#
questionwinforms
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt Cavanagh
    wrote on last edited by
    #1

    Ok so im making a program thats made up of lots of custom user controls. Basically like plugins. Now it works great with all my "plugins" by dragging them onto the form, but what I want is that the user can add the dll's of the controls he wants and my program dynamically loads them in(and I will size and place them etc in the code). Im sure this is possible as tons of apps do it. So what is the best way? Shot :D

    S 0 2 Replies Last reply
    0
    • M Matt Cavanagh

      Ok so im making a program thats made up of lots of custom user controls. Basically like plugins. Now it works great with all my "plugins" by dragging them onto the form, but what I want is that the user can add the dll's of the controls he wants and my program dynamically loads them in(and I will size and place them etc in the code). Im sure this is possible as tons of apps do it. So what is the best way? Shot :D

      S Offline
      S Offline
      SpiveyC
      wrote on last edited by
      #2

      Is this a winform or an Web page

      M 1 Reply Last reply
      0
      • S SpiveyC

        Is this a winform or an Web page

        M Offline
        M Offline
        Matt Cavanagh
        wrote on last edited by
        #3

        Sorry, its a winform. Would be useful if it would work when I ran it on linux with mono(just to be future proof).

        1 Reply Last reply
        0
        • M Matt Cavanagh

          Ok so im making a program thats made up of lots of custom user controls. Basically like plugins. Now it works great with all my "plugins" by dragging them onto the form, but what I want is that the user can add the dll's of the controls he wants and my program dynamically loads them in(and I will size and place them etc in the code). Im sure this is possible as tons of apps do it. So what is the best way? Shot :D

          0 Offline
          0 Offline
          0x3c0
          wrote on last edited by
          #4

          You first need a method of loading an assembly from a path. You can use Assembly.LoadFrom for that. Then you can call GetTypes on the result from that; you'll get a collection of Types from that. Iterate through each of those, and check to see if it inherits from Control. If it does, call Activator.CreateInstance and cast it to a Control instance. Then you just have to add it to a form and do your sizing

          Between the idea And the reality Between the motion And the act Falls the Shadow

          M 2 Replies Last reply
          0
          • 0 0x3c0

            You first need a method of loading an assembly from a path. You can use Assembly.LoadFrom for that. Then you can call GetTypes on the result from that; you'll get a collection of Types from that. Iterate through each of those, and check to see if it inherits from Control. If it does, call Activator.CreateInstance and cast it to a Control instance. Then you just have to add it to a form and do your sizing

            Between the idea And the reality Between the motion And the act Falls the Shadow

            M Offline
            M Offline
            Matt Cavanagh
            wrote on last edited by
            #5

            Sounds good, thanks alot :)

            1 Reply Last reply
            0
            • 0 0x3c0

              You first need a method of loading an assembly from a path. You can use Assembly.LoadFrom for that. Then you can call GetTypes on the result from that; you'll get a collection of Types from that. Iterate through each of those, and check to see if it inherits from Control. If it does, call Activator.CreateInstance and cast it to a Control instance. Then you just have to add it to a form and do your sizing

              Between the idea And the reality Between the motion And the act Falls the Shadow

              M Offline
              M Offline
              Matt Cavanagh
              wrote on last edited by
              #6

              Cool got it working. Thanks alot! By the way for anyone reading this heres the code I used excluding the obvious positioning and fluff: <pre> Assembly asm = Assembly.LoadFrom(dlldirectory); Type typ = asm.GetType("My_Control.UserControl1"); UserControl uc = (UserControl)Activator.CreateInstance(typ); this.Controls.Add(uc); </pre>

              Strive to be humble enough to take advice, and confident enough to do something about it.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups