473,587 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wierd For...Each behavior

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.Control s.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.Control s
if typeof ctl is Label then
ctl.text=""
ctl.visible=Fal se
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

Nov 20 '05 #1
112 4001
Cor
Tom,
dim ctl as Control
for each ctl in MyPanel.Control s
if typeof ctl is Label then
ctl.text=""
ctl.visible=Fal se
ctl.Dispose()
end if
next

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.Cou nt To 0 Step -1
Dim ctl As Control
it typeof ctl is Label then
Me.Controls.Rem ove(ctl)
Next
I hope this helps
Cor
Nov 20 '05 #2
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******** ******@tk2msftn gp13.phx.gbl...
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.Control s.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.Control s
if typeof ctl is Label then
ctl.text=""
ctl.visible=Fal se
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

Nov 20 '05 #3
Isn't Controls.Count a one-based index? I think it should be:

For i = Me.Controls.Cou nt -1 To 0 Step -1
....
"Cor" <no*@non.com> wrote in message
news:3f******** *************** @reader21.wxs.n l...
Tom,
dim ctl as Control
for each ctl in MyPanel.Control s
if typeof ctl is Label then
ctl.text=""
ctl.visible=Fal se
ctl.Dispose()
end if
next 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.Cou nt To 0 Step -1
Dim ctl As Control
it typeof ctl is Label then
Me.Controls.Rem ove(ctl)
Next
I hope this helps
Cor

Nov 20 '05 #4
Tom
Yea, stupid me, I just figured this out when everyone replied. Sheesh, I
should have seen that. Anyway, I'll go in reverse and remove the items that
way. Thanks.

Tom

"Nak" <a@a.com> wrote in message
news:um******** ******@tk2msftn gp13.phx.gbl...
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 have "an" idea, maybe it's because you are removing a control from the
collection in the loop, for example if you used a For loop and removed
controls without taking away 1 from the final index you would over run and
skip controls in a similar way. Maybe try using an integer controlled for
loop and every time you remove a control take away 1 from the counter and

1 from the final index. I've attached an example, excuse the type checking
code, I know it's bad, also I have added a button to simulate the problem
you are getting at current.

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ Slow internet connection?
Having problems with you job?
You're marriage is on the rocks?
You can't sleep at night?
You have a drink and drugs addiction?
You are sexually impotent?
Then enable Option Strict!
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Nov 20 '05 #5

"Cor" <no*@non.com> wrote in message
news:3f******** *************** @reader21.wxs.n l...
Tom,
dim ctl as Control
for each ctl in MyPanel.Control s
if typeof ctl is Label then
ctl.text=""
ctl.visible=Fal se
ctl.Dispose()
end if
next 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.Cou nt To 0 Step -1
Dim ctl As Control
it typeof ctl is Label then
Me.Controls.Rem ove(ctl)
Next
I hope this helps
Cor


It doesn't help when you post bad code that doesn't work, despite your good
intentions.

You also might want to learn when to use While or Do loops instead of
misusing a For loop.
Nov 20 '05 #6
Cor
Robert
You are right, just forgot to type.
Thanks for your attention.
Cor
Nov 20 '05 #7

"Scott Meddows" <sc************ ******@tsged.co m> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.


Because that won't work for controls. Controls are referenced by their
container, so it doesn't matter whether you set all code references to
nothing: the container will still hold a reference until you remove the
control or the container itself.
Nov 20 '05 #8
Cor
Jack.
Yes I mistyped the -1 but that can every programmer with a little expirience
see.
Robert was so kind to tell that.

But show that you can do it better, send a good working code in this
situation with the do while loop.
Cor
Nov 20 '05 #9
Then you'd have to loop through a loop and do the
container.contr ols.remove(Your ForEachLoopCont rol) ?

"Jack Spry" <js***@nospamme rs.com> wrote in message
news:OG******** ******@TK2MSFTN GP12.phx.gbl...

"Scott Meddows" <sc************ ******@tsged.co m> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.


Because that won't work for controls. Controls are referenced by their
container, so it doesn't matter whether you set all code references to
nothing: the container will still hold a reference until you remove the
control or the container itself.

Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1708
by: Qun Cao | last post by:
Hello, I am just starting to play threading in python, here is a really interesting problem I am very curious about: " import thread def main(): thread.start_new(test.()) def test():
0
1292
by: Niranjan | last post by:
Access XP Windows XP This code has been working for over 5 years with no problems and all of a sudden I am running into these wierd problems. I have this code to delete a record. DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70 DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70 I am getting error 2465.
1
6800
by: paul reed | last post by:
Hello, I am having some weird behavior between two machines...one which is running the 1.1 framework and one which is running 1.0. After opening a child form from a parent...I update the database then call the __doPostBack function of the window.opener. This has been working like a charm for the last several months. Our ISP where we run...
3
2069
by: Michael Loughry | last post by:
I'm working for a company in Houston developing a web application. At one point in the code, we have to refresh the page, but save what checkboxes have been selected. Since these checkboxes are created programmatically according to configuration files, they cannot be asp checkboxes. To do this, before refreshing I save which checkboxes are...
14
6683
by: SStory | last post by:
I am trying to make a splash screen for my vb.net app. It is an mdi app. including the splash code produces wierd results. not inluding makes things fine. Also have tried loading the splash form from: * load event of main mdi parent
0
1173
by: Tom | last post by:
OK, here's a wierd one... I have a listbox, which I fill with strings (in my case, file names). I normally load this via a loop, adding each item to the list box in the loop. I put lb.BeginUpdate before the start of the loop, and lb.EndUpdate at the conclusion of the loop. Here's the wierd part: If there is only ONE line loaded into the list...
3
1539
by: Tom | last post by:
We are experiencing some wierd debugging behavior. What happens is that, during debugging with VS 2003, the debugger seems to 'skip' statements that are associated with database operations. For instance, I can be single-stepping through a VB.NET program, and once I hit a DB related statement (such as setting command parameters, executing a...
0
1588
by: Tom | last post by:
I have some very strange issues with combo boxes on a tab control. Here's the scenario: I have a Windows Forms form that has a tab control on it, with two (2) tabs. Tab 2 happens to have a number of text and combo boxes on it (in panels on the tab). These combos were originally simple drop down lists - i.e. you had to select from the list and...
4
1512
by: Muthu Arumugam | last post by:
Tried the following c# code static void Main(string args) { ArrayList list = new ArrayList(); int i = 10;
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.