Private Sub CreateCheckBox()
Dim StrChkBox As String
Dim Ctr     As Control For Each Ctr In Me.Controls
      If Left(Ctr.Name, 7) = "Chk_Box" Then
         Controls.Remove Ctr
         Else
      End If
 Next
  
 With MSHFLG
 For I = 1 To .Rows - 1
     .col = 2
     .Row = I
     StrChkBox = "Chk_Box_" & Trim(.TextMatrix(I, 1))
     Set Ctr = Me.Controls.Add("vb.checkbox", StrChkBox, Frame1)
     Ctr.Visible = True
       
     Ctr.Top = .CellTop + .Top - 10
     Ctr.Left = .CellLeft + .Left - 10
     Ctr.Width = .CellWidth - 2 * Screen.TwipsPerPixelX + 30
     Ctr.Height = .CellHeight - 2 * Screen.TwipsPerPixelY + 40
     Ctr.Caption = Trim(.TextMatrix(I, 1)) & "号机器"
     Ctr.ZOrder 0
 Next I
 End With
End Sub

解决方案 »

  1.   

    应该用 Unload 吧?
      

  2.   

    刚才试了,应该用 Controls.Remove 方法。是不是因为 Ctr 有时引用的不是 动态加载 的控件啊?
    出错时点‘调试’,检查一下 Ctr 是不是某个设计时加入的控件。
      

  3.   

    请输出自动添加的控件名,检查下控件名的前7个字符是不是Chk_Box。
      

  4.   

    width=80000 够了吧,把它弄到界面外
      

  5.   

    visible = false
    或left = -75000
      

  6.   

    Private WithEvents Check1 As CheckBox
    Dim Ctr As Control
    Private Sub Form_Load()
       Set Check1 = Controls.Add("VB.checkbox", "Check1", Me)
       Check1.Caption = ″新增的复选检查框″
       Check1.Visible = True
       Check1.Move Me.Width / 2, 0
    End SubPrivate Sub Command1_Click()
       For Each Ctr In Me.Controls
          If TypeOf Ctr Is CheckBox Then
             Controls.Remove Check1
          Else
             Print Ctr.Name
          End If
       Next
    End Sub
      

  7.   

    If TypeOf Ctr Is CheckBox Then Controls.Remove Ctr.Name
      

  8.   

    我测试的没有问题,可以删除:
    1、确定你的窗体上没有以Chk_Box开头的手工加上的checkbox。
    2、如果有请将他们改名或删除后,再测试。3、程序设计时手工加上去控件是不可以在运行时删除的。
    Private Sub Command1_Click() '创建
    CreateCheckBoxEnd SubPrivate Sub Command2_Click() '删除
    For Each Ctr In Me.Controls
          If Left(Ctr.Name, 7) = "Chk_Box" Then
            Controls.Remove Ctr
            Else
          End If
    NextEnd SubPrivate Sub Form_Load()
    With MSHFLG
    For i = 1 To .Rows - 1
        .TextMatrix(i, 1) = i
    Next i
    End WithEnd Sub
    Private Sub CreateCheckBox()
    Dim StrChkBox As String
    Dim Ctr    As ControlFor Each Ctr In Me.Controls
          If Left(Ctr.Name, 7) = "Chk_Box" Then
            Controls.Remove Ctr
            Else
          End If
    Next
      
    With MSHFLG
    For i = 1 To .Rows - 1
        .Col = 2
        .Row = i
        StrChkBox = "Chk_Box_" & Trim(.TextMatrix(i, 1))
        Set Ctr = Me.Controls.Add("vb.checkbox", StrChkBox, Frame1)
        Ctr.Visible = True
          
        Ctr.Top = .CellTop + .Top - 10
        Ctr.Left = .CellLeft + .Left - 10
        Ctr.Width = .CellWidth - 2 * Screen.TwipsPerPixelX + 30
        Ctr.Height = .CellHeight - 2 * Screen.TwipsPerPixelY + 40
        Ctr.Caption = Trim(.TextMatrix(i, 1)) & "号机器"
        Ctr.ZOrder 0
    Next i
    End With
    End Sub