Wait then execute code in C#? - c#

Ok, i have a closing event on my form. The code inside it fades the form out, then opens the other form. Is there a way i can wait until the fade is done? Here is my code:
private void form1_closing(object sender, FormClosingEventArgs e)
{
if (this.Opacity > 0.01f)
{
e.Cancel = true;
timer1.Interval = 47;
timer1.Enabled = true;
timer1.Start();
}
else
{
timer1.Enabled = false;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
progressBar1.Increment(+1);
label4.Text = progressBar1.Value.ToString() + "%";
if (progressBar1.Value == 100)
{
progressBar1.Value = 100;
timer2.Stop();
this.Close();
Form2 frm2 = new Form2();
frm2.Show();
}
}
Basically, form2 is opening early, i want it to wait after the fade effect is done. :)

this code should do, I guess.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
for (double i = 1.0; i > 0.0; i-=0.01)
{
this.Opacity = i;
Thread.Sleep(10);
}
//Handle whatever you want to do here. The control comes here only when the form is faded out.
}

Code :
void Wait(int Milliseconds)
{
Stopwatch sw = new Stopwatch();
sw.Start();
while(sw.ElapsedMillisecods < Millisecods)
{
Application.DoEvents();
}
sw.Stop();
}
Here is a Wait function and you can use it if you can calculate the total time taken during fading like this :
Wait(Time)

Related

Auto Switch between multiple forms with timer

I have 5 Forms, 1 main form, and 4 forms I want them to switch between each other Automatically every couple of seconds (take turns, each form x seconds and switch to the next).
I have 2 forms so far switching between each other every 2 seconds.
void mytimer_Tick(object sender, EventArgs e)
{
if (!frm2.Focused)
frm2.Focus();
else
frm3.Focus();
}
private void Form1_Load_1(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += mytimer_Tick;
mytimer.Interval = 2000;
mytimer.Start();
}
Thankyou.
Crude format. But you will get the idea.
private void HideAllForms()
{
frm1.Hide();
frm2.Hide();
frm3.Hide();
frm4.Hide();
}
void mytimer_Tick(object sender, EventArgs e)
{
if (frmSrl == 1)
{
frmSrl++;
HideAllForms();
frm1.Show();
}
else if (frmSrl == 2)
{
frmSrl++;
HideAllForms();
frm2.Show();
}
else if (frmSrl == 3)
{
frmSrl++;
HideAllForms();
frm3.Show();
}
else if (frmSrl == 4)
{
frmSrl =1;
HideAllForms();
frm4.Show();
}
else
frmSrl = 1;
}
int frmSrl = 1;
private void Form1_Load_1(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += mytimer_Tick;
mytimer.Interval = 2000;
mytimer.Start();
}

How to change the window position in wpf on a mouse click

I need to change the position of a window on mouse click.
here is the code.
private void Button_Click(object sender, RoutedEventArgs e)
{
for(int i=0; i<50; i++)
{
this.Top -= i;
this.Left -= i;
}
}
But whenever i run this program only the last position is shown. My intention is to move it continuosly till the end of loop.
Finally i found the answer myself. It s working perfectly as i expected. I used SynchronizationContext which can post Actions to update controls on UI thread.
public partial class Splash : Window
{
SynchronizationContext sc;
System.Timers.Timer t;
double i=0;
double tempTop;
double angle = 0;
public Splash()
{
InitializeComponent();
sc=SynchronizationContext.Current;
}
private void Move(object sender, MouseEventArgs e)
{
DragMove();
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void btnMinim_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
l1.Content = "Helicopter Moving";
if(t!=null)
{
t.Stop();
t.Dispose();
}
//for (double i = 0; i < 1; i += 0.05)
//{
// this.Top -= i;
// this.Left -= i;
// Thread.Sleep(100);
//}
//l1.Content = "Helicopter Stopped";
tempTop = this.Top;
t = new System.Timers.Timer();
t.Interval = 10;
t.Enabled = true;
t.Elapsed += Change;
t.Start();
}
void Change(object sender, EventArgs e)
{
if (i <= 3)
{
sc.Post(o =>
{
this.Top = tempTop * (Math.Cos(Math.PI * angle / 180));
this.Left -= i;
angle = (angle >= 360) ? 0 : ++angle;
i = i + 0.01;
}, null);
}
else
{
t.Stop();
i = i * -1;
}
}
}
}
Try this should work Thread.Sleep will not work for you as its a UI thread. You need timer to make this work
Timer t;
private void Button_Click(object sender, RoutedEventArgs e)
{
i=0;
if(t!=null)
{
t.Stop();
t.Dispose();
}
t = new Timer();
t.Interval = 800;
t.Enabled = true;
t.Tick += T_Tick;
t.Start();
}
int i=0;
private static void T_Tick(object sender, EventArgs e)
{
if(i<=50)
{
this.Top -= i;
this.Left -= i;
i++;
}
else
t.Stop();
}
Just start an animation when your click event triggers. You can define how long should the animation last.
Basic benefit of using animations instead of doing calculations manually is animations being run in separated thread, so you don't loose application's responsiveness.
What is more, you can edit your animations in separated tools, such as Blend without the need to verifying animations in runtime.
Few sources:
http://www.wpf-tutorial.com/styles/trigger-animations-enteractions-exitactions/
http://dotnetslackers.com/articles/wpf/IntroductionToWPFAnimations.aspx
http://www.wpftutorial.net/Animation.html

How to show the label for couple of seconds and show another label in form

I have a mini form3 https://imageshack.com/i/p1zxB6Lqp which shows a gif image running for 4 sec. So i need to show 4 different
labels in same position..
For example
label 1 - `Connecting to smtp server..`
label 2 - `Fetching recipients..`
label 3 - `Attaching necessary G-code files..`
label 4 - `Please wait sending..`
How can i show all these labels one after another in same position.. so it looking more professional
for sending mail.
My code snippet:-
Form1
private void button2_Click(object sender, EventArgs e)
{
//mail inforamtion
_f3.ShowDialog(); // - - >> is the form i wanted with all labels
smtp.Send(msg);
MessageBox.Show("Email Successfully Sent!!!", "Mail!!!.");
Environment.Exit(0);
}
Form3:
Timer formCloser = new Timer();
private void Form3_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
timer1.Stop();
}
Please help me out.. how can i add label in my form..
Found the answer.. I kept two timers for each function,
timer1 to run the mini form only for 5 seconds.
and timer two to run only for 1 sec. If any body has a better code to share with please..
Most welcome!!
Here is my code:
private void Form3_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer2.Interval = 1000;
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
timer1.Stop();
}
int StopTime = 0;
private void timer2_Tick(object sender, EventArgs e)
{
StopTime++;
if (StopTime == 1)
{
label1.Text = " Connecting to smtp server..";
}
if (StopTime == 2)
{
label1.Text = " Fetching recipients..";
}
if (StopTime == 3)
{
label1.Text = " Attaching G-code files..";
}
if (StopTime == 4)
{
label1.Text = " Done!!";
StopTime = 0;
timer2.Stop();
}
}

Timer writes in log twice

Everything works fine except for one thing. My program prints "player hit" 2 times then "mob hit" 2 times and then "player hit" 2 times again and so forth. I want it to type "player Hit" "mob Hit" "player Hit" "mob Hit" and so on. I can't see why it types it 2 times.
My code looks like this:
public partial class Form1 : Form
{
public Form1()
{
int Rase1 = 0;
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = (1000);
timer1.Enabled = false;
timer2.Tick += new EventHandler(timer2_Tick);
timer2.Interval = (1000);
timer2.Enabled = false;
}
private void buttonChoseRase_Click(object sender, EventArgs e)
{
if (Rase1 == 0)
{
Rase1 = 1;
Rase Rase = new Race(this);
Rase.Show();
}
else if (Race1 >= 1)
{
buttonChoseRase.Enabled = false;
Rebirth Reb = new Rebirth(this);
Reb.Show();
}
}
private void buttonStart_Click(object sender, EventArgs e)
{
textBoxCombatLog.Text = "";
buttonStart.Enabled = false;
buttonStop.Enabled = true;
timer1.Start();
timer2.Stop();
}
private void buttonStop_Click(object sender, EventArgs e)
{
buttonStart.Enabled = true;
buttonStop.Enabled = false;
timer1.Stop();
timer2.Stop();
}
private void LogWrite(string txt)
{
textBoxCombatLog.AppendText(txt + Environment.NewLine);
textBoxCombatLog.SelectionStart = textBoxCombatLog.Text.Length;
}
private void timer1_Tick(object sender, EventArgs e)
{
LogWrite(TimeDate + "Player Hit \n");
timer1.Stop();
timer2.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
LogWrite(TimeDate + "Mob Hit \n");
timer2.Stop();
timer1.Start();
}
private string TimeDate
{
get { return "[" + DateTime.Now.ToString("HH:mm:ss") + "]" + ": "; }
}
}
Here is what is looks like running, and no matter what I do, everything shows up twice:
You've attached the event handler to the Tick event of the timer through designer in addition to through the code file itself. You need to remove one of the two. Either attach the event handler in code or attach it through the designer.

Countdown after clicking a button that goes from 10 to 0 setting text on a label C#

My problem is very simple but I can't figure it out, so I need your help.
The problem is that I have a button and a label in a form, I simply want to click the button and see the label countdown from 10 to 0 and after that happens the form closes, that simple, can someone help me with this?
BTW, my real app is a form that shows video in real time from my webcam and the idea is to click the button, see the count down and when it finishes the appp saves the current frame as an image.
Thanks in advice!
It sounds like you probably just need three things:
A counter in your class as an instance variable
A timer (System.Windows.Forms.Timer or a DispatcherTimer depending on what UI framework you're using)
A method handling the timer's Tick even which decrements the counter, updates the UI, and stops the timer + takes a snapshot if the counter reaches 0
You can do all of this without any other threads.
Using WindowsFormsApplication u can do it like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Enabled = false; // Wait for start
timer1.Interval = 1000; // Second
i = 10; // Set CountDown Maximum
label1.Text = "CountDown: " + i; // Show
button1.Text = "Start";
}
public int i;
private void button1_Click(object sender, EventArgs e)
{
// Switch Timer On/Off
if (timer1.Enabled == true)
{ timer1.Enabled = false; button1.Text = "Start"; }
else if (timer1.Enabled == false)
{ timer1.Enabled = true; button1.Text = "Stop"; }
}
private void timer1_Tick(object sender, EventArgs e)
{
if (i > 0)
{
i = i - 1;
label1.Text = "CountDown: " + i;
}
else
{ timer1.Enabled = false; button1.Text = "Start"; }
}
}
You only need a label, a button and a timer.
use this code. put one timer,label and button.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
}
private static int i = 10;
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "10";
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = (i--).ToString();
if (i < 0)
{
timer1.Stop();
}
}
}

Categories