Results 1 to 11 of 11

Thread: [RESOLVED] Opacity increase slowly works but form is empty before it reaches max

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Resolved [RESOLVED] Opacity increase slowly works but form is empty before it reaches max

    Hi all o7
    Consider following code to achieve a sort of fade-in effect:
    Code:
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
            Me.Invoke(Sub()
                          While Me.Opacity < 0.9
                              Me.Opacity += 0.00001 'BRIGHT UP BACKGROUND
                          End While
                      End Sub)
        End Sub
    It does the matter but the form is empty (All labels and pictures are invisible apparently) until while loop completes (I guess) How do I rectify this?
    - Without Me.Invoke = not even fading.
    - Tried "FormLoad" event = not even fading.
    - Not a Timer control fan.
    - Never tried "InitializeComponent()".

  2. #2

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Opacity increase slowly works but form is empty before it reaches max

    Temporary worked out with a "Timer" as follows:
    Code:
        Private Sub FADEIN_Tick(sender As Object, e As EventArgs) Handles FADEIN.Tick
            If Me.Opacity < 0.9 Then
                Me.Opacity += 0.01
            Else
                'DO FINISHING THINGS OR CALL A FUNC.
                FADEIN.Enabled = False
            End If
        End Sub
    I convinced that when you are pursuing graphical goals, you have to switch over on bad things. Like more control, more inefficient ways to code.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Opacity increase slowly works but form is empty before it reaches max

    It’s a matter of threading. If you’re tying up the UI thread with a while loop, it can’t be doing anything else…

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Opacity increase slowly works but form is empty before it reaches max

    InitializeComponent is a function that is generated for you. I'm not sure what you expected it to do, but you shouldn't be calling it yourself, aside from the one call in the constructor. Calling it at any other time would create some weird results.

    A timer is pretty much the only option that seems likely to work for this. You are dealing with the UI, which means the UI thread. The loop you are running will cause the form to at least invalidate, but it never gets a chance to paint, because that would require the loop to pause. It's somewhat possible that you might achieve your goal with Application.DoEvents, and that might be pretty harmless in this particular case, but that's usually a bad thing to be doing. Therefore, the timer is the best option.
    My usual boring signature: Nothing

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Opacity increase slowly works but form is empty before it reaches max

    Quote Originally Posted by Shaggy Hiker View Post
    InitializeComponent is a function that is generated for you. I'm not sure what you expected it to do, but you shouldn't be calling it yourself, aside from the one call in the constructor. Calling it at any other time would create some weird results.

    A timer is pretty much the only option that seems likely to work for this. You are dealing with the UI, which means the UI thread. The loop you are running will cause the form to at least invalidate, but it never gets a chance to paint, because that would require the loop to pause. It's somewhat possible that you might achieve your goal with Application.DoEvents, and that might be pretty harmless in this particular case, but that's usually a bad thing to be doing. Therefore, the timer is the best option.
    Your loquacious answer filled in the blanks in my terse reply. I was going to mention DoEvents, but I thought it was better left unsaid without a good explanation…

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Opacity increase slowly works but form is empty before it reaches max

    It's not usually a good suggestion to make, but in this case, where the form really can't be doing anything else, it just might be a good solution. I still prefer the timer, myself. Timers are quite efficient.
    My usual boring signature: Nothing

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Opacity increase slowly works but form is empty before it reaches max

    FWIW this worked nicely

    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Me.Visible = False
            Me.DoubleBuffered = True
            Me.Opacity = 0.0
        End Sub
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            Me.Visible = True
            Dim tsk As Task = Task.Run(Sub()
                                           For x As Integer = 1 To 100
                                               Threading.Thread.Sleep(10)
                                               Me.BeginInvoke(Sub()
                                                                  Me.Opacity += 0.01
                                                              End Sub)
                                           Next
                                       End Sub)
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Opacity increase slowly works but form is empty before it reaches max

    Perfect, thanks.

  9. #9
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: [RESOLVED] Opacity increase slowly works but form is empty before it reaches max

    When it come to presentation effects with WinForms... what I have learned.... Just dont do it!

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Opacity increase slowly works but form is empty before it reaches max

    Quote Originally Posted by pourkascheff View Post
    Perfect, thanks.
    dBasnett’s answer looks all shiny and new, but in reality, it’s not more efficient than the Timer method.
    It’s a secondary thread with 100 cross thread operations, so probably not as efficient as the timer IMO…

  11. #11
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Opacity increase slowly works but form is empty before it reaches max

    Quote Originally Posted by .paul. View Post
    dBasnett’s answer looks all shiny and new, but in reality, it’s not more efficient than the Timer method.
    It’s a secondary thread with 100 cross thread operations, so probably not as efficient as the timer IMO…
    Even more efficient would be to not do it at all. Its just not necessary.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width