在vb的combobox 的click事件中怎么不能删除动态添加的控件?

pgboy55 2007-12-24 09:37:40
我写了一个程序,想在combobox的click事件中删除动态添加的控件,但总是提示“实时错误'365':不能在该上下文中卸载”。我的程序如下:
Option Explicit
Dim fr As Frame

Private Sub Combo1_click()
Form1.Controls.Remove "fr"
End Sub

Private 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的单击事件中却不行呢?
...全文
570 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xunis 2012-03-19
  • 打赏
  • 举报
回复
SendKeys "{ENTER}"

这种解决办法最好
zyg1858 2012-02-19
  • 打赏
  • 举报
回复
SendKeys "{ENTER}"

这种解决办法最好
jessezappy 2011-04-05
  • 打赏
  • 举报
回复
不能在该上下文中卸载(错误 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 语句。
jessezappy 2011-04-05
  • 打赏
  • 举报
回复
我记得这个是 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

这样就可以暂时解决这个问题了。
wang_jian_min 2008-07-30
  • 打赏
  • 举报
回复
COMBOX控件的click事件是由其组成控件picturebox传递来的。换成非传递事件就可(如:combo_keypress)
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
有点难度哦
junki 2008-04-02
  • 打赏
  • 举报
回复
你把Combo1的相关的代码放到前面去
或者使用Combo1的其他事件试试
ly012659 2008-04-02
  • 打赏
  • 举报
回复
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.

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧