Create and then destroy runtime controls
Hello,
i am developing a program that it will create some controls (labels, textboxes, command buttons and image controls) at runtime and i will destroy them and create others!
Can anyone help me please? I can't find a way to destroy the controls!
I can only create them and place them in order!
Re: Create and then destroy runtime controls
Have you tried .REMOVE on the CONTROLS collection?
Not sure it works, but I would assume it does.
Re: Create and then destroy runtime controls
I tried it as the CONTROLS.ADD but nothing happened!
Any other ideas?
Re: Create and then destroy runtime controls
To create the controls i use this code:
VB Code:
Public WithEvents Command1 As CommandButton
Private Sub Command1_Click()
MsgBox "Button Pressed"
End Sub
Private Sub Form_Load()
Set Command1 = Me.Controls.Add("VB.CommandButton", "Command1", Me)
With Command1
.Visible = True
.Width = 900
.Height = 900
.Left = Me.Width / 2 - .Width / 2
.Top = Me.Height / 2 - .Height / 2
.Caption = "Test Button"
End With
End Sub
.... The problem is how to remove them!
Re: Create and then destroy runtime controls
I started a project - added a command button called COMMAND2 and then posted your code and a bit more into the form. The caption for COMMAND2 is "REMOVE".
Code:
Public WithEvents Command1 As CommandButton
Private Sub Command1_Click()
MsgBox "Button Pressed"
End Sub
Private Sub Command2_Click()
Call Me.Controls.Remove("Command1")
End Sub
Private Sub Form_Load()
Set Command1 = Me.Controls.Add("VB.CommandButton", "Command1", Me)
With Command1
.Visible = True
.Width = 900
.Height = 900
.Left = Me.Width / 2 - .Width / 2
.Top = Me.Height / 2 - .Height / 2
.Caption = "Test Button"
End With
End Sub
This works for me.
Re: Create and then destroy runtime controls
Thank you szlamany! It works