Best Approach vs Any.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I hate to be vague in my title, but to be honest I have started too many
splash screen threads lately. I am still looking for an answer.

Before I go on, I have to mention that I need a VB.Net solution. C# won't
work for me, I have no java, C++, or C# experience at all.

My program takes a while ot load and the user has no way of knowing that
anything at all is happening. I would like to make a splash screen. I have
attempted to add a class and use a custom Sub Main() but I admit my knowledge
of this is pretty limited. I have used it in VB6, but in VB6 I was able to
use Load and Show pretty easily.

Can anyone point me to an example of a two form splash screen example. To
make it even harder, it would be ideal if the example had threading example
so I can learn how to load things in the backround during down time.

I recently bought 2 books on the compact framework that came highly
recommended, but to be honest I was disappointed in how little information
they held in 800-1000 pages.
 
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' assuming you have a second form of "SplashForm" class
Dim Dialog As SplashForm = New SplashForm
Dialog.Show()
Application.DoEvents()

' do all of your main form processing stuff here

Dialog.Close()

End Sub
 
Actually I've done this in C#. I'll convert it to VB for this thread.

Some advice though: Both Microsoft and the .NET community are generally
going to be more proficient in C#, especially for advanced functionality.

Ideally, you're going to want to create a new form and show it in Main sub,
perform your application-wide options, and then start your main form on
Application.Run (a.k.a. The UI thread). Unfortunately, there's a bug in .NET
that minimizes all forms if a form is loaded before the UI thread is
finished constructing it's primary form.

I've created an example that builds in VB.NET. Make sure you've installed
SP1, though. Keep in mind you have to manually install SP1 on the emulator,
too.

The following example has 4 Classes: Form1.vb, Form2.vb, EntryPoint.vb, and
Splash.vb. In short, Form2.vb has your final Splash form Solution. Form1.vb
and EntryPoint.vb contain the Ideal code, because they better conform to OOP
standards: Application initialization is contained in the Entry Point. Form2
embeds the application initialization within its own code. Since application
initialization does not pertain directly Form2.vb, it's considered an OOP
standard violation. It's the only way around the bug, however, so both
examples are included. Hopefully this bug will be addressed in .NET CF 2.0,
or someone will discover a better workaround.

I'll post an article about this on code-project with more detailed
information soon.

On a side note: Spawning multiple threads in Windows Forms should be avoided
for "Smart Devices" as much as possible. Use Application.DoEvents whenever
possible. Two reasons: In order to alter a Windows Forms Control from a
background thread, you have to invoke control on the UI thread. Thread
invokation is pretty limited in CF 1.0. Secondly, low-power processors don't
a lot of resources dedicated to context-switching, so it's going to be much
easier to thrash your processor by spawning multiple threads.

Let me know if you have any more questions.


--------FORM2.VB-------

Public Class Form2
Inherits System.Windows.Forms.Form

Private p_Splash As Splash

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Text = "Form2"
End Sub

#End Region
#Region "Overrides"
Protected Overloads Overrides Sub OnClosing(ByVal e As
System.ComponentModel.CancelEventArgs)
MyBase.OnClosing(e)
End Sub

Protected Overloads Overrides Sub OnLoad(ByVal e As System.EventArgs)
p_Splash = New Splash
p_Splash.Show()

p_Splash.Status = "Loading..."
System.Threading.Thread.CurrentThread.Sleep(1000)

p_Splash.Status = "Connecting..."
System.Threading.Thread.CurrentThread.Sleep(1000)

p_Splash.Status = "Cleaning up..."
System.Threading.Thread.CurrentThread.Sleep(1000)

p_Splash.Hide()
p_Splash.Dispose()

Focus()
Show()

MyBase.OnLoad(e)
End Sub
#End Region


End Class


---------SPLASH.VB-------

Public Class Splash
Inherits System.Windows.Forms.Form
Friend WithEvents lblStatus As System.Windows.Forms.Label

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.lblStatus = New System.Windows.Forms.Label
'
'lblStatus
'
Me.lblStatus.Location = New System.Drawing.Point(8, 104)
Me.lblStatus.Size = New System.Drawing.Size(224, 20)
Me.lblStatus.TextAlign = System.Drawing.ContentAlignment.TopCenter
'
'Splash
'
Me.Controls.Add(Me.lblStatus)
Me.Text = "Splash"

End Sub

#End Region
#Region " Public Properties "
Public Property Status() As String
Get
Return lblStatus.Text
End Get
Set(ByVal Value As String)
If (Value = Nothing) Then
Throw New ArgumentNullException("Value")
End If

If (Value = String.Empty) Then
Throw New ArgumentException("Value cannot be empty")
End If

lblStatus.Text = Value
Application.DoEvents()
End Set
End Property


#End Region
End Class



--------ENTRYPOINT.VB-----

Public Class EntryPoint
Public Shared Sub Main()
Dim s As Splash
s = New Splash

s.Show()
s.Status = "Loading something..."
System.Threading.Thread.CurrentThread.Sleep(1000)

s.Status = "Connecting to Something..."
System.Threading.Thread.CurrentThread.Sleep(1000)

s.Status = "Cleaning it up..."
System.Threading.Thread.CurrentThread.Sleep(1000)

Application.Run(New Form1)

s.Close()
s.Dispose() 'Very important to call this! Application.Run usually
handles this.
End Sub
End Class


--------FORM1.VB--------

Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()
Focus()
Show()
'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 24)
Me.Label1.Text = "Data Entry"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(16, 64)
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Font = New System.Drawing.Font("Tahoma", 9.0!,
System.Drawing.FontStyle.Regular)
Me.TextBox2.Location = New System.Drawing.Point(16, 96)
Me.TextBox2.Text = "TextBox2"
'
'TextBox3
'
Me.TextBox3.Font = New System.Drawing.Font("Tahoma", 9.0!,
System.Drawing.FontStyle.Regular)
Me.TextBox3.Location = New System.Drawing.Point(16, 128)
Me.TextBox3.Text = "TextBox3"
'
'Form1
'
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Menu = Me.MainMenu1
Me.Text = "Form1"

End Sub

#End Region

End Class
 
I posted a solution, but maybe it was caught by the MS Anti-spammer. If ti
doesn't show up, shoot me an email at (e-mail address removed).

I'll post an artice on code-project, too.

--ROBERT
 
Back
Top