Question about Msgbox in For loop

Toshiba Satellite l505d-s5965 notebook
November 3, 2010 at 19:14:36
Specs: Windows 7, 1024
How can i prevent alot of msgboxs from showing but only once and move to the next row etc. to show data.

Code:---------------------------------------------------------------------------------------------
Dim i As Integer
For i = 0 To Table1DataGridView.Rows.Count - 1
Dim row As DataGridViewRow
row = Table1DataGridView.Rows(i)
Dim j As Integer
For j = 1 To row.Cells.Count - 1
' MsgBox(i & vbCrLf & "" & vbCrLf & j)

Dim S As String = Me.Table1DataGridView.Rows(i).Cells(2).Value
Dim K As String = Me.Table1DataGridView.Rows(i).Cells(1).Value
If Date.Today > Due_Date_AssignedDateTimePicker.Text Then
MsgBox(S & vbCrLf & "" & vbCrLf & K)
End If
Next
Next
-------------------------------------------------------------------------------------------------------------------------------
my code just checks each row and compair the date and if the date is greater than it will popup with a Msgbox but it shows multple of the same thing. How come and how can i only show one popup per row.

maybe something in my code who knows. thank you all for you help.


See More: Question about Msgbox in For loop

Report •

#1
November 3, 2010 at 23:51:41
You cannot do it, not the way you are trying anyway. The Message box was never meant to be used in this way.

What you need to do is create a form with a couple of text boxes and write the data to the text boxes. If necessary an array of text boxes for each line of data.

A couple of tips for you that will make things easier.

Put all you Dim statements at the beginning of the code. It makes it a lot easier to follow the code with getting bogged down with Dim statement.

Where to have Dim S As String = Me.Table1DataGridView.Rows(i).Cells(2).Value Put Dim S as String at the beginning of the code and S = Me.Table1DataGridView.Rows(i).Cells(2).Value in the code. The way you have it I am surprised it doesn't throw up an error dimensioning the same variable over and over again inside a loop. Not a good idea At best it is going to make things run slower than they should.

Where you have For i = 0 To Table1DataGridView.Rows.Count - 1, put the value into a variable and use the variable in the for/next loop. It speeds things up considerably. They way you have it the value of Table1DataGridView.Rows.Count - 1 has to be recalculated every time through the loop. Put it in a variable and it is only calculated once. Can make a huge difference with large data sets.

Stuart


Report •

#2
November 4, 2010 at 20:43:58
thank you that helped.

I did create a form and i call the form but only the first row is show. What would be the best why to call the form only if the date is greater that was is on the row in Cell2.

my code below just call the rows and such but i cant figure out how to loop each row if the date is greater then popup and show the form that holds the data.

I do have the form set to show the data from the rows thats not a problem but only issues is the looping for rows and showing the changing data is need.

thank you for ur help. New to VB but really like it and learning alot. :)

Dim i As Integer
For i = 0 To Table1DataGridView.Rows.Count - 1
Dim row As DataGridViewRow
row = Table1DataGridView.Rows(i)
Dim j As Integer
For j = 1 To row.Cells.Count - 1
' MsgBox(i & vbCrLf & "" & vbCrLf & j)

Dim S As String = Me.Table1DataGridView.Rows(i).Cells(2).Value
Dim K As String = Me.Table1DataGridView.Rows(i).Cells(1).Value

If Date.Today > Due_Date_AssignedDateTimePicker.Text Then

' Loop here and show Form'


End If
Next
Next


Report •

#3
November 5, 2010 at 07:12:50
Any reference to a control or variable on a form will implicitly load that form but not show it. To actually show the form all you need is FormName.Show. However if to want do display all the difference it is likely to happen too fast for it all to me seen.

You would be better putting the differences into an array and displaying the array when the for/next loop has finished.


Dim Changes() as string
Dim i as integer
Dim Count as Integer
Dim Duedate as Date

i = row.Cells.Count - 1
DueDate = Due_Date_AssignedDateTimePicker.Text


For j = 1 To i

If Date.Today > DueDate Then

RedDim Preserve changes(count)
Changes(count) = Date.Totday
Count = Count + 1
End if
next

You can then display the array on a form, possibly in a text box

This uses a dynamic array. This is useful when you need an array but don't know how many elements it is going to have at the beginning. If you do know you cant just use the Dim statement

Dim Changes(10) as string

If you are going to do any serious data processing with VB you need to learn all about Arrays. Single dimension arrays, multiple dimension arrays, The Redim Preserve statement is a powerful tool when used properly.

Notice how I have Dimed statements and assigned values outside of the For/next loop. You should only reference a calculation inside a loop if the loop causes the result of the calculation to change. If the result remains the same you should allocate it a variable. This seems the case with Due_Date_AssignedDateTimePicker.Text but not Me.Table1DataGridView.Rows(i).Cells(1).Value

Stuart


Report •

Related Solutions

#4
November 5, 2010 at 08:18:50
so i would then after the NEXT in the loop call the form to show the data then?

Dim K As String = Me.Table1DataGridView.Rows(i).Cells(1).Value
that line gets the cell data from Cell one in my gridview. How would that fit in the loop to get the data that changes.

what im trying to do is loop each each and when the date fall on or greater than show the date and cell 1 data which the user types in the Note or reminder.

Im just creating a reminder program thats sql database driven.

thank you for ur help
Brock


Report •

#5
November 5, 2010 at 09:32:45
Dim K As String = Me.Table1DataGridView.Rows(i).Cells(1).Value
that line gets the cell data from Cell one in my gridview. How would that fit in the loop to get the data that changes.

You wouldn't

You would put Dim K as String outside the loop
and
String = Me.Table1DataGridView.Rows(i).Cells(1).Value
inside the loop

You do not need to RedIm as variable every time you use it. Once is enough.

Stuart


Report •

#6
November 5, 2010 at 11:08:24
ah ok now i understand, so if its time based how could i have the code open the form more than once to show each row/data that didn't meant the Date requirements?

i mean there will be or could be if a few dates etc that are greater and have the form popup more than once to show each date and note.

does that make sense?

thank you again uve been alot of help.
Brock


Report •

#7
November 5, 2010 at 11:53:25
You wouldn't.

You would store the difference inside the via the dynamic array and then load the form and display the differences once the loop has completed.

Anything that activates the video display inside a loop is going to slow things down considerably and should be avoided whenever possible.

If you must show the differences as they occur you need to put the code in a function in the form module that is going to display it and write the differences to a text box.

Stuart


Report •

Ask Question