form becoming black while increasing opacity from 0 to 1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akshaycjoshi
    New Member
    • Jan 2007
    • 153

    #1

    form becoming black while increasing opacity from 0 to 1

    I am ShowDialoging one form in which the activate event increases the opacity from 0 to 1. I set the opacity to 0 in it's constructor.The problem is that during the showing of the form it becomes black.
    And after it's opacity becomes one it becoems the normal color (Control).
    The code is as follows

    Code:
             public Form1()
            {
                InitializeComponent();
                this.Opacity = 0;
            }
    
            private void Form1_Activated(object sender, EventArgs e)
            {
                while (this.Opacity < 1)
                {
                    this.Opacity += .01;
                    Thread.Sleep(100);
                }
            }

    I wanted to show the screenshot but when i print-screen while it is black and paste it in MS Paint it shows the normal way !
    Tha's weird.How to get around this ?

    EDIT:I found the solution.The thread is Sleeping so how would it display the opacity changed form !
    I used this.Refresh(); before the sleep and it worked.
    Last edited by akshaycjoshi; Jan 6 '09, 03:58 AM. Reason: found solution
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Yup you are correct. That loop is also known as a "busy loop" as it keeps the thread busy and does not allow it to process the windows messages (which by calling Refresh() you are forcing it to do.) Application.DoE vents() should also work.

    Comment

    Working...