[RESOLVED] Dynamically added usercontrol, how to remove added control from Form?
How to remove (unload) dynamically added usercontrol from form?
Controls are added and placed in form, dynamically during runtime. When tried to remove, error 365 is raised.
So is it possible to remove controls, which are added during runtime.
Code:
Dim WithEvents My_UserControl As VBControlExtender 'Form level declaration
'Form subroutines
Sub AddControl ()
Set My_UserControl = Controls.Add("MyProject.My_UserControl", "My_UserControl")
My_UserControl.Top = 2250
My_UserControl.Left = 90
My_UserControl.ZOrder 0
My_UserControl.Visible = True
End Sub
Sub RemoveControl ()
Dim C As Control
For Each C In MyForm.Controls
If sCtlName = "My_UserControl" Then MyForm.Controls.Remove (C.Name) 'Run-time error 365 -> Unable to unload within this context.
Next
End Sub
Re: Dynamically added usercontrol, how to remove added control from Form?
Removing by referencing control name in string variable or static name, does not work either.
[CODE]
Dim WithEvents My_UserControl As VBControlExtender 'Form level declaration
Dim PreviousCtlName as string
'Form subroutines
Sub AddControl ()
Set My_UserControl = Controls.Add("MyProject.My_UserControl", "My_UserControl")
PreviousCtlName = My_UserControl.Name
My_UserControl.Top = 2250
My_UserControl.Left = 90
My_UserControl.ZOrder 0
My_UserControl.Visible = True
End Sub
Sub RemoveControl ()
If Len(PreviousCtlName) Then MyForm.Controls.Remove (PreviousCtlName) 'Run-time error 365 -> Unable to unload within this context.
'OR
MyForm.Controls.Remove ("My_UserControl") 'Run-time error 365 -> Unable to unload within this context.
End Sub
Re: Dynamically added usercontrol, how to remove added control from Form?
A basic sample without error checking, but does this concept work for you?
Code:
Option Explicit
Private Sub Command1_Click()
Dim TB As TextBox
Set TB = Controls.Add("VB.TextBox", "txt2")
TB.Visible = True
TB.Top = Command1.Top + Command1.Height * 2
TB.Left = Command1.Left
TB.Text = "My first textbox"
End Sub
Private Sub Command2_Click()
RemoveControl "txt2"
End Sub
Private Sub RemoveControl(ByVal sName As String)
Dim i As Long
For i = Controls.Count - 1 To 0 Step -1
If Controls(i).Name = sName Then Controls.Remove i
Next i
End Sub
Re: Dynamically added usercontrol, how to remove added control from Form?
Yes, that works with intrinsic VB controls OK, but not with usercontrols in rather large project**. I made an simple test project, where removing works ok.
Code:
Dim WithEvents My_UserControl As VBControlExtender 'Form level declaration
Dim PreviousCtlName as string
'Form subroutines
Sub AddControl ()
Set My_UserControl = Controls.Add("MyProject.My_UserControl", "My_UserControl")
PreviousCtlName = My_UserControl.Name
My_UserControl.Top = 2250
My_UserControl.Left = 90
My_UserControl.ZOrder 0
My_UserControl.Visible = True
End Sub
Sub RemoveControl ()
For i = Controls.Count - 1 To 0 Step -1
If Controls(i).Name = PreviousCtlName Then Controls.Remove i 'Run-time error 365 -> Unable to unload within this context.
Next i
End Sub
Re: Dynamically added usercontrol, how to remove added control from Form?
Originally Posted by Tech99
Removing by referencing control name in string variable or static name, does not work either.
[CODE]
Dim WithEvents My_UserControl As VBControlExtender 'Form level declaration
Dim PreviousCtlName as string
'Form subroutines
Sub AddControl ()
Set My_UserControl = Controls.Add("MyProject.My_UserControl", "My_UserControl")
PreviousCtlName = My_UserControl.Name
My_UserControl.Top = 2250
My_UserControl.Left = 90
My_UserControl.ZOrder 0
My_UserControl.Visible = True
End Sub
Sub RemoveControl ()
If Len(PreviousCtlName) Then MyForm.Controls.Remove (PreviousCtlName) 'Run-time error 365 -> Unable to unload within this context.
'OR
MyForm.Controls.Remove ("My_UserControl") 'Run-time error 365 -> Unable to unload within this context.
End Sub
I try
Code:
Option Explicit
Dim WithEvents My_UserControl As VBControlExtender 'Form level declaration
'Form subroutines
Sub AddControl()
Set My_UserControl = Controls.Add("ViasEnvio.ctlViaEnvio", "My_UserControl")
My_UserControl.Top = 2250
My_UserControl.Left = 90
My_UserControl.ZOrder 0
My_UserControl.Visible = True
End Sub
Sub RemoveControl()
Controls.Remove ("My_UserControl")
End Sub
Private Sub Form_Load()
AddControl
RemoveControl
End Sub
Re: Dynamically added usercontrol, how to remove added control from Form?
Eduardo, you are right, i am swithcing usercontrol from Combobox click event. Sample attached.
So how to remedy situation?
One possible solution is to add timer and boolean variable bRemovePreviousControl and execute control removing code from there.
Any other possibility without timer?
Re: Dynamically added usercontrol, how to remove added control from Form?
Originally Posted by Tech99
Eduardo, you are right, i am swithcing usercontrol from Combobox click event. Sample attached.
So how to remedy situation?
One possible solution is to add timer and boolean variable bRemovePreviousControl and execute control removing code from there.
Any other possibility without timer?
Yes, with a timer. That's the way to do it. You need to decouple it from the event.
→ The Comprehensive Guide to Cloud Computing
A complete overview of Cloud Computing focused on what you need to know, from selecting a platform to choosing a cloud vendor.