I have four buttons that are called "ship1,ship2" etc.
I want them to move to the right side of the form (at the same speed and starting at the same time), and every time I click in one "ship", all the ships should stop.
I know that I need to use a timer (I have the code written that uses threading, but it gives me troubles when stopping the ships.) I don't know how to use timers.
I tried to read the timer info in MDSN but I didn't understand it.
So u can help me?
HERES the code using threading.
I don't want to use it. I need to use a TIMER! (I posted it here because it doesnt give me to post without any code
private bool flag = false;
Thread thr;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
flag = false;
thr = new Thread(Go);
thr.Start();
}
private delegate void moveBd(Button btn);
void moveButton(Button btn)
{
int x = btn.Location.X;
int y = btn.Location.Y;
btn.Location = new Point(x + 1, y);
}
private void Go()
{
while (((ship1.Location.X + ship1.Size.Width) < this.Size.Width)&&(flag==false))
{
Invoke(new moveBd(moveButton), ship1);
Thread.Sleep(10);
}
MessageBox.Show("U LOOSE");
}
private void button1_Click(object sender, EventArgs e)
{
flag = true;
}
Have you googled Windows.Forms.Timer?
You can start a timer via:
Timer timer = new Timer();
timer.Interval = 1000; //one second
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
timer.Start();
You'll need an event handler to handle the Elapsed event which is where you'll put the code to handle moving the 'Button':
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
MoveButton();
}
Related
I need to show a message after 10 seconds of form load.
I am using the below code
private void Form1_Load(object sender, EventArgs e)
{
SetTimeInterval();
}
System.Windows.Forms.Timer MyTimer = new System.Windows.Forms.Timer();
public void SetTimeInterval()
{
MyTimer.Interval = ( 10 * 1000);
MyTimer.Tick += new EventHandler(TimerEventProcessor);
MyTimer.Start();
}
void TimerEventProcessor(Object myObject,EventArgs myEventArgs)
{
MessageBox.Show("TIME UP");
MyTimer.Stop();
MyTimer.Enabled = false;
}
Tried using MyTimer.Stop() and MyTimer.Enabled = false, but messagebox keeps displaying every 10 seconds. How do I stop it after the first instance?
Your problem is that MessageBox.Show() is a blocking call. So MyTimer.Stop() is only called after you close the MessageBox.
So until you closed the MessageBox there will pop up new ones every 10s. The simple solution is to change the order of calls:
void TimerEventProcessor(Object myObject,EventArgs myEventArgs)
{
MyTimer.Stop();
MyTimer.Enabled = false;
MessageBox.Show("TIME UP");
}
So the timer is stopped as soon as you enter the event handler, before displaying the message box.
i would suggest this method
go to theform.designer.cs
writhe this code
this.timer1.Enabled = true;
this.timer1.Interval = 10000;
and do this in ur .cs file
private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("msg");
}
that work perfectly for me.
So, this seems to be a common question but I can't seem to figure out a way to do this. I have a C# Form application that goes out to an imap client and processes the emails. I want to have a timer formatted like "08:45" (for 8 minutes and 45 seconds) displayed on the form to let the user know how long it has been since they clicked the button to start the process.
I want the timer to stop once my process ends obviously.
private void btn_ImportEmail_Click(object sender, EventArgs e)
{
this.timer = new System.Timers.Timer();
this.lblTimer = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize) (this.timer)).BeginInit();
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimerElapsed);
//connect to email and download messages...
this.timer.Enabled = true;
this.timer.SynchronizingObject = this;
timer.Interval = 1000;
timer.Start();
for (int I = 0 ; I <= messages.count() - 1; I++)
{
//process emails
}
timer.EndInit();
}
private void timer1_Tick(object sender, EventArgs e)
{
lblTimer.Text = DateTime.Now.ToString("mm:ss");
}
private void OnTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
lblTimer.Text = DateTime.Now.ToString("mm:ss");
// lblTimer.Text = string.Format("{0:mm:ss}", DateTime.Now);
}
The following SO Q/A might answer your question...
Display the running time of part of a program in a label
I would recommend changing the format to your needs.
The first thing that I see is that you are using DateTime.Now which will give you the current minutes and seconds, not elapsed minutes and seconds. The second thing and the main thing is that since you are processing your emails in your main UI's thread you are preventing your label from being updated, you would be better off looking at using a background worker instead.
Edit based on Idle_Mind's comment added DateTime Object instead of counter.
public partial class Form1 : Form
{
BackgroundWorker bgw = new BackgroundWorker();
Timer timer = new Timer();
DateTime startTime;
public Form1()
{
InitializeComponent();
timer.Interval = 1000;
timer.Tick += timer_Tick;
bgw.DoWork += bgw_DoWork;
bgw.RunWorkerCompleted+=bgw_RunWorkerCompleted;
}
void timer_Tick(object sender, EventArgs e)
{
label1.Text =((TimeSpan)DateTime.Now.Subtract(startTime)).ToString("mm\\:ss");
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
timer.Stop();
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
for (int I = 0 ; I <= messages.count() - 1; I++)
{
//process emails
}
}
private void button1_Click(object sender, EventArgs e)
{
bgw.RunWorkerAsync();
startTime = DateTime.Now;
timer.Start();
}
}
I have this code
private void picTop_MouseEnter(object sender, EventArgs e)
{
if (timer1.Tick == 10)
{
picBottom.Visible = true;
picTop.Visible = false;
timer1.Stop();
}
else
{
MessageBox.Show("ERROR You cannot view this section at this time.\nPlease try again later.");
}
}
private void picBottom_MouseEnter(object sender, EventArgs e)
{
picBottom.Visible = false;
picTop.Visible = true;
timer1.Start();
}
My timerinterval is set at 1000ms (so 1 second)
I only want the user to go into the top panel again after 10 seconds.
Some help would be greatly appreciated.
Current error I get: timer1.Tick is red underlined, error=
"The event 'System.Windows.Forms.Timer.Tick' can only appear on the left hand side of += or -="
Timer.Tick is not property its an event.
Use it like
timer1.Tick +=
{
picBottom.Visible = true;
picTop.Visible = false;
timer1.Stop();
}
For interval use timer.Interval
timer.Interval = 10000;
Ok. I think I understand what you're trying to achieve...
You have 2 areas on your form called "Top" & "Bottom"
Once the user enters & subsequently leaves the top area, you don't want them to be able to enter again for 10 seconds. is that correct?
So you've got a few problems... first of all, Tick is an event to which you would attach a method to be fired when it is raised. it's not an integer you can check. The only integer property on a timer of relevance for timing for is called Interval
But aside from that I don't think your method is going to be particularly effective.
Perhaps a better idea would be to add a MouseExit event to the top area. and disable that area for 10 seconds. and use a timer to re-enable it.
timer1.Tick += timer1_Tick;
public void Top_MouseExit (object sender, EventArgs e)
{
PicTop.Visible = false; // or hide/disbale it some other way
Timer1.Interval = 10000; //10 seconds
Timer1.Start();
}
public void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
PicTop.Visible = true; //renable the top area
}
Here the below code enables the particular bit as high in parallel port.How to set time interval for the button click in windows form.If i set the time interval to 2 seconds the timer should start then 2 seconds after that it should stop automatically.
private void button1_Click(object sender, EventArgs e)
{
PortAccess.Output(888,1);
}
can u please let me know any suggestion or example to do this.Inside this button_click.
So you just want to clear the bit 2 seconds later? Something like this should work:
private void button1_Click(object sender, EventArgs e)
{
// Prevent multiple button clicks
button.Enabled = false;
PortAccess.Output(888, 1);
Timer timer = new Timer { Interval = 2000 };
timer.Tick += HandleTimerTick;
timer.Start();
}
private void HandleTimerTick(object sender, EventArgs e)
{
Timer timer = (Timer) sender;
timer.Stop();
timer.Dispose();
button.Enabled = true;
PortAccess.Output(888, 0);
}
You may find it simpler to set up the timer just once, and store it as an instance variable. Then you could attach the Tick event and set the interval on construction, and add it to the components of the form so that it's automatically disposed. Your methods would then be:
private void button1_Click(object sender, EventArgs e)
{
button.Enabled = false;
PortAccess.Output(888, 1);
timer.Start();
}
private void HandleTimerTick(object sender, EventArgs e)
{
timer.Stop();
button.Enabled = true;
PortAccess.Output(888, 0);
}
I would like to delay an action by several seconds after a mouse pointer has entered and remained for period of time in a winform graphics rectangle. What would be a way to do this?
Thanks
c#, .net 2.0, winform
private Timer timer;
private void rect_MouseEnter(object sender, EventArgs e)
{
timer = new Timer();
timer.Interval = 3000;
timer.Start();
timer.Tick += new EventHandler(t_Tick);
}
private void rect_MouseLeave(object sender, EventArgs e)
{
timer.Dispose();
}
void t_Tick(object sender, EventArgs e)
{
timer.Dispose();
MessageBox.Show(#"It has been over for 3 seconds");
}
Something such as:
static void MouseEnteredYourRectangleEvent(object sender, MouseEventArgs e)
{
Timer delayTimer = new Timer();
delayTimer.Interval = 2000; // 2000msec = 2 seconds
delayTimer.Tick += new ElapsedEventHandler(delayTimer_Elapsed);
}
static void delayTimer_Elapsed(object sender, EventArgs e)
{
if(MouseInRectangle())
DoSomething();
((Timer)sender).Dispose();
}
Probably could be done more efficiently, but should work :D
Two ways to set up MouseInRectangle -> one is to make it get the current mouse coordinates and the position of the control and see if it's in it, another way would be a variable which you would set to false on control.mouse_leave.
Try using the Timer control (System.Windows.Forms.Timer).
Please notice System.Windows.Forms.Timer is not Exact and you cannot rely it will act on exactly the interval given.
it would be prefeable to use System.Times.timer and use the Invoke action to return to the GUI thread.