I'm currently working on a project in UWP and I have a CommandBar that I want to go from Hidden to Compact if the mouse moves. After five seconds (If the mouse dont move) the CommandBar should go back to Hidden again.
I dont get any errors, but when I move the mouse the CommandBar is going crazy and it's just flashing from Hidden to Compact when I move the mouse again. I think the problem is that the OnMouseMovement event is stacking upon itself.
This is my code for the mouse movement event:
public async void OnPointerMoved(object Sender, PointerRoutedEventArgs e)
{
CmdBar.ClosedDisplayMode = AppBarClosedDisplayMode.Compact;
DispatcherTimer ButtonTimer = new DispatcherTimer();
ButtonTimer.Interval = TimeSpan.FromSeconds(5);
ButtonTimer.Tick += (sender, args) =>
{
CmdBar.ClosedDisplayMode = AppBarClosedDisplayMode.Hidden;
};
ButtonTimer.Start();
}
I made a little test project to try it out and get you an answer, this is what I did :
private DispatcherTimer Timer { get; set; }
public MainPage()
{
this.InitializeComponent();
CmdBar.ClosedDisplayMode = AppBarClosedDisplayMode.Hidden;
Timer = new DispatcherTimer(){Interval = TimeSpan.FromSeconds(5) };
Timer.Tick += (sender, args) => {
CmdBar.ClosedDisplayMode = AppBarClosedDisplayMode.Hidden;
Timer.Stop();
};
}
public async void OnPointerMoved(object Sender, PointerRoutedEventArgs e)
{
Timer.Stop();
CmdBar.ClosedDisplayMode = AppBarClosedDisplayMode.Compact;
Timer.Start();
}
Basically as #Evk said, you are creating a new timer every move of your mouse. So I declared a property for the timer and stop it then restart it when your mouse move.
Related
I am using C# programming language.
I have created a new borderless Windows Form in Visual Studio 2012. I have two panels. First panel (panel1) is docked at top and second panel (panel2) is docked at left and its Visible status is false. I also added a button (buttonMenu) in panel1 and docked it on left. What I am trying to achieve is that on buttonMenu click, panel2 slides from left to its original location (left dock) and when buttonMenu clicked again slides away.
I have tried to set the following code:
private void buttonMenu_Click(object sender, EventArgs e)
{
panel2.Visible = !panel2.Visible;
}
And it works but i am trying to make it slide with an animation.
The easier way to do it would be to create a timer and in its Tick event change the panel width like so
private Timer timer = new Timer();
bool closing = false;
int desiredWidth = 300
private void ConfigureTimer()
{
timer.Tick += timer_Tick;
timer.Interval = 16;
}
private void timer_Tick(object sender, EventArgs e){
{
if(closing)
{
panel2.Width-=15;
if(panel2.Width < 0)
{
panel2.Width = 0;
timer.Stop();
}
}else{
panel2.Width+=15;
if(panel2.Width >= desiredWidth)
{
panel2.Width = desiredWidth;
timer.Stop();
}
}
}
private void SwitchPanelState()
{
closing = !closing;
timer.Start();
}
But if you want to make you life easy and more object oriented, you should create an Control with the specified behavior and just add it in your form
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();
}
i have developed media player in WPF.but i did n't get timer for slider in that.how i will get that timer below is the my .cs file code.
i used dispatchertimer for timer.video is seeking but timer is not displaying with video.
seekbar not moving where i clicked in seekbar.plz help me .
thanks in advance.
DispatcherTimer timer;
public TimeSpan duration;
public Window1()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(400);
timer.Tick += new EventHandler(timer_tick);
Loaded += new RoutedEventHandler(Window_Loaded);
}
private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
{
if (mediaElement1.NaturalDuration.HasTimeSpan)
{
TimeSpan ts = mediaElement1.NaturalDuration.TimeSpan;
slider1.Maximum = ts.TotalSeconds;
slider1.SmallChange = 1;
slider1.LargeChange = Math.Min(10, ts.Seconds / 10);
}
timer.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
slider1.Value = mediaElement1.Position.TotalSeconds;
}
take a look at this
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
You can implement this but you will have to pass references to your controls/window because without having references to your controls, the thread/timer object cannot modify controls (ie text etc).
Hope that helps!
Edit: Sorry I misread, but definately check whether the timer youre using can make changes to your elements
I am trying to get my media controls to auto hide after 2 seconds. However the way I have it set up is that it only works when my mouse leaves the StackPanel I have the media controls in. And if I keep moving the in and out of the StackPanel then it will start to flicker as it is firing the hide even multiple times. I am unsure on how to go about this logically. Anyone have any tips or suggestions?
Here is what I got right now (StackPanel is named controls).
...
controls.MouseMove += new MouseEventHandler(control_unhide);
controls.MouseLeave += new MouseEventHandler(control_hide);
...
void control_hide(object sender, MouseEventArgs e)
{
var miniTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
miniTimer.Tick += (s, i) => { miniTimer.Stop(); controls.Opacity = 0; };
miniTimer.Start();
}
//Unhide controls
void control_unhide(object sender, MouseEventArgs e)
{
controls.Opacity = 100;
}
Also from some sample code I have seen people say to use Collapse and Visible to make the controls hide and reappear. This however doesn't work as the Collapse seems to make the boundaries unresponsive to the mouse entering.
Thanks!
**Edit
I asked this question because I spent a lot of time trying to figure this out yesterday only to sit down today and think of a really easy solution. What I did was this:
//global
private DispatcherTimer hideTimer;
....
//init
hideTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
hideTimer.Tick += (s, i) => { hideTimer.Stop(); controls.Opacity = 0; };
hideTimer.Start();
controls.MouseMove += new MouseEventHandler(control_unhide);
controls.MouseLeave += new MouseEventHandler(control_hide);
...
void control_hide(object sender, MouseEventArgs e)
{
hideTimer.Start();
}
//Unhide controls
void control_unhide(object sender, MouseEventArgs e)
{
controls.Opacity = 100;
hideTimer.Stop();
}
A simple solution would be to put in a guard variable and only hide if you weren't currently hiding:
bool currentlyHiding;
void control_hide(object sender, MouseEventArgs e)
{
if (!currentlyHiding)
{
var miniTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
miniTimer.Tick += (s, i) =>
{
miniTimer.Stop();
controls.Opacity = 0;
currentlyHiding = false;
};
miniTimer.Start();
currentlyHiding = true;
}
}
You will also need to do something similar for the unhide.
Is there any way to capture the time interval between mouse wheel scroll start and stop? Actually I want to capture the interval between the scrolling start and stop when I very quickly scroll the mouse wheel.
I have already looked at MouseWheel event but it don't fulfill my requirement. In senes that it always gives a value of Delta 120 or -120 but i want to call a function depending on the speed of the mouse scroll for example when i scroll the mouse normally i want to perform function 1 and when i scrolled the mouse very quickly i want to perform the function 2. In other words is there any way to distinguish between the mouse scroll high and normal speed.
Any advice will be appreciated.
can't you capture the mouse wheel events and see how long between them. Basically start a timer when you get a mouse wheel event and then in the next event see what the timer is at (and so how long has elapsed between the events) to determine the speed the wheel is being turned at? If the elapsedtime is smaller than a certain threshold, perform function2 and if it is faster than a certain threshold perform function 1.
You will probably have to set it to perform function 1 if the timer goes off in case they only do a single scroll.
In fact you might be able to do it this way:
start a timer (with an interval that indicates slow mouse wheeling) in the mouse wheel event, then if the timer goes off perform function 1. If the mouse wheel event happens again before the timer has gone off then reset the timer and increment a counter (to keep track of the number in wheel events since you did stuff) then start a second (longer) timer. if the counter is greater then a certain threshold perform function 2. When the second timer elapses, reset the counter. Something along those lines should give you the ability to fire function 1 when slow wheel turning and function 2 when the wheel is rapidly turned through a few 'clicks'.
this code should give a (very dirty) indication of the sort of thing I was thinking of. After playing a little I'm not really sure it's a good solution though....
private void mouseWheelHandler (object sender, MouseEventArgs e)
{
slowTimer.Enabled = false;
slowTimer.Stop ();
slowTimer.Interval = 200;
slowTimer.Start();
slowTimer.Enabled = true;
m_counter++;
Trace.WriteLine(string.Format("counter={0}", m_counter));
if (fastTimer.Enabled==false)
{
fastTimer.Enabled = true;
fastTimer.Interval = 150;
fastTimer.Start ();
}
if (m_counter>5)
{
Trace.WriteLine("called method 2");
m_counter = 0;
fastTimer.Stop ();
slowTimer.Enabled = false;
slowCheckTimer.Stop ();
slowCheckTimer.Interval = 250;
slowCheckTimer.Start();
slowCheckTimer.Enabled = true;
}
}
private void slowTimer_Tick(object sender, EventArgs e)
{
Trace.WriteLine("slow timer ticked");
if (slowCheckTimer.Enabled==false)
{
Trace.WriteLine ("called method 1");
}
slowTimer.Enabled = false;
}
private void fastTimer_Tick(object sender, EventArgs e)
{
fastTimer.Enabled = false;
Trace.WriteLine("fast timer ticked");
m_counter = 0;
fastTimer.Stop ();
}
private void slowCheckTimer_Tick(object sender, EventArgs e)
{
Trace.WriteLine("slow check timer ticked");
slowCheckTimer.Stop ();
slowCheckTimer.Enabled = false;
}
Take a look at the Control.MouseWheel event.
As suggested by the Sam Holder i am posting here a modified verion of his advice to help other programmers facing the same problem.
public partial class Form1 : Form
{
int m_counter = 0;
public Form1()
{
InitializeComponent();
// Attach Mouse Wheel Event
this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
}
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
// Refresh Slow Timer
slowTimer.Enabled = false;
slowTimer.Stop();
slowTimer.Interval = 150;
slowTimer.Start();
slowTimer.Enabled = true;
// Incremenet counter
m_counter++;
// Start Fast Timer
if (fastTimer.Enabled == false)
{
fastTimer.Enabled = true;
fastTimer.Interval = 50;
fastTimer.Start();
}
// If this returns true call
// the fast scroll implementation
if (m_counter > 4)
{
Console.WriteLine("Quick Method Called");
m_counter = 0;
fastTimer.Stop();
slowTimer.Enabled = false;
slowCheckTimer.Stop();
slowCheckTimer.Interval = 200;
slowCheckTimer.Start();
slowCheckTimer.Enabled = true;
}
}
private void slowTimer_Tick(object sender, EventArgs e)
{
if (slowCheckTimer.Enabled == false)
{
Console.WriteLine("Slow Method Called");
}
slowTimer.Enabled = false;
}
private void fastTimer_Tick(object sender, EventArgs e)
{
fastTimer.Enabled = false;
m_counter = 0;
fastTimer.Stop();
}
private void slowCheckTimer_Tick(object sender, EventArgs e)
{
slowCheckTimer.Stop();
slowCheckTimer.Enabled = false;
}
}