Link to home
Create AccountLog in
Avatar of emi_sastra
emi_sastra

asked on

How to Create Control Array Dynamically at Run Time?

Hi,

I have a procedure to create control array at run time. But it still has error when try to create it.

In my form, I have a label "lblDuplicate" control array index =0 and combobox call cbodata.

I call :

Create_LabelArray Me, lblDuplicate(1), 1, cboData, Frame1

in module:

Public Sub Create_Label_Control_Array(frmFormName As Object, _
                                          lblNameToCreate As Label, _
                                          intLabelIndex As Integer, _
                                          objClone As Object, _
                                          fraName As Object)

   ' Set lblNameToCreate = frmFormName.Controls.Add("VB.Label", strLabelName)
   
    Load lblNameToCreate(intLabelIndex)
    Set lblNameToCreate(intLabelIndex).Container = fraName
   
    Set lblNameToCreate(intLabelIndex).Font = objClone.Font
   
    If TypeOf objClone Is ComboBox Then
       lblNameToCreate(intLabelIndex).Top = objClone.Top + 20
    Else
       lblNameToCreate(intLabelIndex).Top = objClone.Top + 50
    End If
   
    lblNameToCreate(intLabelIndex).Left = objClone.Left
    lblNameToCreate(intLabelIndex).Height = objClone.Height - 60
    lblNameToCreate(intLabelIndex).Width = objClone.Width
   
    lblNameToCreate(intLabelIndex).Appearance = 0
    lblNameToCreate(intLabelIndex).BorderStyle = 1
   
    lblNameToCreate(intLabelIndex).Visible = True
   
End Sub

At complile time it shows : "Wrong argument".

Please help how to solve this.

Thank you.

Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

AFAIK, you can only create a control array at design time. I'm pretty sure that fact is in the documentation.
Avatar of emi_sastra
emi_sastra

ASKER

Try this link:

http://www.vbexplorer.com/VBExplorer/vb_feature/april2000/april2000.asp

Then you will know it, It can or not.

Thank you.
There is a discussion of control arrays at
http://msdn2.microsoft.com/en-us/library/kxt4418a(VS.71).aspx

This document seems to verify that control arrays are no longer supported in vb.net.  However, it gives you a way to kind of simulate control arrays.

Good luck.
Thanks for the link.

How about my code there?
What should be changed to make it works.

Thank you.
.... I didn't follow all these linkes but....
You can copy a control at runtime.  You could place what ever controls you want on a form and copy these to create new ones.  You can keep the originals invisible and stack them at the bottom of the form.  Copy and use them as needed.

Scott C
you can do something like that

Create_Label_Control_Array Me, lblDuplicate, 1, cboData, Frame1


Public Sub Create_Label_Control_Array(frmFormName As Object, _
                                          lblNameToCreate As ojbect, _
                                          intLabelIndex As Integer, _
                                          objClone As Object, _
                                          fraName As Object)

   ' Set lblNameToCreate = frmFormName.Controls.Add("VB.Label", strLabelName)
   
    Load lblNameToCreate(intLabelIndex)
    Set lblNameToCreate(intLabelIndex).Container = fraName
   
    Set lblNameToCreate(intLabelIndex).Font = objClone.Font
   
    If TypeOf objClone Is ComboBox Then
       lblNameToCreate(intLabelIndex).Top = objClone.Top + 20
    Else
       lblNameToCreate(intLabelIndex).Top = objClone.Top + 50
    End If
   
    lblNameToCreate(intLabelIndex).Left = objClone.Left
    lblNameToCreate(intLabelIndex).Height = objClone.Height - 60
    lblNameToCreate(intLabelIndex).Width = objClone.Width
   
    lblNameToCreate(intLabelIndex).Appearance = 0
    lblNameToCreate(intLabelIndex).BorderStyle = 1
   
    lblNameToCreate(intLabelIndex).Visible = True
   
End Sub

lblNameToCreate As ojbect

should be

lblNameToCreate As object
ASKER CERTIFIED SOLUTION
Avatar of GeneM
GeneM

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
It solve my problem.

Thank you very much.