Private Sub btn1_Click(sender As System.Object, e As System.EventArgs) Handles btn1.Click
ProgressBar1.Maximum = 100
ProgressBar1.Step = 20
Dim names(4, 1) As String
names(0, 0) = "john"
names(0, 1) = "dole"
names(1, 0) = "Kylie"
names(1, 1) = "higgins"
names(2, 0) = "jade"
names(2, 1) = "tugwell"
names(3, 0) = "michael"
names(3, 1) = "straps"
names(4, 0) = "cyndi"
names(4, 1) = "beamon"
lstBox1.Items.Clear()
For i As Integer = 0 To 4
lstBox1.Items.Add(names(i, 0) + " " + names(i, 1))
ProgressBar1.PerformStep()
Next
End Sub
array with timer
Page 1 of 19 Replies - 203 Views - Last Post: 23 February 2013 - 06:14 AM
#1
array with timer
Posted 21 February 2013 - 03:51 AM
hi guys im stuck on how to do a timer on my array so that it displays each full name in my list box every 2 seconds and have the progress bar step each time
Replies To: array with timer
#2
Re: array with timer
Posted 21 February 2013 - 06:44 AM
Hi cirustus,
You could use a sleep command inside the for i loop like this. The 2000 is the milliseconds that it sleeps for which would be 2 seconds. The other way is to use a timer and have the timer tick event add the names and step the progress bar.
You could use a sleep command inside the for i loop like this. The 2000 is the milliseconds that it sleeps for which would be 2 seconds. The other way is to use a timer and have the timer tick event add the names and step the progress bar.
Threading.Thread.Sleep(1000)
#3
Re: array with timer
Posted 21 February 2013 - 06:50 AM
This blocks the current thread it is placed on, and this case it'll be the GUI thread. Thus locking up / freezing the application.
#4
Re: array with timer
Posted 21 February 2013 - 07:09 AM
AdamSpeight2008, on 21 February 2013 - 08:50 AM, said:
This blocks the current thread it is placed on, and this case it'll be the GUI thread. Thus locking up / freezing the application.
Yes AdamSpeight2008 is correct the Threading.Thread.Sleep() will freeze the thread that your program is running on and will cause the items to not be added to the listbox every 2 seconds so, I would try to use a timer to do it in the timer tick event.
#5
Re: array with timer
Posted 21 February 2013 - 07:13 AM
It'll still add the items every two seconds, but'll freeze the application during those two seconds.
#6
Re: array with timer
Posted 21 February 2013 - 08:15 AM
It will add the items but, they do not show in the textbox until the for next loop is done. I guess you could use
before the the sleep() command to update the listbox on each step. However, it does freeze the thread your program is running on so the user can`t do anything with the program until the for next loop is finished. A timer would not freeze it but may be a little more complex to set up.
ListBox1.Update()
before the the sleep() command to update the listbox on each step. However, it does freeze the thread your program is running on so the user can`t do anything with the program until the for next loop is finished. A timer would not freeze it but may be a little more complex to set up.
#7
Re: array with timer
Posted 21 February 2013 - 08:51 AM
You could use a BackgroundWorker and have the .DoWork .ReportProgress every two seconds. The .ReportProgress event handler could update the listbox.
http://msdn.microsof...v=vs.90%29.aspx
http://msdn.microsof...v=vs.90%29.aspx
#8
Re: array with timer
Posted 21 February 2013 - 11:09 PM
IronRazer, on 21 February 2013 - 06:44 AM, said:
Hi cirustus,
You could use a sleep command inside the for i loop like this. The 2000 is the milliseconds that it sleeps for which would be 2 seconds. The other way is to use a timer and have the timer tick event add the names and step the progress bar.
You could use a sleep command inside the for i loop like this. The 2000 is the milliseconds that it sleeps for which would be 2 seconds. The other way is to use a timer and have the timer tick event add the names and step the progress bar.
Threading.Thread.Sleep(1000)
thankyou very much this help lots as well as
listbox1.update()
im now getting the names to list 1 after the other just gotta try work out how to make the progress bar step as the first name is displayed as it is currently stepping on the second name
#9
Re: array with timer
Posted 22 February 2013 - 04:47 PM
You might need to change the place where you put the 2 lines. If you are still using the same for loop from your first post then try this :
This should do the following in this order - add the names to the listbox , step the progressbar, update the listbox, and then sleep 2 seconds. When i tested it it stepped the progressbar on the first name.
For i As Integer = 0 To 4
lstBox1.Items.Add(names(i, 0) + " " + names(i, 1))
ProgressBar1.PerformStep()
'Update the listbox here
'Put the sleep command here
Next
This should do the following in this order - add the names to the listbox , step the progressbar, update the listbox, and then sleep 2 seconds. When i tested it it stepped the progressbar on the first name.
This post has been edited by IronRazer: 22 February 2013 - 04:48 PM
#10
Re: array with timer
Posted 23 February 2013 - 06:14 AM
get rid of the loop, let the timer handle the iteration by incrementing i, adding to the listbox, and incrementing the progressbar.
To do this you will need to declare i and names() is a wider scope so that they're accessible in the timer routine.
To do this you will need to declare i and names() is a wider scope so that they're accessible in the timer routine.
Public Class Form1
'declared at class level which makes them available throughout all routines in the class
Dim i As Integer = 0
Dim names(4, 1) As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'set up your timings
ProgressBar1.Maximum = 100
ProgressBar1.Step = 20
Timer1.Interval = 2000 'This is approx 2 seconds
names(0, 0) = "john"
names(0, 1) = "dole"
names(1, 0) = "Kylie"
names(1, 1) = "higgins"
names(2, 0) = "jade"
names(2, 1) = "tugwell"
names(3, 0) = "michael"
names(3, 1) = "straps"
names(4, 0) = "cyndi"
names(4, 1) = "beamon"
'reset pb and lb in case we it the button again.
progressbar1.value =0
lstBox1.Items.Clear()
'Everything's set up, the listbox is clearStart the timer
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'With each tick of the timer
lstBox1.Items.Add(names(i, 0) & " " & names(i, 1))
ProgressBar1.PerformStep()
'Now that names(i,0) & " " & names(i,1) has been added, we need to increment i for the next tick
i += 1
'if we are over 4 we stop the timer as we are done. Of course, you will want to make these variables that
'coincide with the bounds of your array so that if you add a names(5,0) it will appear.
If i > 4 Then Timer1.Stop()
End Sub
End Class
This post has been edited by CharlieMay: 23 February 2013 - 06:18 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|