System.Timers.Timer -- new to programming, need help

W

WhiteSocksGuy

Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a
System.Timers.Timer to work for me to display a splash screen for about two
seconds and then load the main form. I have two forms (frmSplash and
frmMain) and a code Module setup as my startup object.

Here is the code that I have, when I try to run this part of the splash
screen form shows and then program terminates. What am I doing wrong?

Thanks,

Kevin

*************************************************************

Module Module1

' Declare the two forms
Public myMain As New frmMain()
Public MySplash As New frmSplash()

Public Class StartUp

Public Shared Sub Main()
' Show the splash screen
MySplash.Show()
' Create a new Timer with Interval set to 2 seconds.
Dim aTimer As New System.Timers.Timer(2000)
' Add an event handler to handle the Elapsed time event
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Only raise the event the first time Interval elapses.
aTimer.AutoReset = False
' Enable the timer
aTimer.Enabled = True
End Sub

Public Shared Sub OnTimedEvent(ByVal source As Object, _
ByVal e
As System.Timers.ElapsedEventArgs)
' Show the main form and hide the splash screen
myMain.Show()
MySplash.Hide()
End Sub

End Class

End Module
 
K

Ken Tucker [MVP]

Hi,

In your sub main you show the splash screen and set up a timer.
Once it is done doing that the program ends because there is nothing else
for it to do. Place the timer in the splash screen and have it close it
self after 2 seconds. Try something like this.

Public Shared Sub Main()
' Show the splash screen
MySplash.TopMost = True
MySplash.Show()
Application.Run(frmMain)
End Sub


Ken
 
C

Cor

Hi Kevin,

I prefer for a splash screen the windows.forms.timer it is for this easier
to handle.

Not that you should use it, but I have beneath a sample from a splash screen
inserted, it has more in it than absolute is necessary but it works nice.
(While it shows it does not stop your mainform program)

I hope this helps,

Cor


\\\form1
Private frm As New Form2
Private WithEvents timer1 As New Windows.Forms.Timer
Private Sub Form1_Load(ByVal sender _
As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
timer1.Enabled = True
timer1.Interval = 12500
frm.Show()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles timer1.Tick
timer1.Enabled = False
frm.Close()
frm.Dispose()
End Sub

///
\\\form2

Private WithEvents timer1 As New Windows.Forms.Timer
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = "High I Am Here"
Me.ForeColor = Color.White
timer1.Enabled = True
timer1.Interval = 125
Me.BackColor = Color.CornflowerBlue
Me.Opacity = 1
Me.TopMost = True
Me.Text = "Splash"
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles timer1.Tick
Me.Opacity -= 0.01
If Me.Opacity = 0 Then
timer1.Enabled = False
Me.Close()
End If
End Sub
///
 
H

Herfried K. Wagner [MVP]

* "Cor said:
I prefer for a splash screen the windows.forms.timer it is for this easier
to handle.
ACK.

Not that you should use it, but I have beneath a sample from a splash screen
inserted, it has more in it than absolute is necessary but it works nice.
(While it shows it does not stop your mainform program)

I would place the timer used for closing the splash screen on the
splash screen in order to avoid tight coupling.
 
C

Cor

I would place the timer used for closing the splash screen on the
splash screen in order to avoid tight coupling.

It did not close that way, maybe I should overlook it again but I think
there is still a reference.
There was someother one who had the same problem and than I thought why not
fix it with just an extra timer, (there are now on both forms a timer, but
maybe you have an idea?)

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
It did not close that way, maybe I should overlook it again but I think
there is still a reference.

A reference to what? I am not really sure what you are referring to.
There was someother one who had the same problem and than I thought why not
fix it with just an extra timer,

Can you describe the problem in a few words or post a link?
 
C

Cor

Hi Kevin,

Correction, as me pointed on the timer on form1 is not needed.
(This was originaly not, so maybe I see the reason in future now, I deleted
it again in my sample).

Cor

\\\form1
Private frm As New Form2
Private Sub Form1_Load(ByVal sender _
As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
frm.Show()
End Sub
///
\\\form2

Private WithEvents timer1 As New Windows.Forms.Timer
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = "High I Am Here"
Me.ForeColor = Color.White
timer1.Enabled = True
timer1.Interval = 125
Me.BackColor = Color.CornflowerBlue
Me.Opacity = 1
Me.TopMost = True
Me.Text = "Splash"
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles timer1.Tick
Me.Opacity -= 0.01
If Me.Opacity = 0 Then
timer1.Enabled = False
Me.Close()
End If
End Sub
///
 
C

Cor

Herfried,

I tested it in many ways, I cannot get the reason back why I added it, so it
should been something totally not important.

Thanks for pointing me on that.

Cor
 
W

WhiteSocksGuy

Thanks for all your help, after much toying around with it I have finally
got the splash screen to operate how I want it to, and this is how I done it
for those who are also learning:

I have a splash screen, frmSplash and a main form, frmMain: (frmMain is the
startup object)
A timer component is on each form, interval set to 2.5 seconds for the
splash screen and 5 seconds for the main screen.

***************************************

code from Main Program window (frmMain):

Public Class frmMain
Private Sub frmMain_Load(ByVal sender As System.Object, _
ByVal e As
System.EventArgs) _
Handles MyBase.Load
MyBase.Hide()
Dim mySplash As New frmSplash()
mySplash.ShowDialog()
End Sub

' timer interval set to 5 seconds
' after 5 seconds show the main form
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
MyBase.Show()
End Sub
End Class

code from Splash Screen (frmSplash):

Public Class frmSplash
' timer interval set to 2.5 seconds
' after 2.5 seconds the splash screen closes itself
Private Sub Timer2_Elapsed(ByVal sender As System.Object, _
ByVal e As
System.Timers.ElapsedEventArgs) _
Handles
Timer2.Elapsed
Me.Close()
End Sub
End Class

***************************************

Thanks again,

Kevin
 
B

Brian Henry

you can cause the thread its loading on to sleep also instead of a timer,
its a lot easier just put Thread.Sleep(200) in your app where you want it to
hold at
 
H

Herfried K. Wagner [MVP]

* "Cor said:
I tested it in many ways, I cannot get the reason back why I added it, so it
should been something totally not important.

Thanks for playing around with that and sharing your experiences with
the group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top