Link to home
Start Free TrialLog in
Avatar of CHSrinivas
CHSrinivas

asked on

Adding controls dynamically...

Hi,

In my application iam generating a layout with label controls in the form onload event. In the onload event it will read the properties like X-position, Y-position and name from the database table. I want to delete the control and from the layout and to update the database table. If i tried to delete the control using Form1.Cotrols.Remove "CtrlName"  Its giving "Controls.Remove can only remove controls added with Controls.Add".

Here is my code:

Private Sub Form_Load()

Dim TabCount As Integer
Dim objCmd As New ADODB.Command
Dim objRec As New ADODB.Recordset

objCmd.ActiveConnection = "dsn=Floordsn"
objCmd.CommandText = "sp_FloorItems_FetchAll"
objCmd.CommandType = adCmdStoredProc

objRec.Open objCmd
 intIndex = 1
While Not objRec.EOF
   
    Call Controls.Add("VB.Label", "Label0" & intIndex)
    Load Label0(Label0.Count)
    With Label0(intIndex)
            .Visible = True
            .Width = 800
            .Top = objRec("FL_PosY")
            .Left = objRec("FL_PosX")
            .Height = 195
            .Caption = objRec("FL_Type")
        End With
    intIndex = intIndex + 1
   
    objRec.MoveNext
Wend


'Close the recordset and delete the objects
objRec.Close

Set objRec = Nothing
Set objCmd = Nothing
End Sub

Private Sub Label0_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then
       
        mnuSub(0).Visible = True
        While mnuSub.UBound > 0
            Unload mnuSub(mnuSub.UBound)
        Wend

        'Load menu items
        Load mnuSub(1): mnuSub(1).Caption = "Delete": mnuSub(1).Tag = Index
       
        mnuSub(0).Visible = False
        PopupMenu mnuFile
    End If
End Sub

Private Sub mnuSub_Click(Index As Integer)
   
   Form1.Controls.Remove Label0(2)
End Sub
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi CHSrinivas,

You need to use the UNLOAD statement instead:

Private Sub mnuSub_Click(Index As Integer)
   
   Unload Label0(2)
End Sub

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
Avatar of CHSrinivas
CHSrinivas

ASKER

Hi TimCottee

Thank you for your solution. Its working fine.

Cheers
Srinivas
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can I just mention

Call Controls.Add("VB.Label", "Label0" & intIndex)
Load Label0(Label0.Count)

Creating label01,label02,label03 etc.
Then the next statement is lading alabel in the control array Label0.

I think first off you should remove this line as it is doing nothing it is not referenced in your code.

Call Controls.Add("VB.Label", "Label0" & intIndex)

and as TimCottee stated unload the control array elements as folows.

Unload Label0(SomeIndex)

If you had intended to use

Call Controls.Add("VB.Label", "Label0" & intIndex)

you would then need to call

Call Controls.Remove "Label0" & intIndex


Regards


John McCann