[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1489
  • Last Modified:

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.

0
emi_sastra
Asked:
emi_sastra
  • 3
  • 2
  • 2
  • +2
1 Solution
 
GrahamSkanCommented:
AFAIK, you can only create a control array at design time. I'm pretty sure that fact is in the documentation.
0
 
emi_sastraAuthor Commented:
Try this link:

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

Then you will know it, It can or not.

Thank you.
0
 
GeneMCommented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
emi_sastraAuthor Commented:
Thanks for the link.

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

Thank you.
0
 
clarkscottCommented:
.... 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
0
 
EDDYKTCommented:
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

0
 
EDDYKTCommented:
lblNameToCreate As ojbect

should be

lblNameToCreate As object
0
 
GeneMCommented:
Emi,

Since you have not closed this out, I assume you are still having a problem.  You posted this in the vb.net forum, so I am assuming you are using VS2005.  The following is some sample code which builds 7 buttons on a form.  To use this code, create a form and put two buttons on it (Button1 and Button2).  The code is very simple.

I am having a problem with your code.  I just thought maybe this would help you out.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'This sub builds 7 buttons on the form
        For i As Integer = 1 To 7
            Dim newMemberButton As New System.Windows.Forms.Button

            With newMemberButton
                .Visible = True
                .Size = New Size(30, 30)
                .Location = New Point(40 * i, 40)
                .Text = i.ToString
                .Name = "B" & i.ToString
            End With

            Me.Controls.Add(newMemberButton)
            AddHandler newMemberButton.Click, AddressOf newMemberButton_Click
        Next i
    End Sub

    Private Sub newMemberButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'This sub handles the click of the 7 added buttons.
        Dim tempButton As New Button
        tempButton = sender
        MessageBox.Show(tempButton.Text)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'This sub scans the buttons looking for one whose text is "3"
        Dim control As Control
        For Each control In Me.Controls
            ' Check each control to see if it's a Button
            If TypeOf (control) Is Button Then
                Dim button As Button = control
                ' Check each Button for Text
                If button.Text = "3" Then

                    MessageBox.Show("Button " & button.Name & " Has TEXT 3")
                    button.BackColor = Color.Yellow
                    Me.ActiveControl = button
                    Return
                End If
            End If
        Next
    End Sub
End Class

GeneM
0
 
emi_sastraAuthor Commented:
It solve my problem.

Thank you very much.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now