Wierd For ...每个行为 [英] Wierd For...Each behavior

查看:73
本文介绍了Wierd For ...每个行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很奇怪:我有一个带有面板的Windows窗体。在那个

面板中,我动态地(在运行时)创建一些标签,如下所示:


for i = 1 to x

dim ctlNew as New Label()

with ctlNew

.Name =" Whatever" &安培; trim(cstr(i))

.Text = .Name

.Visible = True

...等等...

结束

MyPanel.Controls.Add(ctlNew)


显示表单时显示正常。但是,我在表单上还有一个重置

按钮,它会通过并销毁标签,因此:


dim ctl as Control
MyPanel.Controls中每个ctl的


如果typeof ctl是Label那么

ctl.text =""

ctl.visible = False

ctl.Dispose()

结束如果

next


OK ,这里是WIERD的事情:For ...每个循环似乎只拿起了

EVEN或ODD编号的控件!它跳过了一些控件(即奇数

)....如果我重新运行循环三次,一个接一个,

它最终会拿起所有控件并摆脱它们。


我想 - 代码是正确的 - 我甚至在之前添加了一个ctl = Nothing和/或

在ctl.Dispose()之后,但它没有帮助。 For ...每个循环似乎只是在运行期间跳过一些标签控件。我很困惑......甚至

虽然这可能是我正在做的事情。


任何人对此都有任何想法吗?


Tom

This is very strange: I have a Windows Form with a Panel on it. In that
panel I dynamically (at run time) create some labels, as so:

for i=1 to x
dim ctlNew as New Label()
with ctlNew
.Name="Whatever" & trim(cstr(i))
.Text=.Name
.Visible=True
... etc etc etc ...
end with
MyPanel.Controls.Add(ctlNew)

This shows up fine when the form is displayed. However, I also have a reset
button on the form, which goes thru and destroys the labels, as such:

dim ctl as Control
for each ctl in MyPanel.Controls
if typeof ctl is Label then
ctl.text=""
ctl.visible=False
ctl.Dispose()
end if
next

OK, here is the WIERD thing: The For...Each loop only seems to pick up the
EVEN or ODD numbered controls! It skips some of the controls (i.e. the odd
ones).... if I rerun the loop about three times, one right after the other,
it will finally pick up all the controls and get rid of them.

I -think- the code is correct - I even added a ctl=Nothing before and/or
after the ctl.Dispose(), but it didn''t help. The For...Each loop just seems
the skip some of the label controls during its run. I am baffled ... even
though it is probably something dumb that I am doing.

Anyone got any ideas on this?

Tom

推荐答案

Tom,
dim ctl as Control
对于MyPanel.Controls中的每个ctl
如果typeof ctl是Label那么
ctl.text =""
ctl.visible = False
ctl.Dispose()
结束如果
下一步
dim ctl as Control
for each ctl in MyPanel.Controls
if typeof ctl is Label then
ctl.text=""
ctl.visible=False
ctl.Dispose()
end if
next



我认为每个循环中的集合在每个循环中更改为


我认为更好更好

For i = Me.Controls.Count To 0 Step -1

Dim ctl As Control

它的类型是ctl是标签然后

Me.Controls.Remove(ctl)

下一页

我希望这有帮助

Cor


I think that the collections in the for each loop is changed in the the for
each loop
I think that nicer is
For i = Me.Controls.Count To 0 Step -1
Dim ctl As Control
it typeof ctl is Label then
Me.Controls.Remove(ctl)
Next
I hope this helps
Cor


为什么不把标签设置为空? GC调用Dispose。

所有你必须要做的就是杀死对象的引用。让GC

来处理剩下的事情。

Tom < To*@nospam.com>在消息中写道

news:ed ************** @ tk2msftngp13.phx.gbl ...
Why don''t you just set the labels to nothing? Dispose is called by the GC.
All you really have to do it kill your reference to the object. Let the GC
take care of the rest.
"Tom" <To*@nospam.com> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...
这很奇怪:我有一个带有Panel的Windows窗体。在那个
面板中,我动态地(在运行时)创建一些标签,如下所示:

对于i = 1到x
dim ctlNew作为新标签()
与ctlNew
.Name =" Whatever" &安培;修剪(cstr(i))
.Text = .Name
.Visible = True
...等等...
结束与
MyPanel.Controls .Add(ctlNew)

显示表单时显示正常。但是,我在表单上还有一个
重置按钮,它会通过并破坏标签,因此:

dim ctl作为MyPanel.Controls中每个ctl的控件

如果typeof ctl是Label那么
ctl.text =""
ctl.visible = False
ctl.Dispose()
结束如果
接下来

好的,这是WIERD的事情:For ...每个循环似乎只是拿起了偶数或奇数编号的控件!它会跳过一些控件(即奇怪的控件)....如果我重新运行循环三次,一次在
之后,它将最终获取所有控件并摆脱它我想 - 代码是正确的 - 我甚至在ctl.Dispose()之前添加了一个ctl = Nothing和/或
,但它没有帮助。 For ...每个循环只需
似乎在运行期间跳过一些标签控件。我很困惑......甚至
虽然这可能是我正在做的事情。

任何人对此都有任何想法吗?

Tom
This is very strange: I have a Windows Form with a Panel on it. In that
panel I dynamically (at run time) create some labels, as so:

for i=1 to x
dim ctlNew as New Label()
with ctlNew
.Name="Whatever" & trim(cstr(i))
.Text=.Name
.Visible=True
... etc etc etc ...
end with
MyPanel.Controls.Add(ctlNew)

This shows up fine when the form is displayed. However, I also have a reset button on the form, which goes thru and destroys the labels, as such:

dim ctl as Control
for each ctl in MyPanel.Controls
if typeof ctl is Label then
ctl.text=""
ctl.visible=False
ctl.Dispose()
end if
next

OK, here is the WIERD thing: The For...Each loop only seems to pick up the
EVEN or ODD numbered controls! It skips some of the controls (i.e. the odd
ones).... if I rerun the loop about three times, one right after the other, it will finally pick up all the controls and get rid of them.

I -think- the code is correct - I even added a ctl=Nothing before and/or
after the ctl.Dispose(), but it didn''t help. The For...Each loop just seems the skip some of the label controls during its run. I am baffled ... even
though it is probably something dumb that I am doing.

Anyone got any ideas on this?

Tom



是不是Controls.Count一个基于一个的索引?我认为它应该是:


For i = Me.Controls.Count -1 To 0 Step -1

....

Cor < no*@non.com>在消息中写道

news:3f *********************** @ reader21.wxs.nl ...
Isn''t Controls.Count a one-based index? I think it should be:

For i = Me.Controls.Count -1 To 0 Step -1
....
"Cor" <no*@non.com> wrote in message
news:3f***********************@reader21.wxs.nl...
Tom,
dim ctl as Control
对于MyPanel.Controls中的每个ctl
如果typeof ctl是Label那么
ctl.text ="" ;
ctl.visible = False
ctl.Dispose()
结束如果
接下来我认为每个循环中的集合在
dim ctl as Control
for each ctl in MyPanel.Controls
if typeof ctl is Label then
ctl.text=""
ctl.visible=False
ctl.Dispose()
end if
next I think that the collections in the for each loop is changed in the the

中被更改<每个循环的

我认为更好的是
对于i = Me.Controls.Count到0步-1
Dim ctl作为控制
它的类型ctl是Label然后
Me.Controls.Remove(ctl)
下一页
我希望这有帮助
Cor


for each loop
I think that nicer is
For i = Me.Controls.Count To 0 Step -1
Dim ctl As Control
it typeof ctl is Label then
Me.Controls.Remove(ctl)
Next
I hope this helps
Cor



这篇关于Wierd For ...每个行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆