Background image flickering during form fade-in/out - vb.net

I use the code down below to create a fade-in/out effect when loading/unloading my form. It works OK, until I add a background image to my form. At that point, the fade is no longer smooth and the background image flickers during the fade.
Does anybody know how to fix that? Thanks for any help in advance!
Kind regards,
Eric
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
fade_in()
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
fade_out()
End Sub
'Fade in
Public Sub fade_in()
For FadeIn = 0.0 To 1.1 Step 0.02
Me.Opacity = FadeIn
Me.Refresh()
Threading.Thread.Sleep(20)
Next
End Sub
'Fade out:
Public Sub fade_out()
For FadeOut = 90 To 10 Step -2
Me.Opacity = FadeOut / 100
Me.Refresh()
Threading.Thread.Sleep(20)
Next
End Sub
End Class

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.

Controlling program-order when using a TabControl

Here is a simple VB.Net Forms program. The form contains a TabControl, which has 2 pages. On TabPage1 there is a Button, and a PictureBox, which contains a small 'Waiting' image. Initially the PictureBox is hidden. The TabControl starts by showing TabPage1.
What I would like to happen is that when the Button is pressed, the PictureBox is made visible, then, my SlowRoutine() is called, then the PictureBox is hidden, then I swap to TabPage2.
What actually happens is that when the Button is pressed, we wait 2 seconds, then we swap to TabPage2. I never see the PictureBox.
If I uncomment the MessageBox line, just to add a halt to the program-flow, then press the Button, the following occurs: 2 seconds passes, and then the PictureBox and the MessageBox appear. Clicking on the MessageBox closes it, and we go to TabPage2. Flipping back to TabPage1 shows that the PictureBox has been hidden.
The order of events is not happening in a logical way. How can I fix this, please?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
PictureBox1.Visible = False
PictureBox1.Hide()
PictureBox1.SendToBack()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Visible = True
PictureBox1.Show()
PictureBox1.BringToFront()
SlowRoutine()
' MessageBox.Show("I am waiting")
PictureBox1.Visible = False
PictureBox1.Hide()
PictureBox1.SendToBack()
TabControl1.SelectTab("TabPage2")
End Sub
Private Sub SlowRoutine()
' My SLOW ROUTINE: wait 2 seconds
Threading.Thread.Sleep(2000)
End Sub
End Class
Thanks to all. Here is the working code based on those comments, in case anyone else needs to do a similar task:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
PictureBox1.Visible = False
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Visible = True
Await SlowRoutineAsync()
PictureBox1.Visible = False
TabControl1.SelectTab("TabPage2")
End Sub
Private Async Function SlowRoutineAsync() As Task
' My SLOW ROUTINE: wait 2 seconds
Await Task.Delay(2000)
End Function
End Class

Print Progress bar value to textbox

I'm trying to print a progressbar's percentage to a textbox. When ever I run my program, nothing appears in the textbox. This is my code:
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
ProgressBar1.Maximum = TextBox1.Text
End Sub
Help is very appreciated! Thank you.
Here is some code that i have written up for you to have a look at, it should lead you in the right direction and help you on your way :)
Imports System.ComponentModel
Public Class Form1
''This will display the information to the textbox and will also load a progressbar(you can change it to something else beside a textbox too eg label, windows form title and so on).
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
TextBox1.Text = e.ProgressPercentage & "%"
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
''This is make the backgroundworker start at load up(change it to a button if need be
CheckForIllegalCrossThreadCalls = False
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
''This is the example that i created to show you how to set a task.
For i = 0 To 10000
TextBox1.Text = i
BackgroundWorker1.ReportProgress(i)
System.Threading.Thread.Sleep(500)
Next
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
''once the task is complete it will show a messagebox and reset the progressbars value to 0 so its not full when the task is compelete.
MessageBox.Show("Completed")
ProgressBar1.Value = 0
End Sub
End Class
Let me know how you go, i live in a country where i cant access the website link that you posted.Happy Coding
UPDATE: do check out the backgroundworkers on google, there are a lot of tutorials to help you :)

How to fill a progress bar by clicking in VB.NET?

I'm making a game in VB.Net and I'm not familiar with the progress bar. I need something where the player need to press a button as fast as they can to fill up the progress bar and proceed to the next leve or if not fast enough then lose.I have no code for this because I don't know how to build up something like this. Any help would be grate.
Thanks
Say you have a button, Button1, and you have a progressbar, ProgressBar1.
You can add to the progressbar's value everytime you click on Button1 by using the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ProgressBar1.Value + 1 < ProgressBar1.Maximum Then
ProgressBar1.Value += 1
End If
End Sub
Now, notice the condition I wrapped the increment code with. This will make sure that the user does not surpass the maximum value allowed in the progressbar1.
Edit:
As for the rest of your program, you will need to use a timer in order to track the time.
for the proceed button, you will need to use the visible property which exists on buttons in order to hide a button until some condition is met.
Re-Edit:
Public Class Form1
Private intCurrentTime As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ProgressBar1.Value + 1 < ProgressBar1.Maximum Then
ProgressBar1.Value += 1
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If intCurrentTime = 10 Then
If ProgressBar1.Value = ProgressBar1.Maximum Then
'SHOW "Proceed" BUTTON
Else
MsgBox("You have failed!")
End If
intCurrentTime = 0
Else
intCurrentTime += 1
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
End Class

How to generate multiple picture boxes and control them using a timer?

My uni asked us to make a game using VB, and I really don't know much about the language.
I'm trying to make a game where balloons go up to them top of the screen and must be popped before getting there.
Public Class Form1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If PictureBox1.Top = 0 Then
PictureBox1.Visible = False
Timer1.Enabled = False
End If
PictureBox1.Top = PictureBox1.Top - 1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 1
Timer1.Enabled = True
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
PictureBox1.Visible = False
End Sub
End Class
This is my code so far, when I click the button, the balloon starts to go up, if I click the balloon, it disappears, it also disappears if it reaches the top and the timer stops.
How can I generate more balloons and control them using that timer?
Now all you have let to do is add the functionality of adding more PictureBoxes, maybe a second timer and when you create them use an Addhandler statement to point them the the pbs_Click event that I made and add them to the List I made as well.
Public Class Form1
Private PBs As New List(Of PictureBox)
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For Each pb As PictureBox In PBs
If pb.Top = 0 Then
pb.Visible = False
Timer1.Enabled = False
Else
pb.Top = pb.Top - 1
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 1000'ms
Timer1.Enabled = True
End Sub
Private Sub pbs_Click(sender As Object, e As EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
PBs.Remove(pb)
End Sub
Private Sub makeNewPB()
Dim pb As New PictureBox
Addhandler pb.Click, AddressOf pbs_Click
'don't forget to make them the size you need
PBs.Add(pb)
End Sub
End Class