Message Box showing multiple times when progress bar complete in VB.NET - 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()

Related

I need some correction VB.net Code Framework 2.0 making a register system

In my program, I want to to check if my app is registered when my progress bar is finished. If it is Registered (checked by the text of a label in form 1) the second textbox should appear and if is not registered the first text box should appear
The problem is that I must add an end if when I don't want to because it will continuously pop up the textbox.
This is my code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Value += 1
If ProgressBar1.Value =
ProgressBar1.Maximum Then
Timer1.Stop()
ProgressBar1.Value = ProgressBar1.Minimum
If Form1.Label4.Text = "Unregistered" Then
MsgBox("Exampletext", MsgBoxStyle.Information)
Me.Hide()
Else
MsgBox("Exampletext1", MsgBoxStyle.Information)
Hide()
End If
End Sub
End Class
BTW: I didn't wanted to make it in framework 2.0 but I made it, to make it more compatible with older Windows.
This is why correct indentation is important...
The compiler thinks that the Else is related to the first If.
And actually, nothing will ever happen when your second If is correct because there is no End If. Therefore it is assuming that the instruction is on the same line.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Value += 1
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Stop()
ProgressBar1.Value = ProgressBar1.Minimum
If Form1.Label4.Text = "Unregistered" Then
MsgBox("Exampletext", MsgBoxStyle.Information)
Me.Hide()
Else
MsgBox("Exampletext1", MsgBoxStyle.Information)
Hide()
End If
End If 'You need to add the End If here
End Sub
I am however voting to close this question as it is a typo error...

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

Refresh ListView on Timer_Tick

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

VB.NET browser click event handler: value of '0' is not valid for 'index' error

I have tried to make a small program using VB.NET.
When it opens, it shows a webpage and a process bar underneath the webpage. When the user clicks the link in the webpage, the progess bar stops processing and show a You clicked a link message. When processing is complete it shows another message which says: Thanks for helping me.
I wrote the code for the total process in VB.NET, but when I debug it it shows a message:
Value of '0' is not valid for 'index'. 'index' should be between 0 and -1.
Parameter name: index
My code is:
Public Class MyPage
Private Sub MyPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 1500
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
WebBrowser1.Document.Links(0).AttachEventHandler("onclick", AddressOf LinkClick)
End Sub
Sub LinkClick(ByVal sender As Object, ByVal e As System.EventArgs)
Timer1.Start()
MsgBox("You clicked the link", , "Clicked The link")
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(2)
If ProgressBar1.Value = 100 Then
Timer1.Stop()
ProgressBar1.Value = 0
MsgBox("Thanks for help me", , "Thankssss!")
End If
End Sub
End Class
How can I solve this issue?
You blindly assume that the page will always have a link. Wrong assumption. Fix:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If WebBrowser1.Document.Links.Count > 0 Then
WebBrowser1.Document.Links(0).AttachEventHandler("onclick", AddressOf LinkClick)
End If
End Sub
Or given the usage, the more sane:
For Each link As HtmlElement In WebBrowser1.Document.Links
link.AttachEventHandler("onclick", AddressOf LinkClick)
Next

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.