Dynamically Loading Custom Control
-
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
-
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
-
Sorry, its a winform. Would be useful if it would work when I ran it on linux with mono(just to be future proof).
-
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
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
-
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
Sounds good, thanks alot :)
-
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
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.