views:

382

answers:

3

I am tryig to fade-in a windows form using c# but it doesnt seem to work after I have shown the form. Is it possible to change the forms opacity after Ive shown it?

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Timers;

namespace ToolStrip
{
    public partial class Form1 : Form
    {
        Form ToolForm = new ToolForm();
        Form PropForm = new PropertyGrid();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ToolForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            ToolForm.Owner = this;
            ToolForm.Show();
            ToolForm.Location = new Point(50, 50);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PropForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            PropForm.Owner = this;
            PropForm.Show();
            PropForm.Location = new Point(50, 50);

            System.Timers.Timer aTimer = new System.Timers.Timer(10000);

            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 2000;
            aTimer.Enabled = true;

            Console.WriteLine("Press the Enter key to exit the program.");
            Console.ReadLine();
        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            PropForm.Opacity = PropForm.Opacity - 0.25;
            Console.WriteLine(PropForm.Opacity);
        }
    }
}
+3  A: 

because you r using System.Timers.Timer which is a multithread timer, in it's OnTimedEvent() it calls control created by another thread, which cause exception.

If you use System.Windows.Forms.Timer, it will work. i tested.

Benny
It works on the other forms Ive mentioned in the code above. Except its not working after Ive shown them.
If you use System.Windows.Forms.Timer, it will work
Benny
Agreed, the Opacity assignment *has* to be done on the UI thread. Avoid the flicker you now get by keeping Opacity between 0 and 0.99
Hans Passant
It worked! Thank you!
@quddusaliquddus. You should consider accepting the answer by clicking the check mark. Maybe you could upvote any answers that helped also, by clicking the up arrow next to them.
PaulG
+1  A: 

Using your code (and creating the other necessary Form classes), I get a cross-threading exception the first time the timer fires and the event handler is called, as Benny suggests.

Making changes to your code to check InvokeRequired in the timer event handler, and use Invoke if necessary to change PropForm.Opacity, results in the opacity changing after the form is shown, as required.

Note that you probably want to start with an Opacity of 0, and increase it gradually - otherwise your form will start off completely solid and gradually fade out

I will mention in passing that Opacity will have no effect on some versions of Windows, though you say you have Opacity effects working elsewhere, so it shouldn't be that in this case.

AakashM
A: 

Ive gotten it to work without timers:

        int Loop = 0;

        for (Loop = 100; Loop >= 5; Loop -= 10)
        {
            this.PropForm.Opacity = Loop / 95.0;
            this.PropForm .Refresh();
            System.Threading.Thread.Sleep(100);
        }

but i cant seem to change this example to fade-in instead of out.