当前位置:编程学习 > VB >>

动态生成的 CheckBox 无法删除

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



--------------------编程问答-------------------- 应该用 Unload 吧?
--------------------编程问答-------------------- 刚才试了,应该用 Controls.Remove 方法。

是不是因为 Ctr 有时引用的不是 动态加载 的控件啊?
出错时点‘调试’,检查一下 Ctr 是不是某个设计时加入的控件。
--------------------编程问答-------------------- 请输出自动添加的控件名,检查下控件名的前7个字符是不是Chk_Box。 --------------------编程问答-------------------- width=80000 够了吧,把它弄到界面外 --------------------编程问答-------------------- visible = false
或left = -75000 --------------------编程问答-------------------- 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 Sub

Private 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


--------------------编程问答-------------------- If TypeOf Ctr Is CheckBox Then Controls.Remove Ctr.Name --------------------编程问答-------------------- 顶一下        --------------------编程问答-------------------- 我测试的没有问题,可以删除:
1、确定你的窗体上没有以Chk_Box开头的手工加上的checkbox。
2、如果有请将他们改名或删除后,再测试。

3、程序设计时手工加上去控件是不可以在运行时删除的。


Private Sub Command1_Click() '创建
CreateCheckBox

End Sub

Private Sub Command2_Click() '删除
For Each Ctr In Me.Controls
      If Left(Ctr.Name, 7) = "Chk_Box" Then
        Controls.Remove Ctr
        Else
      End If
Next

End Sub

Private Sub Form_Load()
With MSHFLG
For i = 1 To .Rows - 1
    .TextMatrix(i, 1) = i
Next i
End With

End Sub
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

补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,