DevExpress CheckButton not toggeling? - vb.net

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

Related

How can I Cancel Multiple Background Workers in a For loop?

Creating a Windows app in Visual Studio 15.
Have a form where I can start up to 10 background worker tasks (BG1 to BG10). I currently cancel each BGWorker individually with:
Private Sub DSx10C_Click(sender As Object, e As EventArgs) Handles DSx10C.Click
BackgroundWorker10.CancelAsync()
BackgroundWorker10.Dispose()
Label10.Text = "Cancelled"
End Sub
I need to do a Cancel ALL option.
How can I loop through all 10 BGWorkers in a For loop?
Hope my question is a better MCVE.
How to cancel all the BackgroundWorkers,
You can do it like this:
Declare an array of BackgroundWorkers
Dim MyBackgroundWorkers() As BackgroundWorker
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' add all your BackgroundWorkers to the array
MyBackgroundWorkers = New BackgroundWorker() {BackgroundWorker1, BackgroundWorker2, BackgroundWorker3, BackgroundWorker4, BackgroundWorker5}
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'' now you can Cancel all of them in a loop
For Each bgw In MyBackgroundWorkers
If bgw.IsBusy Then
bgw.CancelAsync()
End If
Next
End Sub
This is assuming that your BackgroundWorkers are controls you added from the toolbox at design time. If it was declared and added from code, you would need to make appropriate modifications in the Form_Load code.
Try This. Hope This helps.

Visual Basic 2008 Check if label is inside a panel

So i've been trying to make a game with arrow keys.
The label is the character, if the label is inside the object, it will do something...
But i got a very hard problem i cannot find on the internet.
How do i check if a label is inside of an object? Example: Picture box, and Panel.
I've tried this one.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Location.X = Panel1.Location.X And Label1.Location.Y = Panel1.Location.Y Then
Me.Close() 'Any code.
End If
End Sub
Doesn't work,
any help would be appreciated.
I am a beginner by the way, i only make simple applications. Like escape the room, maze... etc.
I would use the label's parent property to see if it is inside the panel.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Parent IsNot Nothing AndAlso Label1.Parent Is Panel1 Then
Me.Close() 'Any code.
End If
End Sub
If you are looking to see if the Label is over the Panel I would try something like this. The ClientRectangle property is the rectangle the control takes up. I am assuming the panel is bigger than the Label.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Panel1.ClientRectangle.Contains(Label1.ClientRectangle) Then
Me.Close() 'Any code.
End If
End Sub
If it is smaller you can check if a point is in the Panel's rectangle. For example top left corner
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Panel1.ClientRectangle.Contains(new Point(Label1.Left, Label1.Top)) Then
Me.Close() 'Any code.
End If
End Sub

How does the ProgressBar.PerformStep() function work?

I'm quite confused about progress bar in VB.net, here is my code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ProgressBar_my.Minimum = 0
ProgressBar_my.Maximum = 10
ProgressBar_my.Step = 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ProgressBar_my.PerformStep()
Threading.Thread.Sleep(5000)
If 1 = 1 Then
ProgressBar_my.PerformStep()
ProgressBar_my.PerformStep()
End If
'Threading.Thread.Sleep(2000)
End Sub
For the above code, what I expected is that after I click Button1, the progress bar will increase the progress status by 1, then it will pause for 5 sec, then it will increase the progress status by 2 at once.
However, after I ran the above code, what I saw was that after I click Button1, the progress bar will increase by 3 continually after 5 sec.
Can someone tell me why it behaves like this and How should I program my code so that I can increase by 1, then pause 5 sec and then increase by 2?
Thanks in advance!
I think what you are seeing (or not seeing) is the fact that the progress bar takes a finite amount of time to advance each step.
When you call Threading.Thread.Sleep on the UI thread this stops the progress bar from being redrawn until after the Sleep
What you should do is update the progress bar on a background worker instead, then I think you will see the effect you desire.
Add a BackgroundWorker to your form
Change your button click code to start the worker:
Private Sub frmSO_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ProgressBar_my.Minimum = 0
ProgressBar_my.Maximum = 10
ProgressBar_my.Step = 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync
End Sub
Then perform the update in the DoWork event:
'define a delegate to handle updates to the UI thread
Private Delegate Sub PerformStepCallback()
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim cb As PerformStepCallback = AddressOf ProgressBar_my.PerformStep
Me.BeginInvoke(cb)
Threading.Thread.Sleep(5000)
Me.BeginInvoke(cb)
Me.BeginInvoke(cb)
End Sub
The problem is that you call Threading.Thread.Sleep(5000), which will, well, pause the current thread. But that also means that the window won't be redrawn, so you see the effect of the first call to PerformStep only after the thread is no longer paused.
You can use another thread for wait and the second update; the easiest way in this case is using the Task Parallel Library:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ProgressBar1.PerformStep()
Task.Delay(5000).ContinueWith(Sub()
ProgressBar1.PerformStep()
ProgressBar1.PerformStep()
End Sub, TaskScheduler.FromCurrentSynchronizationContext)
End Sub
Thanks Matt Wilko for giving me the clue. Here is my code.
From the Components tab of the Toolbox, add a BackgroundWorker component named backgroundWorker2. Create DoWork and ProgressChanged event handlers for the BackgroundWorker.
The following code will initiate the Component.
Public Sub New()
InitializeComponent()
BackgroundWorker2.WorkerReportsProgress = True
BackgroundWorker2.WorkerSupportsCancellation = True
End Sub
Call RunWorkerAsync when the button is clicked and the background worker is not running.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If BackgroundWorker2.IsBusy <> True And ProgressBar_my.Value < 6 Then
BackgroundWorker2.RunWorkerAsync()
End If
End Sub
Private Sub BackgroundWorker2_DoWork_1(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles BackgroundWorker2.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
worker.ReportProgress(1)
System.Threading.Thread.Sleep(2000)
worker.ReportProgress(1)
worker.ReportProgress(1)
End Sub
The following event is raised when every time the ReportProgress method is called.
Private Sub backgroundWorker2_ProgressChanged(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
Handles BackgroundWorker2.ProgressChanged
ProgressBar_my.PerformStep()
End Sub
More information about the BackgroundWorker:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

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

VB restart form

I created a button and I need it to restart the form
I have no idea how to work on the visual basic what should i write
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End Sub
Do you mean Application.Restart() ?
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Application.Restart()
End Sub
Although in Visual Basic this does the same as in C#, it restarts the application not just clears the form.
In order to reload the form you simply need to do this in the form's Load event.
Me.Close()
Me.Show()
Try This..
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Application.Restart()
End Sub