Window form opacity .. How to control? - c#

I have a single form window application now I want to change the form opacity when application runs. Means when application run it will show low opacity form and as time increse it will show complete form with 100 opacity. So how to do that. (should I use timer control to control opacity, if yes then how????)

in constructor of the form you can write something like this.
this.Opacity = .1;
timer.Interval = new TimeSpan(0, 0, intervalinminutes);
timer.Tick += ChangeOpacity;
timer.Start();
And then define a method like this
void ChangeOpacity(object sender, EventArgs e)
{
this.Opacity += .10; //replace.10 with whatever you want
if(this.Opacity == 1)
timer.Stop();
}

To fade forms in and out, I usually do this:
for(double opacity = 0.0; opacity <= 1.0; opacity += 0.2) {
DateTime start = DateTime.Now;
this.Opacity = opacity;
while(DateTime.Now.Subtract(start).TotalMilliseconds <= 30.0) {
Application.DoEvents();
}
}
It's a nice, simple solution if you'll be doing it very infrequently. Otherwise, I would recommend using threads.

In the constructor, start the timer control that will call a method at each tick.
timer.Interval = 1000;
timer.Tick += new EventHandler(TimerEventProcessor);
timer.Start();
............
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs)
{
if(this.Opacity < 1)
this.Opacity += .1;
else
timer.Stop();
}

For Exit Decreasing Opacity Animation
///////\\\\\\ Coded by Error X Tech ///////\\\\\\
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private void DecreaseOpacity(object sender, EventArgs e)
{
if (this.Opacity >= 0.1)
{
this.Opacity -= 0.04; //replace 0.04 with whatever you want
}
if (this.Opacity <= 0.0)
timer.Stop();
if (this.Opacity <= 0.1)
{
System.Environment.Exit(1);
Process.GetCurrentProcess().Kill();
}
}
private void Exit_Click(object sender, EventArgs e)
{
timer.Interval = 47; //replace 47 with whatever you want
timer.Tick += DecreaseOpacity;
timer.Start();
}

In the constructor, set the opacity to 0 and start a timer with an interval of something small like 10 or 100 milliseconds. In the timer_Tick event, you simply need to run this.Opacity += 0.01;
This will make it so that the opacity starts at 0 and increase by .01 every few milliseconds until it's 1 (opacity is a double, when it reaches a value of 1 it's fully opaque)

increasing Opacity Animation while Application start
///////\\\\\\ Coded by Error X Tech ///////\\\\\\
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
void IncreaseOpacity(object sender, EventArgs e)
{
if (this.Opacity <= 1) //replace 0.88 with whatever you want
{
this.Opacity += 0.01; //replace 0.01 with whatever you want
}
if (this.Opacity == 1) //replace 0.88 with whatever you want
timer.Stop();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Opacity = .01;
timer.Interval = 10; //replace 10 with whatever you want
timer.Tick += IncreaseOpacity;
timer.Start();
}

Related

How can I restart the Timer after 10 seconds counting down?

I need to add a Timer displaying a countdown from 10. When it reaches 0 it then needs to start again from 10. This needs to be in a continuous loop.
This is what I have so far:
InitializeComponent();
timer1.Interval = 1000;
timer1.Tick += myTimer_Tick;
timer1.Start();
private void myTimer_Tick(object sender, EventArgs e)
{
label2.Text = timeLeft.ToString();
timeLeft -= 1;
if (timeLeft < 0)
{
timer1.Tick += myTimer_Tick;
}
}
What's happening now is when it reaches 0 it continues in the minus, eg. -1 -2 -3 etc.
I need it to start again from 10.
Thanks
Why not simply set the timeLeft back to ten
private void myTimer_Tick(object sender, EventArgs e)
{
label2.Text = timeLeft.ToString();
timeLeft -= 1;
if (timeLeft < 0)
{
timeLeft = 10;
}
}

Smooth Expanding and Collapsing Animation for WinForms

Im trying to get a smooth expanding and collapsing animation for my form. My current animation is really jittery and non consistent. Heres a Gif of the Animation. Is there another way to do this that doesnt freeze the form?
private void ShowHideToggle_CheckStateChanged(object sender, EventArgs e)
{
if (ShowHideToggle.Checked) //checked = expand form
{
ShowHideToggle.Text = "<";
while (Width < originalWidth)
{
Width++;
Application.DoEvents();
}
}
else
{
ShowHideToggle.Text = ">";
while(Width > 24)
{
Width--;
Application.DoEvents();
}
}
}
Create a Timer:
Timer t = new Timer();
t.Interval = 14;
t.Tick += delegate
{
if (ShowHideToggle.Checked)
{
if (this.Width > 30) // Set Form.MinimumSize to this otherwise the Timer will keep going, so it will permanently try to decrease the size.
this.Width -= 10;
else
t.Stop();
}
else
{
if (this.Width < 300)
this.Width += 10;
else
t.Stop();
}
};
And change your code to:
private void ShowHideToggle_CheckStateChanged(object sender, EventArgs e)
{
t.Start();
}

fade window on close

How can I set time to run a for loop?
I'm going on fade a form on close and this is my code:
Time ftmr=new Timer();
//set time interval 5 sec
ftmr.Interval = 5000;
//starts the timer
ftmr.Start();
ftmr.Tick += Ftmr_Tick;
private void Ftmr_Tick(object sender, EventArgs e)
{
for (int i = 0; i <= 100; i++)
{
this.Opacity = 100 - i;
this.Refresh();
}
Dispose();
}
There are several things wrong in your code. First, you shouldn't use a loop to set the Opacity since it will only really do something after the event is handled entirely. Let the timer do the looping. Second, don't Dispose your form in the timer. It will get rid of all UI elements, so you can throw your form away...
Try this:
Time ftmr=new Timer();
//set time interval 5 sec
ftmr.Interval = 5000 / 100; // 100 attempts
//starts the timer
ftmr.Start();
ftmr.Tick += Ftmr_Tick;
double opacity = 1;
private void Ftmr_Tick(object sender, EventArgs e)
{
this.Opacity = opacity;
opacity -= .01;
this.Refresh();
if (this.Opacity <= 0)
{
ftmr.Stop();
}
}
Note that this code will take longer than 5 seconds to run due to the way the timer works. You might need to tweak the numbers a little bit.
This is one of the few cases where it is acceptable to hang the UI thread and use Thread.Sleep() instead. The Opacity property is immediately effective and doesn't require a repaint. Do so in the FormClosing event.
Be sure to start with the Opacity set to 0.99 so you don't get flicker from the native window getting re-created. Five seconds is too long, both to the user and the operating system, about a second is reasonable. For example:
public Form1() {
InitializeComponent();
this.Opacity = 0.99;
}
protected override void OnFormClosing(FormClosingEventArgs e) {
base.OnFormClosing(e);
if (!e.Cancel) {
for (int op = 99; op >= 0; op -= 3) {
this.Opacity = op / 100f;
System.Threading.Thread.Sleep(15);
}
}
}
}

Timer won't tick

I have a Windows.Forms.Timer in my code, that I am executing 3 times. However, the timer isn't calling the tick function at all.
private int count = 3;
private timer;
void Loopy(int times)
{
count = times;
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
count--;
if (count == 0) timer.Stop();
else
{
// Do something here
}
}
Loopy() is being called from other places in the code.
Try using System.Timers instead of Windows.Forms.Timer
void Loopy(int times)
{
count = times;
timer = new Timer(1000);
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
throw new NotImplementedException();
}
If the method Loopy() is called in a thread that is not the main UI thread, then the timer won't tick.
If you want to call this method from anywhere in the code then you need to check the InvokeRequired property. So your code should look like (assuming that the code is in a form):
private void Loopy(int times)
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate
{
Loopy(times);
});
}
else
{
count = times;
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
}
I am not sure what you are doing wrong it looks correct, This code works: See how it compares to yours.
public partial class Form1 : Form
{
private int count = 3;
private Timer timer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Loopy(count);
}
void Loopy(int times)
{
count = times;
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
count--;
if (count == 0) timer.Stop();
else
{
//
}
}
}
Here's an Rx ticker that works:
Observable.Interval(TimeSpan.FromSeconds(1))
.Take(3)
.Subscribe(x=>Console.WriteLine("tick"));
Of course, you can subscribe something more useful in your program.
you may have started the timer from another thread, so try invoking it from the correct thread.
for example, instead of:
timerX.start();
Use:
Invoke((MethodInvoker)delegate { timerX.Start(); });
Check if your timer in properties is enabled.
Mine was false and after setting to true it worked.
If you are using Windows.Forms.Timer then should use something like following.
//Declare Timer
private Timer _timer= new Timer();
void Loopy(int _time)
{
_timer.Interval = _time;
_timer.Enabled = true;
_timer.Tick += new EventHandler(timer_Elapsed);
_timer.Start();
}
void timer_Elapsed(object sender, EventArgs e)
{
//Do your stuffs here
}
If you use some delays smaller than the interval inside the timer, the system.timer will execute other thread and you have to deal with a double thread running at the same time. Apply an InvokeRequired to control the flow.

c# Timer Stop Confusion

I would like to set a textbox text to "blink" by changing text colors when a button is clicked.
I can get the text to blink how I want it to, but I want it to stop after a few blinks. I cannot figure out how to make it stop after the timer fires a few times.
Here is my code:
public Form1()
{
InitializeComponent();
Timer timer = new Timer();
timer.Interval = 500;
timer.Enabled = false;
timer.Start();
timer.Tick += new EventHandler(timer_Tick);
if (timerint == 5)
timer.Stop();
}
private void timer_Tick(object sender, EventArgs e)
{
timerint += 1;
if (textBoxInvFooter.ForeColor == SystemColors.GrayText)
textBoxInvFooter.ForeColor = SystemColors.Highlight;
else
textBoxInvFooter.ForeColor = SystemColors.GrayText;
}
I know my problem lies with how I'm using the "timerint", but I'm not sure where to put it, or what solution I should use...
Thank you for all your help!
You just have to put the timer check inside the Tick handler. You can access the Timer object by using the sender argument of the handler.
private void timer_Tick(object sender, EventArgs e)
{
// ...
timerint += 1;
if (timerint == 5)
{
((Timer)sender).Stop();
}
}
Here's the complete code that I would use to solve your issue. It correctly stops the timer, detaches the event handler, and disposes the timer. It disables the button during the flashing, and also restores the colour of the textbox after the five flashes are complete.
The best part is that it is purely defined within the one lambda, so no class-level variables required.
Here it is:
button1.Click += (s, e) =>
{
button1.Enabled = false;
var counter = 0;
var timer = new Timer()
{
Interval = 500,
Enabled = false
};
EventHandler handler = null;
handler = (s2, e2) =>
{
if (++counter >= 5)
{
timer.Stop();
timer.Tick -= handler;
timer.Dispose();
textBoxInvFooter.ForeColor = SystemColors.WindowText;
button1.Enabled = true;
}
else
{
textBoxInvFooter.ForeColor =
textBoxInvFooter.ForeColor == SystemColors.GrayText
? SystemColors.Highlight
: SystemColors.GrayText;
}
};
timer.Tick += handler;
timer.Start();
};

Categories