Opacity the Second Time Around

E

eBob.com

I have a real simple app involving the main form and a subform. The main
form uses ShowDialog so that when the subform Closes it is not disposed. I
want the subform to come into view gradually, so its Load code does the
following ...

MsgBox("SubForm Load entered")
For i As Integer = 0 To 100
Me.Opacity = i / 100
Thread.Sleep(10)
Next i

(Except for the Sub and End Sub statements that's the whole thing.)

The first time the main form executes the ShowDialog for the subform I see
what I expect - the subform comes gradually into view. But the second time,
and all subsequent times, the main form executes the ShowDialog, there's a
pause - like it's cranking through the Sleep()s - and then the subform pops
up all at once. I always see the message box, so the Load code for the
subform is being executed. It's like that Me.Opacity = i/100 statement is
not executed or does not work except for the first time the Load code is
executed.

Any ideas what might be going on here?

Thanks, Bob
 
A

Armin Zingler

eBob.com said:
I have a real simple app involving the main form and a subform. The
main form uses ShowDialog so that when the subform Closes it is not
disposed. I want the subform to come into view gradually, so its
Load code does the following ...

MsgBox("SubForm Load entered")
For i As Integer = 0 To 100
Me.Opacity = i / 100
Thread.Sleep(10)
Next i

This does not make sense because Load is executed *before* the Form get's
visible the first time. I suggest moving the code into the Shown event.
I'd also call (Me.)Refresh after setting opacity.


Armin
 
E

eBob.com

Thanks Armin. I had never heard of the Shown event before. Moving the code
to the Shown event handler works like a charm. I guess the mystery now is
why it works the first time in the Load event handler. BTW I think I've
seen other evidence that the Load event handler does not always run before
the form becomes visible for the first time. Specifically, I've some code
in a Load event handler which TRIES to get the form properly sized before it
becomes visible for the first time, and, if I recall correctly, I can see
the size of the form changing before it becomes the size I want. I'll look
into that and report back.

Thanks again for your help. As usual exactly what I needed.

Bob
 
L

Lloyd Sheen

eBob.com said:
I have a real simple app involving the main form and a subform. The main
form uses ShowDialog so that when the subform Closes it is not disposed. I
want the subform to come into view gradually, so its Load code does the
following ...

MsgBox("SubForm Load entered")
For i As Integer = 0 To 100
Me.Opacity = i / 100
Thread.Sleep(10)
Next i

(Except for the Sub and End Sub statements that's the whole thing.)

The first time the main form executes the ShowDialog for the subform I see
what I expect - the subform comes gradually into view. But the second
time, and all subsequent times, the main form executes the ShowDialog,
there's a pause - like it's cranking through the Sleep()s - and then the
subform pops up all at once. I always see the message box, so the Load
code for the subform is being executed. It's like that Me.Opacity = i/100
statement is not executed or does not work except for the first time the
Load code is executed.

Any ideas what might be going on here?

Thanks, Bob

Here is some code that I use for exactly what you are doing. I use a timer
though to do the change in opacity.

Private Sub ShowTimer_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ShowTimer.Tick
ShowTimer.Enabled = False
Me.Opacity += 0.1
If Me.Opacity < 1 Then
ShowTimer.Enabled = True
End If
End Sub

Private Sub CDArtForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Me.Opacity = 0.0
ShowTimer.Interval = 200
ShowTimer.Enabled = True

Me.Height = ht
Me.Width = wid
Me.Top = ttop
Me.Left = lleft

End Sub
 

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