Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET Controls form
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
            Form1.Controls.Add(pan)
            ' also tried
            ' Me.Controls.Add(pan)
        End With
End Sub
End Class
Posted 7-Dec-12 10:01am
Comments
Sergey Alexandrovich Kryukov at 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
begi21 at 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 at 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
richcb at 7-Dec-12 17:39pm
   
You never asked a question or else Sergey would not have said what he said.
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
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.
 
—SA
  Permalink  
v2
Rate this: bad
good
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:
My.Forms.Form1.Controls.Add(something)
 
From a different assembly, you just have to make the instance of Form1 available to it.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 390
1 Nirav Prabtani 273
2 _Amy 255
3 Richard Deeming 250
4 CHill60 195
0 _Amy 110
1 Maciej Los 90
2 OriginalGriff 85
3 Mehdi Gholam 80
4 Sergey Alexandrovich Kryukov 75


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2014
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