user241949
user241949

Reputation: 111

Windows Forms Opacity After Shown- C#

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);
        }
    }
}

Upvotes: 1

Views: 2644

Answers (3)

user241949
user241949

Reputation: 111

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.

Upvotes: 0

AakashM
AakashM

Reputation: 63340

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.

Upvotes: 1

Benny
Benny

Reputation: 8815

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.

Upvotes: 3

Related Questions