Newsgroup: it.comp.lang.visual-basic
Titolo: Controlli a run-time

Vorrei aggiungere a run time un ( o una matrice di ) controllo ( es una
CommandButton) ad una form.
Qualcuno nel NG puņ aiutarmi per favore ??
Grazie in anticipo
Massimo Danieli
 
bj...@tin.it

...


>    
bj...@tin.it

esempio Label(0)
Vai sul pesante, allora :)
Dunque, ho trovato il metodo Controls nella classe Form della library VB col
Visualizzatore Oggetti.
Restituisce una collection di controlli sul form (o su qualunque container).
Poi ho debuggato un form vuoto, ho messo Form1.Controls nella Watch window e
ho visto effettivamente i controlli.
Quando poi ho tentato con l'Add, mi ha dato errore nel numero di argomenti.
Qui mi sono fermato e ho consultato l'msdn, con search su "add control" e ho
tirato fuori quanto segue.
E' un articolo, seguito da un esempio. L'articolo si perde un po' su come
registrare controlli non standard o con licenza
(nota bene: chi non possiede l'msdn puo' sempre andare sul sito Microsoft e
consultarla. se non sto dicendo una cazzata, dovrebbe essere libera)
Enrico
------------------------------------
Add Method (Controls Collection)
Adds a control to the Controls collection and returns a reference to the
control.
Syntax
object.Add (ProgID, name, container)
The Add method syntax has these parts:
Part Description
object Required. Anobject expression that evaluates to an object in the
Applies To list.
ProgID Required. A string that identifies the control. The ProgID of most
controls can be determined by viewing the Object Browser. The ProgID is
composed of the Library and Class of the control. For example, the
CommandButton control's ProgID is VB.CommandButton. In cases where the
ProgID differs from that shown in the Object Browser, Visual Basic displays
an error message that contains the correct ProgId.
name Required. A string that identifies the member of the collection.
container Optional. An object reference that specifies a container of the
control. If not specified or NULL, defaults to the container to which the
Controls collection belongs. You can put a control in any existing container
control (such as the Frame control) by specifying this argument. A user
control or an ActiveX document can also be a container.
Remarks
Note   The Controls collection is a late-bound collection. This means the
compiler cannot determine in advance which controls are contained by the
collection, their objects or their interfaces. Without this information, the
Auto Statement Builder will not function.
This method allows you to add controls to an application at run time.
Dynamic control addition can be used to add the functionality of a control
to an application, even after the application has been compiled and
deployed. For example, you may have several complex user controls, each
suited to a different task. Depending on an external factor, such as time or
date or user input, a different user control could be added to an existing
form in an application. You can also use the container argument of the
method to specify a container control (such as the Frame control) to
position the control. Or you can design an application that automatically
reads a file, database, or registry entry for new controls to load. In this
way, you can modify an application without having to redeploy it.
Important   When you add an unreferenced control that requires a license to
an existing (deployed) application, you must also add the license key for
the control before using the Add method. For information on when and how to
add licenses, see "Licenses Collection" in the See Also list.
Adding Unreferenced Controls at Run Time
You can also use the Add method to dynamically add a control that is not
referenced in the project. (An "unreferenced" control is a control that is
not present in the Toolbox.) To do so, you must also add the control's
License key to the Licenses collection as well. The example below adds a
control's license key before adding the control itself:
Option Explicit
Private WithEvents extCtl As VBControlExtender
Private Sub Form_Load()
   Licenses.Add "prjWeeks.WeeksCtl", "xydsfasfjewfe"
   Set extCtl = Form1.Controls.Add("prjWeeks.WeeksCtl", "ctl1")
   extCtl.Visible = True ' The control is invisible by default.
End Sub
Note   See Add Method (Licenses Collection) in the See Also list for more
information about retrieving a control's license key.
In order to program the events of such an unreferenced control, however, you
must declare an object variable using the WithEvents keyword as a
VBControlExtender object (shown above), and set the object variable to the
reference returned by the Add method. Then use the VBControlExtender
object's ObjectEvent event to program the control's events. An abbreviated
example is shown below.
Option Explicit
Dim WithEvents objExt As VBControlExtender ' Declare VBControlExtender
variable
Private Sub LoadControl()
   Licenses.Add "Project1.Control1", "xydsfasfjewfe"
   Set objExt = Controls.Add("Project1.Control1", "myCtl")
   objExt.Visible = True
End Sub
Private Sub extObj_ObjectEvent(Info As EventInfo)
   ' Program the events of the control using Select Case.
   Select Case Info.Name
   Case "Click"
      ' Handle Click event here.
   ' Other cases now shown
   Case Else ' Unknown Event
      ' Handle unknown events here.
   End Select
End Sub
Note   You can't assign an intrinsic control to the VBControlExtender
variable; any attempt will result in a type mismatch error.
You can also program the events of a dynamically added control by declaring
an object variable using the WithEvents keyword, and setting the reference
returned by the method to the variable, as shown below:
Option Explicit
' Declare object variable as CommandButton.
Private WithEvents cmdObject As CommandButton
Private Sub Form_Load()
   Set cmdObject = Form1.Controls.Add("VB.CommandButton", "cmdOne")
   cmdObject.Visible = True
   cmdObject.Caption = "Dynamic CommandButton"
End Sub
Private Sub cmdObject_Click()
    Print "This is a dynamically added control"
End Sub
If you intend to add a user control or any ActiveX control to your form, you
must either add the control to the Toolbox, or add its License key to the
Licenses collection. See the Add Method (Licenses Collection) for more
information.
Note   If you add an ActiveX or user control to your project but don't use
it on a form, you must also uncheck the Remove Information About Unused
ActiveX Controls option on the Make tab of the Project Properties dialog
box. If your application attempts to add the control, the Add method will
fail because the necessary information has been discarded.
Removing Controls
To remove any controls added dynamically, use the Remove method. It should
be noted that you can only remove controls added using the Add method (in
contrast to controls added using the Load statement). The example below
removes a dynamically added control:
Form1.Controls.Remove "ctl1" ' The control's name is ctl1.
--------------------------------
questo e' l'esempio:
--------------------------------
Add Method (Controls Collection) Examples
Private Sub Form_Load()
   Form1.Controls.Add "VB.CommandButton", "cmdObj1", Frame1
   With Form1!cmdObj1
      .Visible = True
      .Width = 2000
      .Caption = "Dynamic Button"
   End With
End Sub
Note   The code example above uses ! as a syntax element. You can also use
standard collection syntax such as Form1.Controls("cmdObj1") to reference
the control.
This second example declares an object variable of type CommandButton using
the WithEvents keyword, allowing you to program the events of the control.
The object variable is set to the reference returned by the Add method. To
try the example, paste the code into the Declarations section and run the
project.
Option Explicit
Private WithEvents btnObj As CommandButton
Private Sub btnObj_Click()
   MsgBox "This is a dynamically added button."
End Sub
Private Sub Form_Load()
   Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
   With btnObj
      .Visible = True
      .Width = 2000
      .Caption = "Hello"
      .Top = 1000
      .Left = 1000
   End With
End Sub
The third example adds an unreferenced control to the Controls collection.
To program such a control's events, however, you must declare an object
variable of type VBControlExtender, and set the reference returned by the
method to the object. Then program the control's events using the
ObjectEvent event.
Option Explicit
Dim ctlExtender As VBControlExtender
Private Sub Form_Load()
   Set ctlExtender = Controls.Add("Project1.UserControl1", "MyControl")
   With ctlExtender
      .Visible = True
      .Top = 1000
      .Left = 1000
   End With
End Sub
Private Sub extObj_ObjectEvent(Info As EventInfo)
   ' Program the events of the control using Select Case.
   Select Case Info.Name
   Case "UserName"
      ' Check User name value.
      MsgBox Info.EventParameters("UserName").Value
   ' Other cases now shown
   Case Else ' Unknown Event
      ' Handle unknown events here.
   End Select
End Sub

esempio Label(0)
In VB5 non è possibile creare controlli, puoi solo generare copie di
controlli già esistenti sfruttando le matrici di controllo. Ho letto che
invece il VB6 permette di creare ex novo dei controlli e anche dei form a
run time anche senza generarli da un padre esistente, comunque ancora non ho
cominciato ad utilizzare la 6 (devo finire un progetto già iniziato con la
5). Comunque ho già fatto abbastanza programmi che hanno form con un
controllo per ogni tipo che intendo utilizzare e a seconda di quello che mi
serve lo aggiungo da un controllo già esistente. Fammi sapere se hai bisogno
di aiuto.
Ciao.

...


In VB5 Non č possibile creare nuovi controlli, č solo possibile aggiungerne
alla matrice di quel controllo.
In pratica se pensi di voler creare a Run time un controllo allora devi
averne creato almeno uno con la proprietą index valorizzata in modo che VB
crei una matrice per quel controllo.
In VB6 Non so ancora !
--
      Pino Saffioti
-----------------------------------
pino.s...@asl1.to.it


--
Paolo De Laurentiis
E.Mail: 
pd...@tin.it
HomePage 1: 
http://space.tin.it/io/pdelau
HomePage 2: 
http://www.freeweb.org/freeweb/pdelau

Object doesn't support this property or method (Error 438)
Il metodo Add per la collection Controls dell'IDE di VB è disponibile
soltanto a partire dal VB 6.0
Zeross

soltanto a partire dal VB 6.0
Confermo...
Ho giusto scritto una piccola applicazione che creava un form con i
controlli in modo dinamico, partenda da un file xml di configurazione

