schenkp
asked on
Confused Need Help With Code
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(10 0);
t = new Timer();
t.Tick += new EventHandler(t_Tick);
t.Interval = 100;
t.Start();
}
}
}
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(10
t = new Timer();
t.Tick += new EventHandler(t_Tick);
t.Interval = 100;
t.Start();
}
}
}
of course don't forget to add this.KeyPress += new System.Windows.Forms.KeyPr essEventHa ndler(Form 1_KeyPress );
to the initializecomponents thing
to the initializecomponents thing
ASKER
It fades nice but how about having it come right back to 100% Opacity?
ASKER
I would like it to fade then come right back just by clicking the button?
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.KeyPr essEventAr gs e)
{
doFade();
}
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.KeyPr
{
doFade();
}
ASKER
Can you have it come right back to 100 opacity?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
SWEET....
Thanks Bro
Thanks Bro
no prob
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.KeyPr
{
doFade();
}