remove controls

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
524
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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,983
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:

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
524
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
 

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
524
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,282
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

What do you mean 'when the new controls show up'?
 

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
524
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.
 

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
524

ADVERTISEMENT

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
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,983
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,282
Office Version
  1. 365
Platform
  1. Windows
What's this userform's purpose?
 

Watch MrExcel Video

Forum statistics

Threads
1,129,587
Messages
5,637,271
Members
416,963
Latest member
samfuge

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
Top