Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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;

VB
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
            Form1.Controls.Add(pan)
            ' also tried
            ' Me.Controls.Add(pan)
        End With
End Sub
End Class
Posted
Comments
Sergey Alexandrovich Kryukov 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?
--SA
[no name] 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]
Sergey Alexandrovich Kryukov 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).
--SA
Richard C Bishop 7-Dec-12 17:39pm    
You never asked a question or else Sergey would not have said what he said.
Sergey Alexandrovich Kryukov 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.
--SA

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:
VB
My.Forms.Form1.Controls.Add(something)


From a different assembly, you just have to make the instance of Form1 available to it.
 
Share this answer
 
v2
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.

—SA
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900