for loop is broken?

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

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

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

NotesList.Items.Add(Me.Browser.Document.GetElementsByTagName("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?
 
Andy said:
I have the following code inside of a WebBrowser.DocumentCompleted event:

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

NotesList.Items.Add(Me.Browser.Document.GetElementsByTagName("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").Item(0).GetElementsByTagName("li")
For index = 0 to items.Count - 1
NoteList.Items.Add(items.Item(index).InnerText)
Next
 
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.
 
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.











- 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").Item(0).GetElementsByTagName­
("li").Count - 1


NotesList.Items.Add(Me.Browser.Document.GetElementsByTagName("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
 
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?

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.











- 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").Item(0).GetElementsByTagName­
("li").Count - 1


NotesList.Items.Add(Me.Browser.Document.GetElementsByTagName("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
 
Andy said:
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
 
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.
 
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").Item(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 said:
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
 
It was set before I created the project.


Armin Zingler said:
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
 
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 said:
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").Item(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?
 
"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()
 
"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
 
No refresh code anywhere...
"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
 
Back
Top