VB Showing progress in a Label on Form1 - vb.net

I have a Form1 with a "Rename" button: btnRename.
On Form1 I also have a label: lblProgress
Private Sub btnRename_Click(sender As Object, e As EventArgs) Handles btnRename.Click
Call Do_Rename()
Me.Close()
End Sub
Private Sub Do_Rename()
Call Get_File_names()
digits = 5
lblProgress.Text = "1 of 15"
Call Renumbering()
End Sub
When I click on the "Rename" button I want
to do some file renaming
to show progress like:
1 of 18
5 of 18
10 of 18
15 of 18
When I set: lblProgress.Text = "1 of 18", nothing shows up on the form!

You should do the following:
Create a BackgroundWorker
Call RunWorkerAsync in the button's click event
In the DoWork event of the BackgroundWorker, loop over the files to do your file renaming
Call ReportProgress in the loop based on the currently iterated file
In the ProgressChanged event of the BackgroundWorker, update the label's text based on the data sent back from the ReportProgress method
Example:
Private Sub btnRename_Click(sender As Object, e As EventArgs) Handles btnRename.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker = DirectCast(sender, BackgroundWorker)
For i = 0 To files.Count - 1
worker.ReportProgress(i, $"{i + 1} of {files.Count}")
' do your opeations on files
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
lblProgress.Text = e.UserState.ToString()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
lblProgress.Text = "BackgroundWorker completed."
End Sub

Add Doevents or Application.DoEvents() before and after change the label text or you can follow BackgroundWorker as mention by David.
Private Sub Do_Rename()
Call Get_File_names()
Application.DoEvents()
digits = 5
lblProgress.Text = "1 of 15"
Application.DoEvents()
Call Renumbering()
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.

Change the color of a button(s) for a duration of time

I have a small VB.net app that has a LOT of buttons. I need to change the back color of the buttons when they are clicked and then set it back to its original color after a duration of 10 seconds. I am struggling with either using a timer or the time process both of which have their own issues.
Any ideas to make this work and work efficiently?
Code:
Private Sub MyButtons_Click(sender As Object, e As EventArgs) _
Handles Button1.Click,
Button2.Click
Dim myButton = DirectCast(sender, Button)
MakeCall()
myButton.BackColor = Color.Green
'TurnOnActiveCallCOLOR.Enabled = True
For i As Integer = 0 To 10000 - 1
Threading.Thread.Sleep(10000)
Next
myButton.BackColor = Color.FromArgb(64, 64, 64)
End Sub
Here is an example of using a Windows Forms Timer to accomplish what you need:
Private MyButton As Button
Private Sub MyButtons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
MyButton = DirectCast(sender, Button)
MyButton.BackColor = Color.Green
Timer1.Enabled = True
MakeCall()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
MyButton.BackColor = Color.FromArgb(64, 64, 64)
Timer1.Enabled = False
End Sub
To handle an arbitrary number of buttons, you could have something like this:
Public Class Form1
Dim buttonTimers As New Dictionary(Of Control, ButtonTimer)
Class ButtonTimer
Property Timeout As Integer = 2000
Property Target As Control
Property ActiveColor As Color = Color.Green
Property DefaultColor As Color = Color.FromArgb(64, 64, 64)
Private tim As Timer
Sub TimTick(sender As Object, e As EventArgs)
tim.Stop()
Target.BackColor = DefaultColor
End Sub
Sub New()
' empty constructor
End Sub
Sub New(target As Control)
Me.Target = target
Me.Target.BackColor = Me.ActiveColor
tim = New Timer With {.Interval = Timeout}
AddHandler tim.Tick, AddressOf TimTick
tim.Start()
End Sub
Sub Restart()
Target.BackColor = Me.ActiveColor
If tim IsNot Nothing Then
tim.Stop()
tim.Start()
End If
End Sub
Public Sub DisposeOfTimer()
If tim IsNot Nothing Then
tim.Stop()
RemoveHandler tim.Tick, AddressOf TimTick
tim.Dispose()
End If
End Sub
End Class
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Dim myButton = DirectCast(sender, Button)
'MakeCall()
If buttonTimers.ContainsKey(myButton) Then
buttonTimers(myButton).Restart()
Else
buttonTimers.Add(myButton, New ButtonTimer(myButton))
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
For Each x In buttonTimers
x.Value.DisposeOfTimer()
Next
End Sub
End Class
If a button is clicked again before the timeout, the time is restarted.
You can add other constructors if you want to have a different timeout/colours for different buttons.
The MyBase.FormClosing code should be included in your form closing handler (if there is one) so that the timers are cleaned up properly.
I expect it would be tidier overall to make your own custom button class which inherits from Button, so you might want to investigate doing that. (How to: Inherit from Existing Windows Forms Controls.)
asynh and await if you don't want use timer. Simple method using task.delay
Private Async Sub ButtonClick(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click
DirectCast(sender, Button).BackColor = Color.Red 'after click setcolor to red
Await setColorAfterDelay(sender) 'Add this comand on button click and don't forget add asynh before sub in this method
End Sub
Public Async Function setColorAfterDelay(sender As Object) As Task
Await Task.Delay(1000) ''Milisecound how long you wana dealey
DirectCast(sender, Button).BackColor = Color.White 'and set colorto white
End Function
With the use of lambda expressions (and a lookup table if you want to interact with it further) you can do this pretty easily:
'Lookup table for if you want to be able to interact with the timers even more.
Dim ButtonTimers As New Dictionary(Of Button, Timer)
Private Sub MyButtons_Click(sender As Object, e As EventArgs) _
Handles Button1.Click, Button2.Click
MakeCall()
Dim myButton = DirectCast(sender, Button)
myButton.BackColor = Color.Green
'If a timer already exists for the button, restart it.
Dim existingTimer As Timer = Nothing
If ButtonTimers.TryGetValue(myButton, existingTimer) Then
existingTimer.Stop()
existingTimer.Start()
Return 'Do not execute the rest of the code.
End If
'Create the timer and set its Interval to 10000 ms (10 seconds).
Dim buttonTimer As New Timer() With {.Interval = 10000}
'Add a handler to its Tick event.
AddHandler buttonTimer.Tick, _
Sub(tsender As Object, te As EventArgs)
myButton.BackColor = Color.FromArgb(64, 64, 64)
'Dispose timer and remove from lookup table.
ButtonTimers.Remove(myButton)
buttonTimer.Stop()
buttonTimer.Dispose()
End Sub
ButtonTimers.Add(myButton, buttonTimer)
buttonTimer.Start()
End Sub
If you want to interact with a button's timer (if one exists) you can do:
Dim buttonTimer As Timer = Nothing
If ButtonTimers.TryGetValue(yourButtonHere) Then
'Do something with buttonTimer...
End If
This could probably be done better by dynamically creating timer controls so each button has it's own timer but here is what I came up with.
Oh, an set your timer to 1000 interval and enabled to 'False'.
Public Class Form1
Dim T1 As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
T1 = T1 + 1
If T1 = 10 Then
For Each button In Controls
button.backcolor = Color.FromArgb(225, 225, 225)
Next
Timer1.Stop()
T1 = 0
End If
Me.Text = T1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.BackColor = Color.Red
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Button2.BackColor = Color.Orange
Timer1.Start()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Button3.BackColor = Color.Yellow
Timer1.Start()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Button4.BackColor = Color.Green
Timer1.Start()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Button5.BackColor = Color.Blue
Timer1.Start()
End Sub
End Class

Late Binding Issue with BackgroundWorker in VB.Net

I am running a BackgroundWorker, and want to report its progress. In the example below I create a test list which the BackgroundWorker then iterates through. The problem lies in the line 'sender.ReportProgress(i)'. If I have Option Strict on, it does not like my use of 'i' due to Late Binding issues. Is there any alternative way to code this and avoid that issue?
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' Configuring for Background Workers
Control.CheckForIllegalCrossThreadCalls = False
Dim MyList As New List(Of String)
For a As Integer = 0 To 100
MyList.Add(CStr(a))
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim bgw As New System.ComponentModel.BackgroundWorker
bgw.WorkerReportsProgress = True
bgw.WorkerSupportsCancellation = True
AddHandler bgw.DoWork, AddressOf bgw_DoWork
' I create a BackgroundWorker here rather than add one in the toolbox so that I can specify the Handler and use different Handler routines for different part of a large program.
Button1.Enabled = False
Dim progress As New Progress(bgw)
progress.ShowDialog()
Button1.Enabled = True
End Sub
Private Sub bgw_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs)
For i = 0 To MyList.Count -1
Label1.Text = MyList(i)
sender.ReportProgress(i)
System.Threading.Thread.Sleep(200)
Label1.Refresh()
Next
End Sub
End Class
Public Class Progress
Private WithEvents _BGW As System.ComponentModel.BackgroundWorker
Public Sub New(ByVal BGW As System.ComponentModel.BackgroundWorker)
_BGW = BGW
InitializeComponent()
End Sub
Private Sub frmProgress_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
If Not IsNothing(_BGW) Then
_BGW.RunWorkerAsync()
End If
End Sub
Private Sub _BGW_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles _BGW.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
Label1.Text = e.ProgressPercentage
End Sub
Private Sub _BGW_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles _BGW.RunWorkerCompleted
Me.Close()
End Sub
End Class
CType(sender, BackgroundWorker).ReportProgress(i)
Also, if you want to do multiple actions with it, then create a local reference variable like this:
Private Sub bgw_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs)
Dim bgw As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
' ... now you can use "bgw" multiple times below instead of casting each time ...
For i = 0 To MyList.Count -1
Label1.Text = MyList(i)
bgw.ReportProgress(i)
bgw.SomethingElse()
bgw.MoreStuff()
System.Threading.Thread.Sleep(200)
Label1.Refresh()
Next
End Sub
Obviously this isn't necessary in your case, just an FYI...

How can I make Timer do different things based on how it was started

How can I make my Timer do different things depends on what activated it? I've tried using this code
Dim a As Integer = 0
Dim b As Integer = 0
Private Sub Button1_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
Timer1.Start
End Sub
Private Sub Button2_MouseHover(sender As Object, e As EventArgs) Handles Button2.MouseHover
Timer1.Start
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick, Button2.MouseHover, Button1.MouseHover
If sender Is Button1 Then
a = a + 1
TextBox1.Text = a
End If
If sender Is Button2 Then
b = b + 1
TextBox2.Text = b
End If
End Sub
but Textbox just add 1 once. This means that the Timer just act one time not continuously like Timer usually do. So is there anything I do wrong there, or i can do something different?.
Another possibly simpler approach, would be to use the Timer.Tag property and use one handler for both buttons:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Timer1.Tag = sender
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
a += 1
If Timer1.Tag Is Button2 Then
TextBox1.Text = a.ToString
End If
If Timer1.Tag Is Button1 Then
TextBox2.Text = a.ToString
End If
End Sub
Since Tag is already of type Object no external casting is needed.
I included the Start function, since I wasn't sure how you're initially starting the timer. If you're doing in a different manner it can be left out of the button event handler
I am not sure what is value of this and this is not, generally speaking, something people do, but if you want to know which action activated timer you need to register it
Private _timer As Timer
Private _activatingControl As Object
Private Sub ActivateTimer(c as Object)
_activatingControl = c ' this is first
_timer.Start()
End Sub
Private Sub Button2_MouseHover(sender As Object, e As EventArgs) Handles Button2.MouseHover
ActivateTimer(sender)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick, Button2.MouseHover, Button1.MouseHover
. . . . . .
System.Diagnostics.WriteLine("timer was activated by" + _activatingControl.ToString())
If DirectCast(_activatingControl, Control).Name = "Button1" Then
. . . .
ElseIf DirectCast(_activatingControl, Control).Name = "Button2" Then
. . . .
End If
End Sub
So, you will always need to activate timer via ActivateTimer. Timer1_Tick(sender As Object will always be Timer itself
I like tinstaafl's Tag property method. A similar approach would be to create a custom timer which inherits from System.Windows.Forms.Timer and has a Button property which can be set in the button's MouseHover event handler.
The timer's Start method only needs to be called once so I moved this to the form's load event handler. I am not sure exactly what you are wanting to achieve. Depending on this, the timer's Start method could be left where it was in the button MouseHover event handler and Timer1.Stop could be called at the end of the timer's tick event handler. This would then increment the value of the counter (a) only once in response to each MouseHover event. Alternatively Timer1.Stop could be called in the buttons' MouseLeave events if you only wanted the counter to increment while the mouse is hovering over the buttons.
Public Class Form1
Private Class CustomTimer
Inherits System.Windows.Forms.Timer
Private m_myButton As Button
Public Property Button() As Button
Get
Return m_myButton
End Get
Set(ByVal value As Button)
m_myButton = value
End Set
End Property
End Class
Private WithEvents Timer1 As New CustomTimer
Private a As Integer
Private Sub Form1_Load(sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Timer1.Interval = 100
Timer1.Start()
End Sub
Private Sub Button_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover, Button2.MouseHover
Timer1.Button = DirectCast(sender, Button)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
a += 1
If Timer1.Button Is Button1 Then
TextBox1.Text = a.ToString
ElseIf Timer1.Button Is Button2 Then
TextBox2.Text = a.ToString
End If
End Sub
End Class

Delete file after "x" second after creation

I want to delete file after 15 second of create that file.
I use this code but no success.
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
Dim test = Application.StartupPath & "\" + tte4
Timer1.Enabled = True
If Timer1.Interval = 0 Then
My.Computer.FileSystem.DeleteFile(test)
End If
End Sub
You must handle the timed event in a handler for the Timer's Tick Event (or Elapsed if using System.Timers.Timer):
Private m_strTest As String = String.Empty
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
m_strTest = Application.StartupPath & "\" + tte4
Timer1.Enabled = True
End Sub
If using System.Forms.Timer (most likely):
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
System.IO.File.Delete(m_strTest)
Timer1.Enabled = False
End Sub
If using System.Timers.Timer:
Private Sub Timer1_Elapsed(ByVal source As Object, ByVal e As ElapsedEventArgs) Handles Timer1_Elapsed
System.IO.File.Delete(m_strTest)
Timer1.Stop()
End Sub
If you added your Timer from the Toolbox, the above code will work (due to WithEvents). If you added it programatically, you'll also have to wire up the Event Handler and remove the Handles from your Timer1_Tick Method:
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
m_strTest = Application.StartupPath & "\" + tte4
AddHandler Timer1.Tick, AddressOf Timer1_Tick
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs)
System.IO.File.Delete(m_strTest)
Timer1.Enabled = False
End Sub
You need define a method that the timer fires when the interval elapses.
' Timer set to fire every 15 seconds
Dim Timer1 As New Timer(15000)
' OnTimedEvent is the method that will fire after 15 seconds
AddHandler Timer1.Elapsed, AddressOf OnTimedEvent
' Start the Timer
Timer1.Start()
The OnTimedEvent is as followed:
Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
' Stop the timer from firing again
Timer1.Stop()
' Delete your file
End Sub
Reference
https://msdn.microsoft.com/en-us/library/k0wdddfy(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
You have the right idea in using a Timer to wait for the deletion, but your way of trying to use it is incorrect. A (System.Windows.Forms) Timer raises an event when it ticks, and you need to tie that event to the code you want to run.
Also, to combine parts of a path it is better to use IO.Path.Combine, and when doing file operations it is a good idea to "wrap" the relevant code in a Try..Catch construct and handle the exception if it happens.
So:
Option Infer On
Option Strict On
Imports System.IO
Public Class Form1
Dim tim1 As New System.Windows.Forms.Timer
Dim tte4 As String = "afile.txt"
Private Sub Timer1_Tick(sender As Object, e As EventArgs)
tim1.Enabled = False
Dim target = Path.Combine("C:\temp", tte4)
If File.Exists(target) Then
Try
File.Delete(target)
Catch ex As Exception
MessageBox.Show(String.Format("The file ""{0}"" could not be deleted because {1}", target, ex.Message))
End Try
Else
' the file does not exist - do something if required
End If
End Sub
Private Sub SetUpTimer()
tim1.Enabled = False
tim1.Interval = 1000 * 15
' here we tie the event to the code
AddHandler tim1.Tick, AddressOf Timer1_Tick
End Sub
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles MyBase.Click
' this has the side-effect of trying to delete a file after an interval
tim1.Enabled = True
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SetUpTimer()
End Sub
End Class
You did not show code for creating the file - I assume you used a click on the form just to demonstrate the idea.