我写了一个程序,想在combobox的click事件中删除动态添加的控件,但总是提示“实时错误'365':不能在该上下文中卸载”。我的程序如下:
Option Explicit
Dim fr As FramePrivate Sub Combo1_click()
    Form1.Controls.Remove "fr"
End SubPrivate Sub Form_Load()
    Dim cb As CheckBox
    
    Set fr = Form1.Controls.Add("vb.frame", "fr")
    With fr
        .Visible = True
        .Left = 2000
        .Top = 1500
        .Height = 2000
        .Width = 3000
        .Caption = "选择"
    End With
    
    Set cb = Form1.Controls.Add("vb.checkbox", "cb", fr)
    With cb
        .Visible = True
        .Left = 200
        .Top = 200
        .Caption = "选项一"
    End With
    
    Form1.Combo1.AddItem ("项目一")
    Form1.Combo1.ItemData(Combo1.NewIndex) = 1
    Form1.Combo1.AddItem ("项目二")
    Form1.Combo1.ItemData(Combo1.NewIndex) = 2    
End Sub
窗体中设计时只加了一个combobox,然后动态添加了一个frame,并在其中添加了一个checkbox。望各位高手解答!另外,我试了在按钮的单击事件中,删除动态添加的控件的可以成功,为什么在combobox的单击事件中却不行呢?

解决方案 »

  1.   

    你把Combo1的相关的代码放到前面去
    或者使用Combo1的其他事件试试
      

  2.   

    Unable to unload within this context (Error 365)
    In some situations you are not allowed to unload a form or a control on a form. This error has the following causes and solutions: There is an Unload statement in the Paint event for the form or for a control on the form that has the Paint event. 
    Remove the Unload statement from the Paint event.There is an Unload statement in the Change, Click, or DropDown events of a ComboBox. 
    Remove the Unload statement from the event.
    There is an Unload statement in the Scroll event of an HScrollBar or VScrollBar control. 
    Remove the Unload statement from the event.There is an Unload statement in the Resize event of a Data, Form, MDIForm, or PictureBox control. 
    Remove the Unload statement from the event.There is an Unload statement in the Resize event of an MDIForm that is trying to unload an MDI child form. 
    Remove the Unload statement from the event.There is an Unload statement in the RePosition or Validate event of a Data control. 
    Remove the Unload statement from the event.There is an Unload statement in the ObjectMove event of an OLE Container control. 
    Remove the Unload statement from the event.
      

  3.   

    COMBOX控件的click事件是由其组成控件picturebox传递来的。换成非传递事件就可(如:combo_keypress)
      

  4.   

    我记得这个是 Combobox 控件的设计问题,是微软没有解决的大BUG,我以前做过的程序不能在 combobox 的 click 事件中卸载其他 PictureBox,否则即出错。置于卸载其他类型控件倒是没试过。我但是的解决方案是在 combobox 控件的 click 事件中加入一句:
    SendKeys "{ENTER}" 然后,再在 combobox 控件的 KeyPress 事件中处理有 Unload 语句的那些代码。
    Private Sub Combo1_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then
            '在这里处理那些有 Unload 语句的东西
        End If
        KeyAscii = 0
    End Sub这样就可以暂时解决这个问题了。
      

  5.   

    不能在该上下文中卸载(错误 365)
    某些情况下,不允许卸载窗体或窗体中的控件。此错误产生的原因及解决方法如下: 在窗体的 Paint 事件中,或在窗体上控件的 Paint 事件中,有一个 Unload 语句,此时的控件应具有 Paint 事件。 
    从 Paint 事件中删除 Unload 语句。在 ComboBox.的 Change、Click 或 DropDown 事件中有一个 Unload 语句。 
    从事件中删除 Unload 语句。在 HScrollBar 或 VScrollBar 控件的 Scroll 事件中有一个 Unload 语句。 
    从事件中删除 Unload 语句。在 Data、Form、MDIForm 或 PictureBox 控件的 Resize 事件中有一个 Unload 语句。 
    从事件中删除 Unload 语句。在试图卸载 MDI 子窗体的 MDIForm 的 Resize 事件中,有一个 Unload 语句。 
    从事件中删除 Unload 语句。在 Data 控件的 RePosition 或 Validate 事件中有一个 Unload 语句。 
    从事件中删除 Unload 语句。在 OLE Container 控件的 ObjectMove 事件中有一个 Unload 语句。 
    从事件中删除 Unload 语句。
      

  6.   

    SendKeys "{ENTER}"  这种解决办法最好
      

  7.   

    SendKeys "{ENTER}"   这种解决办法最好