Close form after set time - vb.net

The code below allows me to fade in and out when it opens and closes, which is what I want. However, I would like my form to remain open for 10 seconds before the fading starts. I am struggling to get that part done.
Here is what I have so far:
Public Class frmDefinitions
Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
tmr_out.Enabled = True
End Sub
Private Sub frmDefinitions_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
Me.Opacity = 100
tmr_in.Enabled = True
End Sub
Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tmr_in.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
tmr_in.Enabled = False
End If
End Sub
Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tmr_out.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
tmr_out.Enabled = False
Me.Close()
End If
End Sub
End Class

You will need to setup a third Timer to delay the start of your tmr_out Timer. I would trigger the delay as soon as your tmr_in is disabled. You should then get your 10 second delay before you start your fade out. You could also try to use the Form's Shown event to start the Delay but you would need to adjust the 10 seconds to accommodate the fade in delay.
Public Class Form1
Dim tmrDelay As New Timer()
Public Sub New()
InitializeComponent()
tmrDelay.Interval = 10000
AddHandler tmrDelay.Tick, AddressOf tmrDelay_Tick
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.Opacity = 0
tmr_in.Enabled = True
End Sub
Private Sub tmrDelay_Tick(sender As System.Object, e As System.EventArgs)
tmrDelay.Stop()
tmr_out.Start()
End Sub
Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_in.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
tmr_in.Enabled = False
tmrDelay.Start() 'Start Your 10 second delay here.
End If
End Sub
Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_out.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
tmr_out.Enabled = False
Me.Close()
End If
End Sub
End Class

Related

why after splashscreen runs a bit slow to form1 in vb.net

is there a solution so that after the splashscreen so can go directly to form1? And is there something wrong with my code and is it because I created a splachscreen from a Windows Form instead of from a custom splashscreen?
Thanks
Here's [a link] video (https://drive.google.com/file/d/1URn_DDTnu3h1oERhwFGPCa9-9Se1H_g9/view?usp=share_link)!
Imports WinFormAnimation
Public Class FormWelcome
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
CircularProgressBar1.Value += 1
CircularProgressBar1.Text = CircularProgressBar1.Value.ToString
If Me.Opacity < 1 Then
Me.Opacity += 0.05
End If
If CircularProgressBar1.Value = 100 Then
Timer1.Stop()
Timer2.Start()
End If
End Sub
Private Sub FormWelcome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CircularProgressBar1.Value = 0
Me.Opacity = 0
Timer1.Start()
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Me.Opacity -= 0.1
If Me.Opacity = 0 Then
Timer2.Stop()
Form1.Show()
Me.Close()
End If
End Sub
End Class

Background image moving vb.net

I'm trying to create a small game, but I'm getting trouble.
I would like the background image to move as a loop when the player is moving across the map. The following code is a sample of what I want but the reverse.
Public Class Form1
Private WithEvents Tmr As New Timer With {.Interval = 40}
Private water As New Bitmap("C:\TestFolder\Water.png")
Private waterposition As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Size = New Size(240, 74)
Tmr.Start()
End Sub
Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
waterposition += 1
If waterposition = water.Width Then waterposition = 0
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawImage(water, waterposition, 0)
If waterposition > 0 Then e.Graphics.DrawImage(water, 0 - (water.Width - waterposition), 0)
End Sub End class
And now, when I'm trying to reverse it (from right to left) The image won't redraw.
Private Sub Tmr_Tick(sender As Object, e As EventArgs) Handles Tmr.Tick
waterposition -= 1
If waterposition = 0 Then waterposition = water.Width
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawImage(water, waterposition, 0)
If waterposition < 0 Then e.Graphics.DrawImage(water, (water.Width + waterposition), 0)
End Sub
Any suggestion? Thanks

Do Process while Progress Bar is Running

I know that knowledge is expensive, but is there who want to help me
i want to run process when progress bar is running,
i try with this code
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call Prcss()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Progress.Value < 100 Then
Progress.Value += 2
ElseIf Progress.Value = 100 Then
Timer1.Stop()
Form1.Show()
Me.Hide()
End If
End Sub
Private Sub Prcss()
With Progress
.Value = 0
Threading.Thread.Sleep(450)
Label1.Text = "Renewing Custom Content"
.Value = 20
Threading.Thread.Sleep(450)
Label1.Text = "Getting Information"
.Value = 50
Threading.Thread.Sleep(450)
Label1.Text = "Downloading Udpdate"
.Value = 70
Threading.Thread.Sleep(450)
Label1.Text = "Ready to Start"
.Value = 100
End With
End Sub
i don't know where is my mistake, i read this on my book.
Try using a BackgroundWorker or a Thread:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i As Integer = 1 To 1000
BackgroundWorker1.ReportProgress(CInt(i / 10))
Threading.Thread.Sleep(500)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MessageBox.Show("Finished!")
End Sub
This is the basic example and require you to add a BackgroundWorker to the form. You can see help here: https://msdn.microsoft.com/es-es/library/cc221403(v=vs.95).aspx

VB.net real-time time elapsed feature

I already have created a real time clock that synchronizes with the computer time and is being displayed in a label.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
I want to make a real-time time elapsed feature that keeps on counting the seconds/minutes/hours elapsed from the time it started till the time it stops and it would be basing on the real-time clock i have created. I would be creating a start and stop button for this. Is this possible? Thanks in advance.
I am now able to complete everything and i added a feature that records the starting and ending time based on my real time clock. Here is my working code:
Dim hr, min, sec As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Start.Text = ""
EndLbl.Text = ""
Elapse.Text = ""
Timer2.Enabled = True
Start.Text = Time.Text
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
sec = sec + 1
If (sec = 60) Then
sec = 0
min = min + 1
ElseIf (min = 60) Then
min = 0
hr = hr + 1
ElseIf (hr = 24) Then
hr = 0
min = 0
sec = 0
End If
Elapse.Text = String.Format("{0}hr : {1}min : {2}sec", hr, min, sec)
Timer2.Interval = 1000
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Timer2.Enabled = False
EndLbl.Text = Label4.Text
hr = 0
min = 0
sec = 0
Timer2.Interval = 1
End Sub
Credits to the starting code given by NeverHopeless. Thanks alot.
I suggest you use only 1 timer:
Public Class Form2
Private _elapseTimerRunning As Boolean = False
Private _elapseStartTime As DateTime
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
txtTime.Text = Now.ToString("h:mm:ss tt")
If _elapseTimerRunning = True Then
Dim elapsedtime = DateTime.Now.Subtract(_elapseStartTime)
txtElapsed.Text = String.Format("{0}hr : {1}min : {2}sec", elapsedtime.Hours, elapsedtime.Minutes, elapsedtime.Seconds)
End If
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
_elapseStartTime = DateTime.Now
_elapseTimerRunning = True
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
_elapseTimerRunning = False
End Sub
End Class
An example for displaying the elapsed time the application has run.
Public Class Form1
'shows elapsed time that the app has run
'
Dim elapTimer As New Threading.Timer(AddressOf tick, Nothing, 1000, 1000)
Dim stpw As Stopwatch = Stopwatch.StartNew
Private Sub tick(state As Object)
If stpw.IsRunning Then
'format - http://msdn.microsoft.com/en-us/library/ee372287.aspx
Me.Invoke(Sub()
Label1.Text = stpw.Elapsed.ToString("d\ \ hh\:mm\:ss\.ff")
End Sub)
End If
End Sub
End Class
To add start/stop functionality using buttons:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'start the elapsed timer
stpw.Start() 'continue
'or
'stpw.Restart() 'restart
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
stpw.Stop()
End Sub
Something like this would help you: (untested, but will give you a starter)
Dim hr, min, sec As Integer 'msec;
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer2.Enabled = True
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'msec++;
'if(msec == 60) { msec = 0; sec++; }
sec+=1;
if(sec = 60) Then
sec = 0 : min+=1
end if
if(min = 60) Then
min = 0 : hr+=1
end if
if(hr = 24) Then
hr = 0 : min = 0 : sec = 0
end if
'TimeElapsed.Text = String.Format("{0}:{1}:{2} {3}", hr, min, sec, msec)
TimeElapsed.Text = String.Format("{0}:{1}:{2}", hr, min, sec)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
NOTE: Timer2 will run for every second.

VB Detect Idle time

I'm looking for a way to detect if the user has been idle for 5 min then do something, and if and when he comes back that thing will stop, for example a timer.
This is what i have tried (but this will only detect if form1 has been inactive / not clicked or anything):
Public Class Form1
Private Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'You should have already set the interval in the designer...
Timer1.Start()
End Sub
Private Sub form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MsgBox("Been idle for to long") 'I just have the program exiting, though you could have it do whatever you want.
End Sub
End Class
This is done easiest by implementing the IMessageFilter interface in your main form. It lets you sniff at input messages before they are dispatched. Restart a timer when you see the user operating the mouse or keyboard.
Drop a timer on the main form and set the Interval property to the timeout. Start with 2000 so you can see it work. Then make the code in your main form look like this:
Public Class Form1
Implements IMessageFilter
Public Sub New()
InitializeComponent()
Application.AddMessageFilter(Me)
Timer1.Enabled = True
End Sub
Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
'' Retrigger timer on keyboard and mouse messages
If (m.Msg >= &H100 And m.Msg <= &H109) Or (m.Msg >= &H200 And m.Msg <= &H20E) Then
Timer1.Stop()
Timer1.Start()
End If
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
MessageBox.Show("Time is up!")
End Sub
End Class
You may have to add code that disables the timer temporarily if you display any modal dialogs that are not implemented in .NET code.
This might work by setting it to just call the Reset idk i just want it work all over the program idk how to do it, i just created this code :
Public Class test
Dim IdleTimer As String
Dim testsave As String
Dim idle_TimerSet As String = 60 '<---- Here You choose The timer (1 per Sec)
Private Sub test_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
IdleTimer = idle_TimerSet
End Sub
Private Sub Idle_Tick(sender As Object, e As EventArgs) Handles Idle.Tick
If IdleTimer <= 1 Then
MsgBox("[ Idle Screen ]")
Else
IdleTimer = IdleTimer - 1 '<--- The Counter
IdleTracker.Text = IdleTimer
End If
End Sub
Private Sub test_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
Call Reset_Idle() 'This is on the main Form
End Sub
Private Sub test_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Call Reset_Idle() 'This is on the main Form
End Sub
Public Sub Reset_Idle() '<-- The Reset Action
'Idle.Enabled = False
IdleTimer = idle_TimerSet
'Idle.Enabled = True
End Sub
End Class