Autohide Media Controls SilverLight Player - c#

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.

Related

Make picture box move across screen during runtime

I am using Windows Forms (.NET Framework) and am trying to make a picture box move a cross a screen.
I have tried using timers and this while loop but the image (it's supposed to be a plane) does not appear in the case of the while loop and the use of timers makes it difficult to remove past picture Boxes so they appear to generate a sequence of planes. How can I accomplish this?Does it have something to do with Sleep()?
private void Button1_Click(object sender, EventArgs e)
{
//airplane land
//drawPlane(ref locx, ref locy);
//timer1.Enabled = true;
while (locx > 300)
{
var picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(30, 30),
Location = new System.Drawing.Point(locx, locy),
Image = Properties.Resources.plane2, //does not appear for some reason
};
this.Controls.Add(picture);
Thread.Sleep(500);
this.Controls.Remove(picture);
picture.Dispose();
locx = locx - 50;
}
You can use a "Timer" to change the position of the PictureBox regularly.
Here is a simple demo that using Timer Class you can refer to.
public partial class Form1 : Form
{
private System.Timers.Timer myTimer;
public Form1()
{
InitializeComponent();
myTimer = new System.Timers.Timer(100);
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
myTimer.AutoReset = true;
myTimer.SynchronizingObject = this;
}
private void myTimer_Elapsed(object sender, ElapsedEventArgs e)
{
pictureBox1.Location = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y);
}
private void btStart_Click(object sender, EventArgs e)
{
myTimer.Enabled = true;
}
}
The result,

DispatcherTimer stacking - UWP

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.

Easy way to identify button and auto load its target

I have this portion of code
przyciski[i] = new Button();
przyciski[i].Visible = false;
przyciski[i].Name = "przycisk" + i;
przyciski[i].Click += new System.EventHandler(ButtonClickHandler);
which is describing dynamicly created button, and this eventhandler underneath the program
private void ButtonClickHandler(object sender, EventArgs e)
{
Button btn = (Button)sender;
if(btn.Name == "przycisk1")
{
//Open specific JPEG in external aplication
}
}
Is there any quicker way to identify button and its target?
Here's another option. If you're going to perform a different piece of code for every button anyway, why bother giving them a name and then having to detect the name in the click event?
Just create your buttons, and then specify what each should do.
var przyciski = new List<Button>();
for (var i = 0; i < 5; i++)
przyciski.Add(new Button { Visible = false });
przyciski[0].Click += (s, e) => { /* Do something */ };
przyciski[1].Click += (s, e) => { /* Open specific JPEG in external aplication */ };
przyciski[2].Click += (s, e) => { Console.WriteLine("You clicked button 2."); };
przyciski[3].Click += (s, e) => { };
przyciski[4].Click += (s, e) => { };
If przyciski is an instance variable, you can check for reference equality:
private void ButtonClickHandler(object sender, EventArgs e)
{
if (sender == przycisk[1])
{
//Open specific JPEG in external aplication
...
}
...
}

Text animation in Windows Forms

I was wondering if there was a way of adding a sort of animation to text displayed on a form.
What I had in mind when I thought of this was kind of similar to what you can do with text in PowerPoint (i.e. a typewriter-like animation where the text is typed one at a time, have the whole textbox appear with a certain effect etc), I'm just looking to find out what you can do using Windows Forms.
Currently I'm using a textbox to display information on my form application, though in hindsight I realise labels would have worked just as well.
EDIT: Turns out I was using labels after all, I just gave it a name with 'textbox' inside for lack of a better description.
public partial class Form1 : Form
{
int _charIndex = 0;
string _text = "Hello World!!";
public Form1()
{
InitializeComponent();
}
private void button_TypewriteText_Click(object sender, EventArgs e)
{
_charIndex = 0;
label1.Text = string.Empty;
Thread t = new Thread(new ThreadStart(this.TypewriteText));
t.Start();
}
private void TypewriteText()
{
while (_charIndex < _text.Length)
{
Thread.Sleep(500);
label1.Invoke(new Action(() =>
{
label1.Text += _text[_charIndex];
}));
_charIndex++;
}
}
}
Now, I personally wouldn't do this because gratuitous animations tend to annoy users. I'd only use animation sparingly - when it really makes sense.
That said, you can certainly do something like:
string stuff = "This is some text that looks like it is being typed.";
int pos = 0;
Timer t;
public Form1()
{
InitializeComponent();
t = new Timer();
t.Interval = 500;
t.Tick += new EventHandler(t_Tick);
}
void t_Tick(object sender, EventArgs e)
{
if (pos < stuff.Length)
{
textBox1.AppendText(stuff.Substring(pos, 1));
++pos;
}
else
{
t.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
pos = 0;
textBox1.Clear();
t.Start();
}
or something like that. It'll tick off ever half second and add another character to the multi-line text box. Just an example of what someone could do.

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

Categories

Resources