Click here to Skip to main content
13,558,844 members
Rate this:
Please Sign up or sign in to vote.
I am trying to add some controls to the Main(single) Form but without success.
From the Form within thats no problem, adding it from Sub New() or Load() but from another Class i am not able to do it. An example here below;

I am trying to make a .dll with some special functions on it, thats why i need to add dynamically some Controls to the Main Form

Simple Example;

Public Class Form1

End Class

<System.Runtime.InteropServices.ComVisibleAttributte(True)> _
Public Class Example : Inherits UserControl

Sub New()
        Dim pan As New Panel
        With pan
            .BackColor = Color.Blue
            .Size = New Size(33, 33)
            .Location = New Point(10, 10)
            .Visible = True
            ' also tried
            ' Me.Controls.Add(pan)
        End With
End Sub
End Class
Posted 7-Dec-12 10:01am
SAKryukov 7-Dec-12 16:11pm
There is no such notion as "add from class". What do you mean? (Or what do you mean by "from form"?) Problem is not clear. "I'm not able to do it" is not informative. What happens, exactly?
bEGI23 7-Dec-12 16:28pm
I am creating a .dll which will make changes to the Form, exactly, it will add some controls to it.
So the End User will set a reference to my .dll in order to get the desired visual effects of it.
Now my question was very clear and im speaking english so one more time am i possible to dynamically add control from this dll to that Windows.Form which will load it.
[I also declare a Public variable in that dll which represents the Windows.Form Name. Then on Windows.Form_Load it will set its name to that Public Variable in the .dll With this then it knows on which Windows.Form to add those Controls if more than 1 is present]
SAKryukov 7-Dec-12 18:20pm
You did not answer my question. There is no problem to add a control "dynamically"; in fact, all controls are always added dynamically, there is no "non-dynamic" way.
No, you language is not quite English, but I think I understand it. It absolutely does not matter in what DLL (there no "DLLs"; in .NET, there are modules and assemblies; "DLL" is nothing more than a file name pattern, which can be anything at all).
richcb 7-Dec-12 17:39pm
You never asked a question or else Sergey would not have said what he said.
SAKryukov 7-Dec-12 18:25pm
I finally can see what's wrong. The code would not compile, but OP never explained what's the problem. I answered, please see.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Not Form1.Controls.Add(pan), but just Controls.Add(pan). See also my comments.

Form1 is some type, the base type for Example. The type does not have the property Control you would be able to call, but its instance has. This is an instance property, not static property.

Finally, I figured this out: the problem is that you don't understand the very basics; in this case, this is understanding of types and their instances (objects). There is a lot more to understand before you can get to UI development and proceed independently. I can advise to step back and learn basics of programming, including OOP. For that, I would recommend doing your exercises on simple console applications.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Form1 class does not have the member Controls. If it inherited System.Windows.Forms.Form, you still cannot add a control like you did. You must add it to an instance of Form1.

From the same assembly as the Form1 you could try this:

From a different assembly, you just have to make the instance of Form1 available to it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01-2016 | 2.8.180515.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100