Opacity Problems

The Opacity property on a windows form allows the form to look translucent.  By looping through varying degrees of opacity in the form's closing event I have a form that looks like it is fading out.  In VB 2005 I have been unable to replicate this.  The problem is that when the form begins to fade, instead of retaining the form's look and colors, it turns completely black and then fades to lesser degrees of gray.  In VB 2003 I tricked it into working by setting the form's default opacity property to 99%.  Here is my code.  What am I doing wrong


Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
     For d As Double = 1.0 To 0.0 Step -0.1
        Me.Opacity = d
        System.Threading.Thread.Sleep(200)
     Next
End Sub


 



Answer this question

Opacity Problems

  • SimonDowling

    Thanks!  Adding the Application.DoEvents() solved the problem.
  • Mark Pearl

    It may be important to understand why the doevents does help.

    Your form needs to repaint itself for new opacity levels and the do events allows that to occur.

  • Krop

    This code worked for me when I tried it:

    Dim pfIndex As Single

    For
    pfIndex = 1 To 0 Step -0.1
        
    Me.Opacity = pfIndex
         Application.DoEvents()
         System.Threading.Thread.Sleep(100)
    Next


  • Opacity Problems