Problem with Dialog Boxes and Notify Icons

D

Derek Martin

Hi there, this is probably really dumb, but I am using a dialog as my main
form and my startup form is my splash screen - which implements some
progress bars to load up some relevant data. When that is done, loading
form does this:

Me.Visible = False
mainform.ShowDialog()
Application.Exit()

So far so good (is this a dumb way of doing that???). Now, I am
implementing a notification icon in mainform. Here is some relevant stuff:

(class variable shown as boolean = true and notify icon set up correctly)

Private Sub Open_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles NotifyIcon1.DoubleClick
Me.WindowState = FormWindowState.Normal
Me.ShowInTaskbar = True
shown = True
NotifyIcon1.Visible = False
End Sub

Private Sub MainFormSizeChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.SizeChanged
If shown And Me.WindowState = FormWindowState.Minimized Then
shown = False
Me.ShowInTaskbar = False
NotifyIcon1.Visible = True
End If
End Sub

Here is the problem: When I hit the ol minimize button on mainform, the
application control is passed back to loading and the application exits
since that is the next line. What am I doing wrong/what can I adjust so
that control doesn't go back to loading when the form is minimized??

Plus, any tips on making that better are certainly welcome!

Thanks!
 
A

Armin Zingler

Derek Martin said:
Hi there, this is probably really dumb, but I am using a dialog as my
main form and my startup form is my splash screen - which implements
some progress bars to load up some relevant data. When that is done,
loading form does this:

Me.Visible = False
mainform.ShowDialog()
Application.Exit()

So far so good (is this a dumb way of doing that???).

IMO not a good idea. You should only use a Form as the startup object if the
Form will always be visible from the start til the end of the application.
[...]
Here is the problem: When I hit the ol minimize button on mainform,
the application control is passed back to loading and the application
exits since that is the next line. What am I doing wrong/what can I
adjust so that control doesn't go back to loading when the form is
minimized??

ShowDialog probably exits when the Form is minimized. If you change what
I've written above, this problem should have been solved also.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
D

Derek Martin

I can live with that :)
Any suggestions then on how to accomplish a splash screen in the main
form????


Thanks!
Derek



Armin Zingler said:
Derek Martin said:
Hi there, this is probably really dumb, but I am using a dialog as my
main form and my startup form is my splash screen - which implements
some progress bars to load up some relevant data. When that is done,
loading form does this:

Me.Visible = False
mainform.ShowDialog()
Application.Exit()

So far so good (is this a dumb way of doing that???).

IMO not a good idea. You should only use a Form as the startup object if the
Form will always be visible from the start til the end of the application.
[...]
Here is the problem: When I hit the ol minimize button on mainform,
the application control is passed back to loading and the application
exits since that is the next line. What am I doing wrong/what can I
adjust so that control doesn't go back to loading when the form is
minimized??

ShowDialog probably exits when the Form is minimized. If you change what
I've written above, this problem should have been solved also.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
A

Armin Zingler

Derek Martin said:
I can live with that :)
Any suggestions then on how to accomplish a splash screen in the
main form????

/In/ the main form or before showing the main form? I assume the latter:

shared sub main
dim splash as new splashform
dim main as mainform
splash.show
splash.refresh
main = new mainform
main.show
splash.close
application.run(main)
end sub

I only answered the question above - not the one with the NotifyIcon. ;-)

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
C

copyco

This is the way I did my splash screen. It seems to work great.

This is the code in the main, startup form:

Private Sub Main_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

frmSplash = New frmSplash() ' my name for the form
frmSplash.ShowSplash()
' Apparently this method is available for a reason,
' so it may be better than the .ShowDialog method, etc.

' This is my own stuff for controling the splashscreen
' activity from this main form...
frmSplash.lstSplash.Items.Add("Loading...")
' This is so above changes are displayed...
frmSplash.Refresh()

' When you're done with the splashscreen...
frmSplash.Close()

End Sub


Hope that helps.
 
D

Derek Martin

Thank you to both for your assistance. They have aided me in learning more
about the splashes, however, my particular case seems to be a bit different.

My splash screen uses a progress bar to actually show things as they are
being done on the mainform, loading up some objects, etc. I followed some
patterns from these posts and I get to a certain part that works well, but
it doesn't lend itself to threading very easily. For instance, in the
loading code, if I am referencing the mainform by an object instead of
creating it in the loading code (here I create it in the main method), I
cannot start up a thread on that object, I'd have to recreate it which beats
the purpose. I have attached my code if anyone can assist.

Thanks!

Derek


Public Class loading
Inherits System.Windows.Forms.Form
Dim elapsed As Short = 0
Dim t As Thread
Dim mainform As main

Private Sub StartTimer_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles StartTimer.Tick
elapsed += 1
Me.Opacity = 1
If elapsed >= 10 Then
Me.StartTimer.Enabled = False
Me.beginLoadingComponents()
End If
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Me.ProgressBar1.Value += Me.ProgressBar1.Maximum / 10
If Me.ProgressBar1.Value >= Me.ProgressBar1.Maximum Then
Timer1.Enabled = False
t.Join()
Me.Hide()
Me.Cursor = Cursors.Arrow
mainform.ShowDialog(Me) 'INSTEAD OF DOING THIS, I need to pass
control to this form and dispose of this one...that way I can minimize to a
tray icon without exiting the app! Help!!!!
Application.Exit()
End If
End Sub

Private Sub beginLoadingComponents()
Me.Timer1.Enabled = True
mainform = New main
t = New Thread(AddressOf mainform.gettingstarted)
t.Start()
'mainform.gettingstarted()
End Sub

Private Overloads Sub loading_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Cursor = Cursors.AppStarting
Me.StartTimer.Enabled = True
End Sub
End Class
 
A

Armin Zingler

Derek Martin said:
Thank you to both for your assistance. They have aided me in
learning more about the splashes, however, my particular case seems
to be a bit different.

My splash screen uses a progress bar to actually show things as they
are being done on the mainform, loading up some objects, etc. I
followed some patterns from these posts and I get to a certain part
that works well, but it doesn't lend itself to threading very easily.
For instance, in the loading code, if I am referencing the mainform
by an object instead of
creating it in the loading code (here I create it in the main
method), I cannot start up a thread on that object, I'd have to
recreate it which beats the purpose. I have attached my code if
anyone can assist.

If you want an independent splash screen showing the progress, you need to
create it in it's own thread. I wouldn't create the Mainform in a different
thread.

Within Class Loading (example only):

public shared sub showInstance
dim t as thread
t = new thread(addressof threadstart)
t.start
end sub

private sub threadstart
application.run(new loading)
end sub

Sub main would look like in my first example. If anything happens within the
Mainform that is to be displayed in the Splash Form, I'd raise an event in
the Mainform, handle it in the class/module containing sub main and update
the Splash Form in the event handler. But, when updating the splash Form,
you must use Begininvoke to do it because the event handler handling the
event from the Mainform is executed in the main thread, not in the thread
that created the Splash Form.
 
D

Derek Martin

Thanks Armin, that went a little over my head, but I will investigate this
procedure, it sounds pretty good.
Quick question:

If the sub main() looks like your previous example, then where are you
executing showInstance in the loading class? In formload?

:)

Derek
 
A

Armin Zingler

Derek Martin said:
Thanks Armin, that went a little over my head, but I will investigate
this procedure, it sounds pretty good.
Quick question:

If the sub main() looks like your previous example, then where are
you executing showInstance in the loading class? In formload?

:)

Yes, like the previous example, the only difference is the call to
showInstance instead of Show. I renamed it afterwards.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
C

Cor Ligthert

Hi Armin,

I think I do not understand you.

I changed it to this and I thought the mainform keeps the focus.

I want to change the one I have from you with the progres bar for this as
sample, however I want to use not the application start but the form load
and change that. Do you have a problem with that (Your name will always be
told however with a little addition that I made small but not major
changes).

Public Sub Start()
Me.Show()
RaiseEvent Progress(1, 3)
'Thread.Sleep simulates action without blocking
' the thread showing the Splashform
For i As Integer = 100 To 1000 Step 1000
Threading.Thread.Sleep(i)
Application.DoEvents()
Next
RaiseEvent Progress(2, 3)
For i As Integer = 100 To 2000 Step 100
Threading.Thread.Sleep(i)
Application.DoEvents()
Next
RaiseEvent Progress(3, 3)
End Sub
Private Sub Button1_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
MessageBox.Show("hello")
End Sub

Cor
 
A

Armin Zingler

Cor Ligthert said:
Hi Armin,

I think I do not understand you.

I changed it to this and I thought the mainform keeps the focus.

I want to change the one I have from you with the progres bar for
this as sample, however I want to use not the application start but
the form load and change that. Do you have a problem with that (Your
name will always be told however with a little addition that I made
small but not major changes).

Public Sub Start()
Me.Show()
RaiseEvent Progress(1, 3)
'Thread.Sleep simulates action without blocking
' the thread showing the Splashform
For i As Integer = 100 To 1000 Step 1000
Threading.Thread.Sleep(i)
Application.DoEvents()
Next
RaiseEvent Progress(2, 3)
For i As Integer = 100 To 2000 Step 100
Threading.Thread.Sleep(i)
Application.DoEvents()
Next
RaiseEvent Progress(3, 3)
End Sub
Private Sub Button1_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
MessageBox.Show("hello")
End Sub


I don't understand what's your intention. Of course you can create different
situations. I also thought about it but it would have been more work to
handle them all. In the uploaded project, the work (in Sub Start) is done
before the main Form is shown. In you example, the Form is shown before, so
you can make other examples of course.
 
C

Cor Ligthert

You had a question about the focus, that I do not understand, only one
sentence in your code gives it the focus in my idea that first me.show that
I added.

Cor
 
A

Armin Zingler

Cor Ligthert said:
You had a question about the focus, that I do not understand, only
one sentence in your code gives it the focus in my idea that first
me.show that I added.

The Show was already in my code. The main form was shown /after/ the splash
form. Despite it didn't get the focus. That's what I did not understand.
After adding the MainForm.Activate() in the current version of the code, the
problem is solved, so I don't know which problem you are after.

I don't know why Show should be called 2 times. In addition, I don't want to
show the form before the initialization is completely done (in sub start),
that's why there is a splash screen.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
C

Cor Ligthert

Hi Armin,

did I wrote this
uploaded a project to demonstrate a multithreaded splash screen:

(anybody knows why the main form looses focus?)

I thought it is Armin, quick start with checking however did not understand
what you mean because I was nothing, the form was hidden because the wait of
the thread, so I added a lot of code but did found nothing that let me
understand the problem.

Cor
 
A

Armin Zingler

Cor Ligthert said:
Hi Armin,

did I wrote this

Is this a question??

I thought it is Armin, quick start with checking however did not
understand what you mean because I was nothing, the form was hidden
because the wait of the thread, so I added a lot of code but did
found nothing that let me understand the problem.


The Form was not only hidden (visible =false). Even after the Form was
shown, it did not have the focus. It was hidden behind all other
applications, but it was visible in the taskbar and I could click on it to
bring it to front. I did (and don't) understand why.

In other words:
1. I start the exe in the explorer
2. Splash form is shown and has the focus
3. After some seconds, the Splash form disappears and the Main Form is
shown, BUT it does not have the focus. The Explorer has the focus. I have to
minimize the explorer to see the Main form behind.

After I added
MainForm.Activate()

after
MainForm.Show()

The problem was solved.
 
C

Cor Ligthert

Hi Armin,
Is this a question??

You know where I did come from so you too now we are not that Strict as
German

Yes it was a question without a question mark.

:)

Now I understand what you mean, in the way I did it I got the same problem,
I changed it all back to the same way you intended it, however without the
applications.start.

Than you get this code.

Private Sub MainForm_Load(ByVal sender As Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
spsFrm = New SplashForm
spsFrm.AsyncShow()
AddHandler MainForm.Progress, AddressOf MainformProgress
RaiseEvent Progress(1, 3)
'Thread.Sleep simulates action without blocking
' the thread showing the Splashform
Threading.Thread.Sleep(1000)
RaiseEvent Progress(2, 3)
Threading.Thread.Sleep(3000)
RaiseEvent Progress(3, 3)
spsFrm.AsyncClose()
Me.Activate()
End Sub

I had also to add that Me.Activate to let the form get the focus.

Cor
 

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