By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,069 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,069 IT Pros & Developers. It's quick & easy.

for loop is broken?

P: n/a
I have the following code inside of a WebBrowser.DocumentCompleted event:

For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li").Count

NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName("ul").Item(0).GetElementsByTagName("li" ).Item(index).InnerText)

Next index

For some reason, nothing after the next index will run. It's like everything
after it gets ignored. Even when I just use next it doesn't work (although I
get the wrong data if I do that). How do you fix this?


Oct 15 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
In this kind of loop it is normal

Count - 1

Cor

"Andy B" <a_*****@sbcglobal.netwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>I have the following code inside of a WebBrowser.DocumentCompleted event:

For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li").Count

NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName("ul").Item(0).GetElementsByTagName("li" ).Item(index).InnerText)

Next index

For some reason, nothing after the next index will run. It's like
everything after it gets ignored. Even when I just use next it doesn't
work (although I get the wrong data if I do that). How do you fix this?

Oct 15 '08 #2

P: n/a
Andy B wrote:
I have the following code inside of a WebBrowser.DocumentCompleted event:

For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li").Count

NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName("ul").Item(0).GetElementsByTagName("li" ).Item(index).InnerText)

Next index

For some reason, nothing after the next index will run. It's like everything
after it gets ignored. Even when I just use next it doesn't work (although I
get the wrong data if I do that). How do you fix this?
I suppose that you are catching exceptions somewhere?

In the last iteration of the loop, you are trying to access an item that
is outside the list, so you get an IndexOutOfRangeException, which will
send the execution of the code into the catch block, skipping the code
after the loop.

You should NEVER just catch an exception and silently throw it away
(unless in a very very small try...catch block where you specify exactly
what exception you expect, and know exactly why you can ignore it).

The index specification in the Next statement is optional, and excluding
it doesn't change the behaviour of the loop in any way.

You should loop from 0 to Count-1, not from 0 to Count:

Dim items as HtmlElementCollection =
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li")
For index = 0 to items.Count - 1
NoteList.Items.Add(items.Item(index).InnerText)
Next

--
Göran Andersson
_____
http://www.guffa.com
Oct 15 '08 #3

P: n/a
OK. Figured I had to be missing something. Will try it out. On the note of
try...catch blocks, how do you catch all exceptions and show them in a
messageBox (for debugging reasons. When I run the code below, I never get an
exception raised or anything unless the system just throws it away and I
don't know it.
"Göran Andersson" <gu***@guffa.comwrote in message
news:eG**************@TK2MSFTNGP05.phx.gbl...
Andy B wrote:
>I have the following code inside of a WebBrowser.DocumentCompleted event:

For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").It em(0).GetElementsByTagName("li").Count

NotesList.Items.Add(Me.Browser.Document.GetElemen tsByTagName("ul").Item(0).GetElementsByTagName("li ").Item(index).InnerText)

Next index

For some reason, nothing after the next index will run. It's like
everything after it gets ignored. Even when I just use next it doesn't
work (although I get the wrong data if I do that). How do you fix this?

I suppose that you are catching exceptions somewhere?

In the last iteration of the loop, you are trying to access an item that
is outside the list, so you get an IndexOutOfRangeException, which will
send the execution of the code into the catch block, skipping the code
after the loop.

You should NEVER just catch an exception and silently throw it away
(unless in a very very small try...catch block where you specify exactly
what exception you expect, and know exactly why you can ignore it).

The index specification in the Next statement is optional, and excluding
it doesn't change the behaviour of the loop in any way.

You should loop from 0 to Count-1, not from 0 to Count:

Dim items as HtmlElementCollection =
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li")
For index = 0 to items.Count - 1
NoteList.Items.Add(items.Item(index).InnerText)
Next

--
Göran Andersson
_____
http://www.guffa.com

Oct 15 '08 #4

P: n/a
You likely have done something to make this happen. The usual behavior is to
break into the debugger when an exception is thrown...

--
Patrice

"Andy B" <a_*****@sbcglobal.neta écrit dans le message de groupe de
discussion : Ok**************@TK2MSFTNGP05.phx.gbl...
OK. Figured I had to be missing something. Will try it out. On the note of
try...catch blocks, how do you catch all exceptions and show them in a
messageBox (for debugging reasons. When I run the code below, I never get
an exception raised or anything unless the system just throws it away and
I don't know it.
>>
Oct 15 '08 #5

P: n/a
On Oct 15, 1:38*pm, "Andy B" <a_bo...@sbcglobal.netwrote:
OK. Figured I had to be missing something. Will try it out. On the note of
try...catch blocks, how do you catch all exceptions and show them in a
messageBox (for debugging reasons. When I run the code below, I never getan
exception raised or anything unless the system just throws it away and I
don't know it.

"Göran Andersson" <gu...@guffa.comwrote in message

news:eG**************@TK2MSFTNGP05.phx.gbl...
Andy B wrote:
I have the following code inside of a WebBrowser.DocumentCompleted event:
For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName*("li").Count
NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName("ul").Item(0).*GetElementsByTagName("li ").Item(index).InnerText)
Next index
For some reason, nothing after the next index will run. It's like
everything after it gets ignored. Even when I just use next it doesn't
work (although I get the wrong data if I do that). How do you fix this?
I suppose that you are catching exceptions somewhere?
In the last iteration of the loop, you are trying to access an item that
is outside the list, so you get an IndexOutOfRangeException, which will
send the execution of the code into the catch block, skipping the code
after the loop.
You should NEVER just catch an exception and silently throw it away
(unless in a very very small try...catch block where you specify exactly
what exception you expect, and know exactly why you can ignore it).
The index specification in the Next statement is optional, and excluding
it doesn't change the behaviour of the loop in any way.
You should loop from 0 to Count-1, not from 0 to Count:
Dim items as HtmlElementCollection =
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName*("li")
For index = 0 to items.Count - 1
* *NoteList.Items.Add(items.Item(index).InnerText)
Next
--
Göran Andersson
_____
http://www.guffa.com- Hide quoted text -

- Show quoted text -
Put your code in a try-catch block to test if it returns any
exception:

'-------------------------------------
Try

For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName*
("li").Count - 1
NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName("ul").Item(0).*
GetElementsByTagName("li").Item(index).InnerText)
Next index

' Let's try to catch exception if so,
'Use msgbox to see
Catch ex As Exception
Msgbox(ex.Message)

End Try
'-----------------------------------

In my idea, as you describe in your first post, if you were using your
loop in a try-catch block, you may have got an exception inside your
for-next loop, thus the execution in for-next loop is aborted and an
exception may have been thrown. And you may not have seen the
exception if your Catch block was empty (the worst scenerio). To
catch, use it as above.

Onur Güzel
Oct 15 '08 #6

P: n/a
I tried the idea with the try..catch block with the Count-1 idea. This is
what I get:

1. No exceptions are thrown in the code anywhere.
2. When using next index, the for loop works, but none of the code after the
for loop runs.
3. When just using the next statement, the for loop messes up things and I
get the wrong data as well as the code after next statement wont run.
4. The Count - 1 statement doesn't seem to make any changes.
5. Running the entire program through the debugger, index evaluates to the
right value.
6. Stepping into the code, anything after the next statement fails (it is
totally skipped).
7. The code after the next statement doesn't appear to be broken since
MessageBox.Show("succeeded") doesn't even run.

Is there any other ideas I can do to try and troubleshoot this for loop?

"kimiraikkonen" <ki*************@gmail.comwrote in message
news:f1**********************************@o4g2000p ra.googlegroups.com...
On Oct 15, 1:38 pm, "Andy B" <a_bo...@sbcglobal.netwrote:
OK. Figured I had to be missing something. Will try it out. On the note of
try...catch blocks, how do you catch all exceptions and show them in a
messageBox (for debugging reasons. When I run the code below, I never get
an
exception raised or anything unless the system just throws it away and I
don't know it.

"Göran Andersson" <gu...@guffa.comwrote in message

news:eG**************@TK2MSFTNGP05.phx.gbl...
Andy B wrote:
I have the following code inside of a WebBrowser.DocumentCompleted
event:
For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName*("li").Count
NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName("ul").Item(0).*GetElementsByTagName("li ").Item(index).InnerText)
Next index
For some reason, nothing after the next index will run. It's like
everything after it gets ignored. Even when I just use next it doesn't
work (although I get the wrong data if I do that). How do you fix this?
I suppose that you are catching exceptions somewhere?
In the last iteration of the loop, you are trying to access an item that
is outside the list, so you get an IndexOutOfRangeException, which will
send the execution of the code into the catch block, skipping the code
after the loop.
You should NEVER just catch an exception and silently throw it away
(unless in a very very small try...catch block where you specify exactly
what exception you expect, and know exactly why you can ignore it).
The index specification in the Next statement is optional, and excluding
it doesn't change the behaviour of the loop in any way.
You should loop from 0 to Count-1, not from 0 to Count:
Dim items as HtmlElementCollection =
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName*("li")
For index = 0 to items.Count - 1
NoteList.Items.Add(items.Item(index).InnerText)
Next
--
Göran Andersson
_____
http://www.guffa.com- Hide quoted text -

- Show quoted text -
Put your code in a try-catch block to test if it returns any
exception:

'-------------------------------------
Try

For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName*
("li").Count - 1
NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName("ul").Item(0).*
GetElementsByTagName("li").Item(index).InnerText)
Next index

' Let's try to catch exception if so,
'Use msgbox to see
Catch ex As Exception
Msgbox(ex.Message)

End Try
'-----------------------------------

In my idea, as you describe in your first post, if you were using your
loop in a try-catch block, you may have got an exception inside your
for-next loop, thus the execution in for-next loop is aborted and an
exception may have been thrown. And you may not have seen the
exception if your Catch block was empty (the worst scenerio). To
catch, use it as above.

Onur Güzel
Oct 15 '08 #7

P: n/a
Andy B wrote:
Is there any other ideas I can do to try and troubleshoot this for
loop?
Do you have Option Strict On at the top of the script? That will point out
where things don't quite match what you intended.

Did you try Göran's tidier-looking version?

Andrew
Oct 15 '08 #8

P: n/a
I have option strict turned on in vs2008sp1 options. I didn't try his code
yet. Was going to and had it copied to the clipboard but lost it in a
computer restart.
"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:6l************@mid.individual.net...
Andy B wrote:
>Is there any other ideas I can do to try and troubleshoot this for
loop?

Do you have Option Strict On at the top of the script? That will point out
where things don't quite match what you intended.

Did you try Göran's tidier-looking version?

Andrew

Oct 15 '08 #9

P: n/a
Ok. I tried the following code and it didn't work. Not only does the code
after the next statement fail to run, I get the wrong data inside the
NotesList listbox (well, I get the right data, but it appears twice instead
of only once).

Dim NoteItems as HtmlElementCollection =
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li")
For index = 0 to NoteItems.Count - 1
NoteList.Items.Add(NoteItems.Item(index).InnerText )
Next

Any other ideas?
Is there a way to debug the Browser_DocumentCompleted event code to see
where things are going wrong?
"Andy B" <a_*****@sbcglobal.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>I have option strict turned on in vs2008sp1 options. I didn't try his code
yet. Was going to and had it copied to the clipboard but lost it in a
computer restart.
"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:6l************@mid.individual.net...
>Andy B wrote:
>>Is there any other ideas I can do to try and troubleshoot this for
loop?

Do you have Option Strict On at the top of the script? That will point
out where things don't quite match what you intended.

Did you try Göran's tidier-looking version?

Andrew


Oct 15 '08 #10

P: n/a
"Andy B" <a_*****@sbcglobal.netschrieb
I have option strict turned on in vs2008sp1 options. I didn't try
his code yet. Was going to and had it copied to the clipboard but
lost it in a computer restart.

Hint only: If you've created the project before setting the Option Strict
setting to On in the VS options (Off is default), the project still has
Option Strict Off. The VS setting is only for new projects. In the active
project, you have to set it in it's properties.
Armin

Oct 15 '08 #11

P: n/a
It was set before I created the project.
"Armin Zingler" <az*******@freenet.dewrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"Andy B" <a_*****@sbcglobal.netschrieb
>I have option strict turned on in vs2008sp1 options. I didn't try
his code yet. Was going to and had it copied to the clipboard but
lost it in a computer restart.


Hint only: If you've created the project before setting the Option Strict
setting to On in the VS options (Off is default), the project still has
Option Strict Off. The VS setting is only for new projects. In the active
project, you have to set it in it's properties.
Armin

Oct 15 '08 #12

P: n/a
Andy,

Probably is that what you get in the Item from another MSHTML class.
I have done this once in far past, it is a hell of a job to cast it, but it
is advisable first to break your code in pieces in a way that you can see
what types are used.

What some (expirienced) peopledo with this MSHTML types is et Option Strict
first to off to see if that will go.
I have never done that in a production environment, but for testing it can
sometimes be helpfull.

Cor
"Andy B" <a_*****@sbcglobal.netschreef in bericht
news:uu**************@TK2MSFTNGP06.phx.gbl...
Ok. I tried the following code and it didn't work. Not only does the code
after the next statement fail to run, I get the wrong data inside the
NotesList listbox (well, I get the right data, but it appears twice
instead of only once).

Dim NoteItems as HtmlElementCollection =
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li")
For index = 0 to NoteItems.Count - 1
NoteList.Items.Add(NoteItems.Item(index).InnerText )
Next

Any other ideas?
Is there a way to debug the Browser_DocumentCompleted event code to see
where things are going wrong?
"Andy B" <a_*****@sbcglobal.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>I have option strict turned on in vs2008sp1 options. I didn't try his code
yet. Was going to and had it copied to the clipboard but lost it in a
computer restart.
"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:6l************@mid.individual.net...
>>Andy B wrote:
Is there any other ideas I can do to try and troubleshoot this for
loop?

Do you have Option Strict On at the top of the script? That will point
out where things don't quite match what you intended.

Did you try Göran's tidier-looking version?

Andrew



Oct 15 '08 #13

P: n/a
Andy B wrote:
Ok. I tried the following code and it didn't work. Not only does the code
after the next statement fail to run, I get the wrong data inside the
NotesList listbox (well, I get the right data, but it appears twice instead
of only once).

Dim NoteItems as HtmlElementCollection =
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li")
For index = 0 to NoteItems.Count - 1
NoteList.Items.Add(NoteItems.Item(index).InnerText )
Next

Any other ideas?
Is there a way to debug the Browser_DocumentCompleted event code to see
where things are going wrong?
Is the NoteList listbox empty from start?

Is it possible that the page reloads itself for some reason (like a
break-out-of-frames script), so that you are actually loading it twice?

What's in the code that follows the loop anyway? Is it possible that
this code can cause any of this?

--
Göran Andersson
_____
http://www.guffa.com
Oct 16 '08 #14

P: n/a
"Is the NoteList listbox empty from start?"
Yes.

"Is it possible that the page reloads itself for some reason (like a
break-out-of-frames script), so that you are actually loading it twice?"
1. The Browser.Document is loaded with a local html file on the network.
2. A textBox in a GoButton_Click event loads the page.

private sub GoButton_Click(Sender as object, e as System.EventArgs) handles
Me.GoButton.Click
Browser.Navigate(URLTextBox.Text)
end sub
"What's in the code that follows the loop anyway? Is it possible that this
code can cause any of this?"
After the for loops next statement is this:
'*** there would be more useful stuff here but even the code below won't
run.
NotesList.Focus()

Oct 16 '08 #15

P: n/a
On Oct 16, 5:00*am, "Andy B" <a_bo...@sbcglobal.netwrote:
"Is the NoteList listbox empty from start?"
Yes.

"Is it possible that the page reloads itself for some reason (like a
break-out-of-frames script), so that you are actually loading it twice?"
1. The Browser.Document is loaded with a local html file on the network.
2. A textBox in a GoButton_Click event loads the page.

private sub GoButton_Click(Sender as object, e as System.EventArgs) handles
Me.GoButton.Click
Browser.Navigate(URLTextBox.Text)
end sub

"What's in the code that follows the loop anyway? Is it possible that this
code can cause any of this?"
After the for loops next statement is this:
'*** there would be more useful stuff here but even the code below won't
run.
NotesList.Focus()
Meanwhile, i noticed, Webbrowser's DocumentCompleted event is not
fired if you use Refresh() method of Webbrowser. Do you have any
Refresh-containing code in your code block that you haven't posted so
far?...

Based on your descriptions in your other posts, that kind of things
may have prevented your application from firing DocumentCompleted
event even no exception is thrown.

Onur Güzel
Oct 16 '08 #16

P: n/a
No refresh code anywhere...
"kimiraikkonen" <ki*************@gmail.comwrote in message
news:f9**********************************@e38g2000 prn.googlegroups.com...
On Oct 16, 5:00 am, "Andy B" <a_bo...@sbcglobal.netwrote:
"Is the NoteList listbox empty from start?"
Yes.

"Is it possible that the page reloads itself for some reason (like a
break-out-of-frames script), so that you are actually loading it twice?"
1. The Browser.Document is loaded with a local html file on the network.
2. A textBox in a GoButton_Click event loads the page.

private sub GoButton_Click(Sender as object, e as System.EventArgs)
handles
Me.GoButton.Click
Browser.Navigate(URLTextBox.Text)
end sub

"What's in the code that follows the loop anyway? Is it possible that this
code can cause any of this?"
After the for loops next statement is this:
'*** there would be more useful stuff here but even the code below won't
run.
NotesList.Focus()
Meanwhile, i noticed, Webbrowser's DocumentCompleted event is not
fired if you use Refresh() method of Webbrowser. Do you have any
Refresh-containing code in your code block that you haven't posted so
far?...

Based on your descriptions in your other posts, that kind of things
may have prevented your application from firing DocumentCompleted
event even no exception is thrown.

Onur Güzel
Oct 16 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.