dcsimg
Results 1 to 11 of 11

Thread: [RESOLVED] Dynamically added usercontrol, how to remove added control from Form?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    655

    Resolved [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

  2. #2
    Member gilman's Avatar
    Join Date
    Jan 2017
    Posts
    44

    Re: Dynamically added usercontrol, how to remove added control from Form?

    if you go through the collection through a loop for next you can not modify the collection in the loop.
    Try
    Code:
    Sub RemoveControl()
        MyForm.Controls.Remove "My_UserControl"
    End Sub

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    655

    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

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    655

    Re: Dynamically added usercontrol, how to remove added control from Form?

    double post - delete this

  5. #5
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,458

    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

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    655

    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
    MSDN document
    https://docs.microsoft.com/en-us/off...text-error-365
    lists possible causes, but none of them adapt to this case.
    Attached Files Attached Files
    Last edited by Tech99; May 22nd, 2019 at 04:46 AM.

  7. #7
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,709

    Re: Dynamically added usercontrol, how to remove added control from Form?

    You are probably trying to remove the control from an event of the control.

    Post a sample with the problem.

    And the size of the project has nothing to do for sure.

  8. #8
    Member gilman's Avatar
    Join Date
    Jan 2017
    Posts
    44

    Re: Dynamically added usercontrol, how to remove added control from Form?

    Quote Originally Posted by Tech99 View Post
    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
    And works fine, the problem must be other.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    655

    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?
    Attached Files Attached Files

  10. #10
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,709

    Re: Dynamically added usercontrol, how to remove added control from Form?

    Quote Originally Posted by Tech99 View Post
    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.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    655

    Re: Dynamically added usercontrol, how to remove added control from Form?

    Ok, thanks Eduardo. Sample solution attached.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width