Solved

Confused Need Help With Code

Posted on 2006-11-04
9
206 Views
Last Modified: 2010-04-16
HI,
I am using the following code to have a form fade out to zero percent opacity, then fade right back into 100 Opacity when a button is pushed.  

I cant seem to shut it off after it fades back to 100 opactiy?

Also I need to change the form size to 200 by 200 using this code when the form is at 0 opacity

this.Size = new Size(200, 200);

But I am not sure where to put insert the code?


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

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }
       // private String imgName;
        private DateTime dt;
        private Timer t;
        private bool fadeOut = true;

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
     
        void t_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = dt.Subtract(DateTime.Now);
            if (ts.TotalSeconds > 0)
            {
                if (fadeOut)
                {
                    if (this.Opacity > .0)
                    {
                        this.Opacity -= .01;
                    }
                    else
                    {
                        fadeOut = !fadeOut;
                    }
                }
                else
                {
                    if (this.Opacity < 1.0)
                    {
                        this.Opacity += .01;
                    }
                    else
                    {
                        fadeOut = !fadeOut;
                    }
                }
            }
            else
            {
                t.Stop();
                //this.Close(); // optional
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dt = DateTime.Now.AddSeconds(100);
            t = new Timer();
            t.Tick += new EventHandler(t_Tick);
            t.Interval = 100;
            t.Start();
        }

    }
}
0
Comment
Question by:schenkp
  • 5
  • 4
9 Comments
 
LVL 3

Expert Comment

by:lizard450
ID: 17874669
I think this is what you want... ???  i'm failing to see why you need a timer... can you explain?


private bool falseIsDown = true;
        private void button1_Click(object sender, EventArgs e)
        {
            doFade();
        }

        private void doFade()
        {
            falseIsDown = !falseIsDown;
            double incrementAmount = .01;
            if(!falseIsDown)
            {
                incrementAmount = incrementAmount*-1;
            }
            double loopTimes = Math.Abs(1/incrementAmount);
            for (double a = loopTimes; a >= 0;a--)
                this.Opacity += incrementAmount;
            this.Size = new Size(200, 200);
        }

        void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            doFade();
        }
0
 
LVL 3

Expert Comment

by:lizard450
ID: 17874680
of course don't forget to add   this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(Form1_KeyPress);
     to the initializecomponents thing
0
 

Author Comment

by:schenkp
ID: 17874685
It fades nice but how about having it come right back to 100% Opacity?
0
 

Author Comment

by:schenkp
ID: 17874689
I would like it to fade then come right back just by clicking the button?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Expert Comment

by:lizard450
ID: 17874692
Meh... just thought id clean the code up a bit...

private int falseIsDown = 1;
        private void doFade()
        {
            falseIsDown = falseIsDown*-1;
            double incrementAmount = .01*falseIsDown;
            double loopTimes = Math.Abs(1/incrementAmount);
            for (double a = loopTimes; a >= 0;a--)
                this.Opacity += incrementAmount;
            this.Size = new Size(200, 200);
        }

        void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            doFade();
        }
0
 

Author Comment

by:schenkp
ID: 17874694
Can you have it come right back to 100 opacity?
0
 
LVL 3

Accepted Solution

by:
lizard450 earned 500 total points
ID: 17874696
private void doFade()
        {
            int TimetoWait = 1000;
            int flipInt = -1;
            double incrementAmount = .01*flipInt;
            double loopTimes = Math.Abs(1/incrementAmount);
            for (double a = loopTimes; a >= 0;a--)
                this.Opacity += incrementAmount;
            this.Size = new Size(200, 200);
            Thread.Sleep(TimetoWait);
            incrementAmount = incrementAmount*flipInt;
            for (double a = loopTimes; a >= 0; a--)
                this.Opacity += incrementAmount;          
           
        }
0
 

Author Comment

by:schenkp
ID: 17874703
SWEET....

Thanks Bro
0
 
LVL 3

Expert Comment

by:lizard450
ID: 17874705
no prob
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now