remove controls

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
529
I've been adding dynamic buttons using some code like this:
Code:
Private Sub UserForm_Initialize()
Set collect3 = New Collection
Set ws = Sheets("sheets1")
iRow = (ws.Cells(Rows.Count, c3).End(xlUp).Row + 2)

butset = 3
butPERrow = 5
butRows = "0"
butH = 50
butW = 180
buTop = (butH + 1)
buLft = (butW + 1)
i = "0"

For Each cell In ws.Range("a1:a" & iRow).Cells
            Set ctr = UserForm1.Frame3.Controls.Add("Forms.CommandButton.1")
            ctr.Caption = cell.Value
            ctr.Font.Size = 17
            ctr.BackStyle = 1
            ctr.TabStop = False
            ctr.Font.Name = "Arial"
            ctr.Font.Bold = True
            ctr.WordWrap = True
            ctr.Name = cell.Value
            ctr.Height = butH
            ctr.Width = butW
            
            If i = butPERrow Then
                butRows = butRows + 1
                i = "0"
            End If
            
            ctr.Top = buTop * butRows
            ctr.Left = buLft * i
            
            Set ct3 = New Class3
            Set ct3.Group3 = ctr
            collect3.Add Item:=ct3, Key:=ctr.Name

            i = i + 1
nextitem:
Next cell
end sub


Works great...Later on in my code, I want to clear the slate....I've been doing it with...

Code:
    For Each ct3 In collect3
        UserForm1.Frame3.Controls.Remove ct3.Group3.Name
    Next ct3
    Set collect3 = New Collection

My problem is that it works...and then stops working after some time. It seems that some of the controls are missed when being removed and when the new controls show up, they have some old ones kicking about. Any idea's how I can get rid of those pesky controls in a reliable fashion?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
What is the Group3. property of your controls???
Alternatly, you could set the .Tag property of the controls to "made" as you add them and then loop through and remove that way.

Code:
For Each oneControl in UserForm1.Controls
    If oneControl.Tag = "made" Then
        oneControl.Parent.Remove oneControl.Name
    End If
Next oneControl

Also, when the userform is unloaded all of the added controls should disappear.
EDIT: I just noticed, as you are looping through collect3, you should set each ct3 = Nothing to de-instansice the made objects after removing the control from the Frame. You need to clean up the created object (Class3) as well as the added control (CommandButton).

Finally, rather than creating a bunch of commandbuttons, you could add items to a listbox and use the Listbox1_Change event rather than the NewCommandButtonCumClass3_Click event.
 
Last edited:
Upvote 0
Group3 is from my class3 where the buttons get they're functionality from....
I'm using buttons because it's for a touch screen application.

Code:
Option Explicit
 
Public WithEvents Group3 As MSForms.CommandButton
Private Sub Group3_click()
MsgBox " this is the code for group3 buttons"
End Sub
 
Upvote 0
what would I declare the oneControl as? It works until I move it into my class module..... I tried msforms.commandbutton, but it didn't work
 
Upvote 0
What do you mean 'when the new controls show up'?
 
Upvote 0
Hi Norie,
so if you have a userform and you create some dynamic buttons that fill the page, after pressing one of these buttons, I wanted to clear the dynamically made buttons and replace them with new buttons that had new instructions and criteria.......When I was removing them, they wouldn't always remove...some times they would stick around using the code that I was using. So I'm trying to adapt mikerickson's code for removal because it works...Except I'm having an issue with it running from my class module, so I need to declare the "oneControl" but am not sure how I'm to dim it.....
also, mikerickson mentioned set each ct3 to nothing.....Should I do that in the loop or outside? Any examples would be great.
 
Upvote 0
still have the problem...I must be missing something

Code:
Dim onecontrol As Control
For Each ct3 In collect3
For Each onecontrol In UserForm1.Controls
    If onecontrol.Tag = i Then
        onecontrol.Parent.Remove onecontrol.Name
    End If
Next onecontrol
ct3 = Nothing
Next ct3
 
Upvote 0
When looking at the userform while the "checkpoint" message box has halted code execution, all the command buttons should be removed.
Code:
Dim oneControl as Object

    For Each oneControl in UserForm1.Controls
        If oneControl.Tag = "made" Then
            oneControl.Parent.Remove oneControl.Name
        End If
    Next oneControl
msgbox "checkpoint"
    For each oneControl in collect3
        set oneControl = Nothing
    Next oneControl
    Set collect3 = Nothing
End Sub
 
Upvote 0
What's this userform's purpose?
 
Upvote 0

Forum statistics

Threads
1,214,572
Messages
6,120,306
Members
448,955
Latest member
Dreamz high

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top