For Each...Next problem...
Here is my code. I have the Timer set at 2 seconds for now. It pulls the data in fine, sets the text for the label fine. But it only shows the last row of data and does not rotate through the rows.
Any ideas?
Code:
Public Class MarqueTest
Private Sub MarqueTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AdminMessTableAdapter.Fill(Me.MarqueDataSet.AdminMess)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim dr As DataRow
For Each dr In Me.MarqueDataSet.AdminMess.Rows
TitleLabel1.Text = dr.Item("DateSubmited") & " - " & dr.Item("Title") & " - POSTED BY " & dr.Item("CallSignSubmitted")
Next
End Sub
End Class
Re: For Each...Next problem...
try this:
Code:
For Each dr In Me.MarqueDataSet.AdminMess.Rows
TitleLabel1.Text += dr.Item("DateSubmited") & " - " & dr.Item("Title") & " - POSTED BY " & dr.Item("CallSignSubmitted").tostring & Environment.NewLine
Next
You keep clearing the textbox instead of adding text into it
Code:
TitleLabel1.Text = "Hello" ' Clear the textbox and add the string "Hello"
TitleLabel1.Text += " Hello" ' Adding the string "Hello" to the text that already stored inside the text box.
Re: For Each...Next problem...
@Motil
A little info, use &= instead of += ;)
Re: For Each...Next problem...
Why? What's wrong with += vs &= .... both have been overloaded for strings and produce the same result.
-tg
Re: For Each...Next problem...
I think mickey has a point it could raise an expectation if you mix strings with integers i believe don't it ?
Re: For Each...Next problem...
In this particular situation i think there isn't any problem, but i think it's a good practice to use & for strings and + for numbers :)
Re: For Each...Next problem...
you're right my friend but when you working with 3-5 languages at the same time it's hard to remember the correct syntax all the time, i'm working a lot with javascript at my work and += is the way to go in any situation
Re: For Each...Next problem...
I was aiming for a little explanation to go with it... which is what I was trying elicit, and did get... on why using &= might be preferable over += .... to some of us, it's obvious, but to others it isn't so...
That is all. And now back to your regularly scheduled postings.
-tg
Re: For Each...Next problem...
Thank you all for your quick replys, I will try the suggestions. I just wanted to make sure we are on the same page.
I am intending for the first row of data to show up and as the timer refreshes it moves to the next row of data until the end and starts over. Each time setting the text as described. Thus created alternating text like a new tickers, etc.
Re: For Each...Next problem...
Your problem is that EVERY time the Timer Ticks you loop through ALL the data and show EVERY row, one after the other. As such EVERY time the Timer Ticks you're going to show the first row, then replace it with the second, then replace it with the third, ... then replace it with the last. That's why you always see only the last record. All the others get replaced so quickly that you never see them.
What you should do is set an Integer variable to 0 and then, in the Tick event handler, you get the row at that index and then increment it by 1. That means that the first Tick will show the first row, the second Tick the second row, etc., etc. You'll obviously need to check whether you're at the last row each time and Stop the Timer if you are.
Re: For Each...Next problem...
Thank you very much for that. I am understanding the process now but can you just give an example of how code for increasing by 1 would be.
I asume
dim start As Integer
start = 0
or something like that?
Re: For Each...Next problem...
That start var must be declared at the level of the class.
Something like this:
VB.NET Code:
Dim start As Integer = 0
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim dr As DataRow
If MarqueDataSet.Tables("AdminMess").Rows.Count > start Then
dr = MarqueDataSet.Tables("AdminMess").Rows(start)
TitleLabel1.Text = dr.Item("DateSubmited") & " - " & dr.Item("Title") & " - POSTED BY " & dr.Item("CallSignSubmitted")
start += 1
Else
Timer1.Stop()
End If
End Sub
Re: For Each...Next problem...
Re: For Each...Next problem...
Ok, next smal issue. I your code examle and it works like a champ BUT I cannot get it to do it continuiously. I have tried Do/Loop and other methods to no resolve.
Re: For Each...Next problem...
Do it continuously? What are you trying yo achieve?
If you want that code never stop instead of
put