how do I delay action on mouse enter rectangle c# - c#

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.

Related

How to use timer to click buttons in C#

How to use timers to trigger click button event every 3 seconds?
I'm trying to rotate 2 pictures in pictureboxes by triggering the rotate button automaticly using timer but it seems doesnt works. I never used timer before so this is my first time. Anyone know whats wrong with my code or any other code suggestion for it? Thanks
Code I'm using
private void timer1_Tick(object sender, EventArgs e)
{
rotateRightButton_Click(null, null);
pictureBox1.Refresh();
pictureBox2.Refresh();
}
private void timerStartButton_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timerStopButton_Click(object sender, EventArgs e)
{
timer1.Stop();
}
It's even possible (and more simple) with tasks
public partial class Form1 : Form
{
// variable to keep track if the timer is running.
private bool _timerRunning;
public Form1()
{
InitializeComponent();
}
private async Task StartTimer()
{
// set it to true
_timerRunning = true;
while (_timerRunning)
{
// call the rotateRightButton_Click (what you want)
rotateRightButton_Click(this, EventArgs.Empty);
pictureBox1.Refresh();
pictureBox2.Refresh();
// wait for 3 seconds (but don't block the GUI thread)
await Task.Delay(3000);
}
}
private void rotateRightButton_Click(Form1 form1, EventArgs empty)
{
// do your thing
}
private async void buttonStart_Click(object sender, EventArgs e)
{
// if it's already started, don't start it again.
if (_timerRunning)
return;
// start it.
await StartTimer();
}
private void buttonStop_Click(object sender, EventArgs e)
{
// stop it.
_timerRunning = false;
}
}
timer1.Interval = 3000; // set interval to 3 seconds and then call Time Elapsed event
timer1.Elapsed += Time_Elapsed;
//Event
private void Time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// will be triggered in every 3 seconds
rotateRightButton_Click(null, null);
pictureBox1.Refresh();
pictureBox2.Refresh();
}
Hope this helps!

MediaElement position doesn't change

I want to use MediaElement to play music, and when music played to some position, do some action. The code is like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
game_pose_poller.RunWorkerAsync(); // game_pose_poller is a BackgroundWorker object
button1.IsEnabled = false;
}
private void game_pose_poller_DoWork(object sender, DoWorkEventArgs e)
{
while(true)
{
if (mediaElement1.Position >= sometime)
{
// do something
However I found the program did nothing at all. When debugging I found that mediaElement1.Position is always zero. Why is it always zero even after Play() called? mediaElement1.Source is an mp3 file which included as resource in project, and LoadedBehavior is Manual(or the Play() raise exception).
I've made something similar some times ago using a DispatchTimer instead of a BackgroundWorker. This is how I've got it to work:
private DispatcherTimer positionTimer;
private void button1_Click(object sender, RoutedEventArgs e)
{
// Create a timer that will check your condition (every second for example)
positionTimer= new DispatcherTimer();
positionTimer.Interval = TimeSpan.FromSeconds(1);
positionTimer.Tick += new EventHandler(positionTimerTick);
positionTimer.Start();
mediaElement1.Play();
button1.IsEnabled = false;
}
void positionTimerTick(object sender, EventArgs e)
{
if (mediaElement1.Position.TotalSeconds >= sometime)
{
// do something
}
}

How to add delay inside a for-loop using timers

By following code(the interval of timer2 is 1000)
private void timer1_Tick(object sender, EventArgs e) {
timer7.Enabled=false;
timer8.Enabled=false;
lblTimer_Value_InBuildings.Text="0";
}
private void timer2_Tick(object sender, EventArgs e) {
lblTimer_Value_InBuildings.Text=(int.Parse(lblTimer_Value_InBuildings.Text)+1).ToString();
}
we can not create a delay in a for-loop
for(int i=1; i<=Max_Step; i++) {
// my code...
// I want delay here:
timer1.Interval=60000;
timer1.Enabled=true;
timer2.Enabled=true;
// Thread.Sleep(60000); // makes no change even if uncommenting
}
Whether I uncomment the line Thread.Sleep(60000); or not, we see nothing changed with lblTimer_Value_InBuildings in timer2_Tick.
Would you please give me a solution(with or without timers)?
Your timer is your loop, you don't need a for loop. You just keep track of your loop variables outside of the function calls. I would recommend wrapping all of this functionality into a class, to keep it separate from your GUI code.
private int loopVar = 0;
public void Form_Load()
{
// Start 100ms after form load.
timer1.Interval = 100;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
// My Code Here
loopVar++;
if (loopVar < Max_Step)
{
// Come back to the _tick after 60 seconds.
timer1.Interval = 60000;
timer1.Enabled = true;
}
}
When you do Thread.Sleep(60000), you are telling the UI thread to sleep for 60 seconds. What this also does is prevent the execution of the timer, because the UI thread is hung up sleeping instead of processing events such as the timer sleep.
The code totally made me feel boring
methods:
private void timer1_Tick(object sender, EventArgs e) {
timer2.Enabled=false;
timer1.Enabled=false;
lblTimer_Value_InBuildings.Text="0";
}
private void timer2_Tick(object sender, EventArgs e) {
lblTimer_Value_InBuildings.Text=(int.Parse(lblTimer_Value_InBuildings.Text)+1).ToString();
}
private void timer3_Tick(object sender, EventArgs e) {
if(0!=(int)timer3.Tag) {
// your code goes here and peformed per step
timer1.Enabled=true;
timer2.Enabled=true;
}
timer3.Tag=(1+(int)timer3.Tag)%Max_Step;
}
initials:
var delayedInterval=60000;
timer1.Interval=60000;
timer2.Interval=1000;
timer3.Interval=delayedInterval+timer1.Interval;
lblTimer_Value_InBuildings.Text="0";
timer3.Tag=1;
timer3.Enabled=true;
Your original code never makes timer1 and timer2 stopped, so I think you should correct timer7 and timer8 to timer1 and timer2, otherwise they don't make sense here.

moving a button using a timer

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();
}

How to set time interval to button_click in C# windows form application

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);
}

Categories

Resources