|
-
Oct 26th, 2011, 02:23 AM
#1
Thread Starter
New Member
[RESOLVED] Code does not execute after For Each Next Loop

I am calling a sub from a form load event. The procedure that is being called adds columns to a listview and then loops through an array to populate the listview. I have another procedure called after the loop that does not execute when the loop completes. Please advise as to what I am doing wrong for the code not to execute after the loop in the if/then/else statement or why it is not executing if I remove the if/then/else statement and just placed the called sub after the loop or why the code does not go back to the form load event and then call the next called sub. Code below:
Code:
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Startup As frmRPMSStartup
Dim login As frmRPMSLogin
Dim EmployeeName As String
Try
'some code to fill labels here
Firstcalledsub()
Secondcalledsub()
Catch ex As Exception
Throw ex
MessageBox.Show("Error on log-in " & ex.ToString, "Log-in error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End Try
End Sub
HTML Code:
Sub Firstcalledsub()
'Create columns in listview
With lstvListview .View = View.Details
.LabelEdit = True
.FullRowSelect = True
.GridLines = True
.Columns.Add("string", 65, HorizontalAlignment.Left)
.Columns.Add("string", 400, HorizontalAlignment.Left)
.Columns.Add("string", 400, HorizontalAlignment.Left)
.Columns.Add("string", 400, HorizontalAlignment.Left)
.Columns.Add("string", 65, HorizontalAlignment.Left)
.Columns.Add("string", 75, HorizontalAlignment.Left)
.Columns.Add("string", 100, HorizontalAlignment.Left)
.Columns.Add("string", 100, HorizontalAlignment.Left)
.Columns.Add("string", 75, HorizontalAlignment.Left)
.Columns.Add("string", 120, HorizontalAlignment.Left)
.Columns.Add("string", 175, HorizontalAlignment.Left)
.Columns.Add("string", 60, HorizontalAlignment.Left)
.Columns.Add("string", 75, HorizontalAlignment.Left)
.Columns.Add("string", 100, HorizontalAlignment.Left)
.Columns.Add("string", 100, HorizontalAlignment.Left)
.Columns.Add("string", 50, HorizontalAlignment.Left)
.Columns.Add("string", 30, HorizontalAlignment.Left)
End With
'Create item to hold contents of handover array
Dim itm As ListViewItem
Dim arrayitems As String
Dim i As Integer = 0
If IsNothing(array) Then
ToDoListItems()
Else
For Each arrayitems In Datarray
'Add data from Database logins array to listview item
itm = New ListViewItem(New String() {array(i, 0), array(i, 1), array(i, 2), array(i, 3), array(i, 4), array(i, 5), array(i, 6), array(i, 7), array(i, 8), array(i, 9), array(i, 10), array(i, 11), array(i, 12), array(i, 13), array(i, 14), array(i, 15), array(i, 16)})
'Add item to Handover listview
lstvHandover.Items.Add(itm)
i += 1
Next
Secondcalledsub()
End If
End Sub
-
Oct 26th, 2011, 03:22 AM
#2
Re: Code does not execute after For Each Next Loop
An exception is being thrown but you aren't seeing it because you are just rethrowing it. You've got code to display it but that's after the Throw, so it's never executed. Why are you rethrowing the exception anyway?
-
Oct 26th, 2011, 03:31 AM
#3
Thread Starter
New Member
Re: Code does not execute after For Each Next Loop
I put the messagebox in there to see if it would interpret the exception differently but that didn't work. Even when I take that out no exception is being thrown. The first procedure call actually works and my first listview box is populated with the data from the array. Once the loop executes my the form UI is displayed and the data is displayed in my first list box. It just not goes to the next sub to populate my other list boxes. Code below:
Code:
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
AddHandoverItems()
Catch ex As Exception
Throw ex
End Try
End Sub
Sub AddHandoverItems()
'Create columns in listview
With lstvHandover
.View = View.Details
.LabelEdit = True
.FullRowSelect = True
.GridLines = True
.Columns.Add("Shift ID", 65, HorizontalAlignment.Left)
.Columns.Add("Current Status", 400, HorizontalAlignment.Left)
.Columns.Add("Needed", 400, HorizontalAlignment.Left)
.Columns.Add("To Do", 400, HorizontalAlignment.Left)
.Columns.Add("Tracker Ticket", 65, HorizontalAlignment.Left)
.Columns.Add("NOV POC", 75, HorizontalAlignment.Left)
.Columns.Add("NOV POC Phone", 100, HorizontalAlignment.Left)
.Columns.Add("NOV POC Email", 100, HorizontalAlignment.Left)
.Columns.Add("Rig Contact", 75, HorizontalAlignment.Left)
.Columns.Add("Rig Phone", 120, HorizontalAlignment.Left)
.Columns.Add("Rig Email", 175, HorizontalAlignment.Left)
.Columns.Add("Rig ID", 60, HorizontalAlignment.Left)
.Columns.Add("Third Party", 75, HorizontalAlignment.Left)
.Columns.Add("Third Party Phone", 100, HorizontalAlignment.Left)
.Columns.Add("Third Party Email", 100, HorizontalAlignment.Left)
.Columns.Add("Date", 50, HorizontalAlignment.Left)
.Columns.Add("Handover Record", 30, HorizontalAlignment.Left)
End With
'Create item to hold contents of handover array
Dim itm As ListViewItem
Dim handoveritems As String
Dim i As Integer = 0
If IsNothing(Database_Logins.Turnover) Then
ToDoListItems()
Else
For Each handoveritems In Database_Logins.Turnover
'Add data from Database logins array to listview item
itm = New ListViewItem(New String() {Database_Logins.Turnover(i, 0), Database_Logins.Turnover(i, 1), Database_Logins.Turnover(i, 2), _
Database_Logins.Turnover(i, 3), Database_Logins.Turnover(i, 4), Database_Logins.Turnover(i, 5), _
Database_Logins.Turnover(i, 6), Database_Logins.Turnover(i, 7), Database_Logins.Turnover(i, 8), _
Database_Logins.Turnover(i, 9), Database_Logins.Turnover(i, 10), Database_Logins.Turnover(i, 11), _
Database_Logins.Turnover(i, 12), Database_Logins.Turnover(i, 13), Database_Logins.Turnover(i, 14), _
Database_Logins.Turnover(i, 15), Database_Logins.Turnover(i, 16)})
'Add item to Handover listview
lstvHandover.Items.Add(itm)
i += 1
Next handoveritems
ToDoListItems()
End If
End Sub
Public Sub ToDoListItems()
'Add user to employee label
Dim strEmployee As String
strEmployee = Database_Logins.Name
lblDailyToDo.Text = strEmployee & " Daily To Do's"
'Create columns in listview
With lstvToDo
.View = View.Details
.LabelEdit = True
.FullRowSelect = True
.GridLines = True
.Columns.Add("User ID", 65, HorizontalAlignment.Left)
.Columns.Add("Memo", 200, HorizontalAlignment.Left)
.Columns.Add("Date", 75, HorizontalAlignment.Left)
End With
'Create item to hold contents of handover array
Dim itm As ListViewItem
Dim i As Integer = 0
Dim todoitems As String
If IsNothing(Database_Logins.EmployeToDo) Then
AnnouncementListsItems()
Else
For Each todoitems In Database_Logins.EmployeToDo
itm = New ListViewItem(New String() {Database_Logins.EmployeToDo(i, 0), Database_Logins.EmployeToDo(i, 1), Database_Logins.EmployeToDo(i, 2), _
Database_Logins.EmployeToDo(i, 3), Database_Logins.EmployeToDo(i, 4)})
'Add item to ToDo Listview
lstvToDo.Items.Add(itm)
i += 1
Next
AnnouncementListsItems()
End If
End Sub
It won't proceed to the other procedure calls. It just finishes the first one then loads the UI with no exception.
-
Oct 26th, 2011, 04:09 AM
#4
Re: Code does not execute after For Each Next Loop
You've done the complete opposite of what I indicated. Keep the MessageBox and get rid of the Throw.
-
Oct 27th, 2011, 05:38 AM
#5
Thread Starter
New Member
Re: Code does not execute after For Each Next Loop
So the real question is, how do I get every item from my listview into my listview control. When I use:
HTML Code:
For i = 0 To UBound(Database_Logins.Turnover)
'Add data from Database logins array to listview item
itm = New ListViewItem(New String() {Database_Logins.Turnover(i, 0), Database_Logins.Turnover(i, 1), Database_Logins.Turnover(i, 2), _
Database_Logins.Turnover(i, 3), Database_Logins.Turnover(i, 4), Database_Logins.Turnover(i, 5), _
Database_Logins.Turnover(i, 6), Database_Logins.Turnover(i, 7), Database_Logins.Turnover(i, 8), _
Database_Logins.Turnover(i, 9), Database_Logins.Turnover(i, 10), Database_Logins.Turnover(i, 11), _
Database_Logins.Turnover(i, 12), Database_Logins.Turnover(i, 13), Database_Logins.Turnover(i, 14), _
Database_Logins.Turnover(i, 15), Database_Logins.Turnover(i, 16)})
i += 1
'Add item to Handover listview
lstvHandover.Items.Add(itm)
Next
I do not get the error anymore but all of my data elements are not added to my listview control.
When I use the For each method with a string or For 1=0 to Database_Logins.Turnover.Length it adds all the data elements but gives me the error, index is outside the bounds of the array. Please advise.
-
Oct 27th, 2011, 08:46 AM
#6
Re: Code does not execute after For Each Next Loop
What does "i += 1" mean/do?
Are you trying to increment i inside your loop for some reason?
-
Oct 27th, 2011, 02:25 PM
#7
Re: Code does not execute after For Each Next Loop
Hint about arrays
Code:
Dim bar(9, 5) As Integer
For x As Integer = 0 To UBound(bar, 1)
bar(x, 5) = x + 10
Debug.WriteLine(bar(x, 5).ToString)
Next
It is not recommended to manipulate the for variable within the loop.
-
Oct 27th, 2011, 07:41 PM
#8
Thread Starter
New Member
Re: Code does not execute after For Each Next Loop
Yes 'i += 1' is when I am increment the For variable. How else do i increment it in VB? I am dynamically dimensioning my array in another class. I have a database that will get update often so the array will change vary in size. Once I fill the array with all the data I then want it copied into a listview control as above.
As stated before I've tried all I know to try from the For each method with a string, For 1=0 to Database_Logins.Turnover.Length, the same for loop with (databaselogins.turnover.length) - 1, UBound(database_logins.turnover) -1. The only one that works halfway is using the Ubound but it doesn't copy all of my data elements from the array. It runs through the loop once and only adds the first row of data into the array. Now I have two rows with the possibility of 30 rows. Please advise.
-
Oct 27th, 2011, 08:15 PM
#9
Thread Starter
New Member
Re: Code does not execute after For Each Next Loop
Gentlemen thanks for schooling me. It was my increment statement that was causing the problem. Final code:
vb Code:
For i = 0 To UBound(Database_Logins.Turnover) 'Add data from Database logins array to listview item itm = New ListViewItem(New String() {Database_Logins.Turnover(i, 0), Database_Logins.Turnover(i, 1), Database_Logins.Turnover(i, 2), _ Database_Logins.Turnover(i, 3), Database_Logins.Turnover(i, 4), Database_Logins.Turnover(i, 5), _ Database_Logins.Turnover(i, 6), Database_Logins.Turnover(i, 7), Database_Logins.Turnover(i, 8), _ Database_Logins.Turnover(i, 9), Database_Logins.Turnover(i, 10), Database_Logins.Turnover(i, 11), _ Database_Logins.Turnover(i, 12), Database_Logins.Turnover(i, 13), Database_Logins.Turnover(i, 14), _ Database_Logins.Turnover(i, 15), Database_Logins.Turnover(i, 16)}) 'Add item to Handover listview lstvHandover.Items.Add(itm) Next ToDoListItems()
You guys are awesome. I really appreciate the assistance. So when using arrays I should not increment my variable?
-
Oct 28th, 2011, 06:22 AM
#10
Re: Code does not execute after For Each Next Loop
 Originally Posted by iderf
...You guys are awesome. I really appreciate the assistance. So when using arrays I should not increment my variable?
The For...Next loop does it for you. http://msdn.microsoft.com/en-us/library/5z06z1kb.aspx
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|