Refresh ListView on Timer_Tick - vb.net

I'm updating my ListView in VB.net every 5 seconds on the timer_ticker event.
But I have a problem, I don't want the list to "blink" whenever it refreshes.
Is there a way to do this?
I filled the Listview in a function seperately but it didn't make any difference.
Here's my code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
Timer1.Enabled = False
FillListView()
Catch ex As Exception
Finally
Timer1.Enabled = True
End Try
End Sub

Related

Progress bar not updating on a separate form

I am trying to make a progress bar on a separate form when doing other tasks in my application. everything opens up and shows correctly but progress bar is not updating. I tried background worker with no joy. Code is in VB.net
Please help. I must be missing a step or two in my code.
What I am trying to do is when listview is clicked, takes a bit to populate textboxes out of my excel database, so I would like the another form to open and update progress bar accordingly.
Hope someone can help.
This is my Click Event:
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
Application.EnableVisualStyles()
BackgroundWorker1.RunWorkerAsync()
frmProgress.Show()
AddToDatabaseToolStripMenuItem.Enabled = False
frmProgress.Close()
End Sub
Then I have Background Worker code as follows:
Private Sub backgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
For i As Integer = 1 To 100
showData()
Thread.Sleep(1000)
BackgroundWorker1.ReportProgress(CInt(100 * i / 100), "Running..." & i.ToString)
Next
End Sub
Private Sub backgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs)
frmProgress.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles BackgroundWorker1.RunWorkerCompleted
' Called when the BackgroundWorker is completed.
Label1.Text = "Loading... Done"
End Sub
Then my Progress form Load event is this:
Private Sub frmProgress_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Application.EnableVisualStyles()
With ProgressBar1
.Style = ProgressBarStyle.Blocks
.Visible = True
End With
Me.Text = "IDB"
Label1.Text = "Loading... Please wait"
Me.Cursor = Cursors.WaitCursor
End Sub
ShowData() is in the module and is the code that is loading and takes few seconds to load.
Thank you for your help.

Debugging keeps going to a timer1_tick event in a closed form

I'm using a Parent form to show my other forms as MdiChild forms like this:
If ActiveMdiChild IsNot Form4 Then
ActiveMdiChild.Close()
Form4.MdiParent = Me
Form4.Dock = DockStyle.Fill
Form4.AutoScroll = False
Form4.Show()
End If
One of my forms (TimerForm) has a timer, but when I put a break-point in another form (Form4) and follow the code and before the debugging is finished, it goes to the timer1_event in the closed TimerForm.
I tried this on TimerForm:
Private Sub TimerForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Timer1.Enabled = False
Timer1.Stop()
Timer1.Dispose()
End Sub
I also tried ActiveMdiChild.Dispose() with no luck.
How to stop the Timer from firing after the form is closed ?
Thank you.
Thank you all.
I solved my problem by programmatically adding Timer1_Tick event:
Private Sub TimerForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler Timer1.Tick, AddressOf Timer1_Tick
Timer1.Start()
End Sub
And removing the event on FormClosing:
Private Sub TimerForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Timer1.Stop()
Timer1.Enabled = False
Timer1.Dispose()
RemoveHandler Timer1.Tick, AddressOf Timer1_Tick
End Sub

Message Box showing multiple times when progress bar complete in VB.NET

I am trying some with progress bar, but it's not showing popup correctly. When i use msgbox, its appears 100s of times and when I use form2 by replacing msgbox it keep showing even I close it.
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(1)
If ProgressBar1.Value = ProgressBar1.Maximum Then
MsgBox("Done")
End If
End Sub
End Class
If you want to show message only once then stop the timer before the message box
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(1)
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Stop()
MsgBox("Done")
End If
End Sub
This is because you are not disable or Stop the timer. when the ProgressBar1.Value reaches the maximum the message box will display as "Done" but timer is still executing so you will get the message till timer is disabled, since the condition If ProgressBar1.Value = ProgressBar1.Maximum Then is true. so you need to disable the timer if the condition is true.
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Enabled = False
MsgBox("Done")
End If
or you can use Timer1.Stop()

DevExpress CheckButton not toggeling?

I have some farmiliaraty with VB.net, but as of yeaterday only 1 day experiance with DevExpress. The functions seam quite easy but the occasional question is bound to pop-up.
My first question is:
How can I create a toggel button which toggels the timer on or off using the DevExpress "Check button" control. the code I have currently in use will only stop my timmer but not restart it when I click my Checkbutton control a second time?
Private Sub CheckButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckButton1.CheckedChanged
If True Then
Timer1.Stop()
Else
Timer1.Start()
End If
End Sub
Just use the CheckButton.Checked property as follows:
Private Sub CheckButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckButton1.CheckedChanged
If CheckButton1.Checked Then
Timer1.Stop()
Else
Timer1.Start()
End If
End Sub

How to use timer for for next loop in visual basic 2008

I have a case where i need to generate millions of unique codes. For this I have created a generate function where the random number is generated. I call this function from a for loop and add the generated number on a list box. my code is as follow
for i=1 to val(txtnumber.txt)
mynum=generate()
next
I have created a lable on form where i wanted to display the no of secs elapsed while processing the loop. I used timer control as
timer1.start()
for i=1 to val(txtnumber.text)
mynum=generate()
listbox1.items.add(mynum)
next
timer1.stop
and on timer1_tick function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Val(Label1.Text) + 1
End Sub
but when i click generate button, all numbers are generated, but timer doesnot shows time elapsed.
I may have missed something, so please help me out
This is probably best handled in a BackgroundWorker. Place one on the form and set its WorkerReportsProgress=True. Also, placing a million numbers in a ListBox probably isn't a good idea, so I omitted that.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Button1.Enabled = False
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim started As DateTime = Now
For i As Integer = 1 To val(txtnumber.txt)
mynum=generate()
BackgroundWorker1.ReportProgress(i, Nothing)
Next
Dim ended As TimeSpan = Now.Subtract(started)
BackgroundWorker1.ReportProgress(0, ended.TotalSeconds.ToString)
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If e.UserState IsNot Nothing Then
Label1.Text = e.UserState.ToString()
Else
Label1.Text = e.ProgressPercentage.ToString
End If
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Button1.Enabled = True
End Sub
Your label should be updating correctly when the worker reports the ProgressChanged event.
What you're encountering is a threading issue. The work you are doing to generate the numbers is being executing by the UI thread, so it never gets a chance to update the screen. Take a look here: How to prevent UI from freezing during lengthy process?
This one might also have good information for you: Updating UI from another thread
Try this:
Private _Counter As Integer = 0
Private _StartTime As Date = Now
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
_StartTime = Now
_Counter = CInt(Val(txtnumber.Text))
ListBox1.Items.Clear()
Label1.Text = "0"
Timer1.Interval = 50
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
ListBox1.Items.Add(generate())
Label1.Text = New Date((Now - _StartTime).Ticks).ToString("HH:mm:ss.ff")
_Counter -= 1
If (_Counter <= 0) Then
Timer1.Stop()
End If
End Sub
Or you can research actual Threading.