Update mediaElement based on slider value - c#

I have a mediaElement and a slider.
The slider's maximum value is video's duration in seconds (for example if the video is 2 minutes, the slider's value is 120).
I want to update the mediaElement.Position based on the slider's value but the problems is that I dont want to update the position until the user FINISHED manipulating the value.
so what I did is 2 functions:
private void DurationSlider_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
SeekToMediaPosition();
}
this function applys if the user stopped manipulating the slider.
private void DurationSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
SeekToMediaPosition();
}
this function applys if the user clicked on different value in the slider.
The problem is that they clash.
Manipulating the slider cause the value to be changed...
so What I did is added this function:
bool manipulating = false;
private void DurationSlider_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
manipulating = true;
}
and in the ValueChanged function I checked whether manipulating = true or not
It solved half of the problem - it does not change the video position while manipulating (until I finish) but if I click on different position on the slider (without releasing the mouse) and continue manipulating the slider's value - it changes the video position to where I clicked on the slider and then changes again to where I ended the manipulation.
So whats wrong?
I want to change the video position ONLY when the user released the mouse.
I cant find event that fires what I want...

You could use PointerCaptureLost Event to determine the position after the user finished dragging the control.
<Slider PointerCaptureLost=="Slider_PointerCaptureLost"
Height="27" Margin="132,162,0,0" VerticalAlignment="Top" Width="303"/>
Then in code behind.
private void Slider_PointerCaptureLost(object sender, DragCompletedEventArgs e)
{
Slider s = sender as Slider;
// Your code
MessageBox.Show(s.Value.ToString());
}

Related

Using MouseHover event and ToolTip

To show the relevant information(in monopoly game, the property belongs which player, current market price etc.), I put a Label on the top of a panel, and used a ToolTip object to display the information. This is the image of my current setup.
Here are the steps I have done:
1.Added MouseHover event handler (The Label name is MEDITERANEAN)
this.MEDITERANEAN.MouseHover += new System.EventHandler(this.MEDITERANEAN_MouseHover);
2.Initialized Tooltip
private void InitializeToolTip()
{
toolTipLabel.ToolTipIcon = ToolTipIcon.Info;
toolTipLabel.IsBalloon = true;
toolTipLabel.ShowAlways = true;
}
3.Call setToolTip() in MouseHover call back function
private void MEDITERANEAN_MouseHover(object sender, EventArgs e)
{
toolTipLabel.SetToolTip(MEDITERANEAN, "You put mouse over me");
rolledDice.AppendText("Mouse Over");
}
But when I start application and move my cursor over the label, there is no text from toolTipLabel. What part did I make mistakes?
Interestingly, i made other function and it works.
private void panelBoard_MouseOver(object sender, EventArgs e)
{
toolTipLabel.SetToolTip(panelBoard, "You put mouse over me");
rolledDice.AppendText("Mouse Over");
}
I think you just need to bring your lable control in front of image. Try something like this .
MEDITERANEAN.BringToFront();
I found the solution, first I should set Panel's property "Enable" to true, then set label's property "visible" to true as well.

Check time after a mousebuttondown before the mousebuttonup

I think that must be only a little problem, but I can't get a clear thought on that. Someone an idea?
I have some borders on a canvas (filled with images) and i want to click the border (i do this with the OnMouseLeftButtonDown) where the border gets red (so the user knows for sure which object he had clicked) and then, after 1 or 2 seconds, when the mousebutton is still pushed down, a drag'n'drop should start.
At first I had the borders inside buttons, but the clickevent seems to conflict with the dragevent. So I got rid of the buttons and did everything inside the borders directly, which works well also. But how can I start the drag after the mousebuttondown and stop it when the mousebuttonup happens before the time runs out.
Someone an idea for a clean solution?
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_source = sender as Border;
Mouse.Capture(_source);
}
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_source = null;
Mouse.Capture(null);
}
and in event OnMouseMove you can modify border margins depends on mouse position but after checking if _source is type of Border.
var position = e.GetPosition(Canvas);
EDIT:
For that time you can add property Stopwatch _watch, and inside OnMouseLeftButtonDown event handler you can do it:
_watch = Stopwatch.StartNew();
in OnMouseLeftButtonUp:
_watch.Stop();
and in OnMouseMove before your code:
while (_watch.ElapsedMilliseconds < 2000 && _watch.IsRunning)
{
Thread.Sleep(100);
}

WP7, C#, Increase slider value by holding a button

I have to increase the slider control value while holding a button.
As long as I am holding the button the slider has to keep increasing.
For example, the scenario is typical with volume control.
I have a slider for volume and a button Increase for volume.
Now as long as I keep holding the Increase button, the volume (marker) in the slider should keep increasing continuously till I release the button.
What I have achieved is changing the value of the slider on individual click events on the button.
Kindly give your suggestions on how I can achieve this.
According to the book there should be RepeatButton. In this case it will perfectly suit your needs. Try to avoid Thread sleeps. Freezes are not pretty good thing. It's one of first candidates for refactoring.
public bool Ok = false;
public void Do()
{
while (Ok)
{
this.Text += ".";
System.Threading.Thread.Sleep(100);
Application.DoEvents();
//I added dots to the form text , You do your own mission
}
}
private void btnLouder_MouseDown(object sender, MouseEventArgs e)
{
Ok = true;
Do();
}
private void btnLouder_MouseUp(object sender, MouseEventArgs e)
{
Ok = false;
}
Add a TrackBar control to the form and then
Replace the code in While block with this one, it does exactly what you want :
if(trackBar1.Value < trackBar1.Maximum)
trackBar1.Value += 1;

C# Fade-in and fade-out transparency when form is being moved

I have the code to make the form partially transparent when is being moved, but I want to know if it's possible to add fade-in and fade-out effects when I start moving and stop moving the form.
EDIT
The code I am using to add transparency to the form is:
bool canMove = false;
private void Form1_Load(object sender, EventArgs e)
{
canMove = true;
}
private void Form1_Move(object sender, EventArgs e)
{
if (canMove)
{
this.Opacity = 0.7;
}
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
this.Opacity = 1;
}
You should use a Timer control, set the opacity in timer's tick event. until the form stops moving (define a variable like isMoving and set it to true/false based on form's status).
You can find an example of setting opacity in timer's tick event in my article about a fading label. Use Google translator to read it.
Hope this helps.
You could take a Timer control, you could then start the timer when form starts moving and set the transparency of the form to some value, and on each tick of the Timer, make the transparency to decrease and on some value make it to increase. If you want to have the fadein fadeout effect when form stops moving, you can do the same when the form has moved.

How do I set a ScrollBar's value when it grows dynamically?

I'll start by what I'm trying to have happen:
I have data loaded in a range, where, say, scrolling all the way to the left puts me on April 1, and scrolling all the way to the right puts me on June 1.
The user positions the scrollbar on April 1st, and clicks the left arrow on the scrollbar. Now the scrollbar is positioned at March 31, and the range of data now spans from March 1-June 1.
Here's my problem:
I have been handling the left-arrow-click in the Scroll event handler (roughly as follows):
private void horizontalScroll_Scroll(object sender, ScrollEventArgs e)
{
if (LeftArrowClicked())
{
horizontalScroll.Maximum = calculateNewMaximum(earliestDate, latestDate);
horizontalScroll.Value = calculateNewPosition(currentDate.AddDays(-1), earliestDate);
}
}
Stepping through with the debugger, the moment it leaves this event handler, horizontalScroll.Value drops to 0, while horizontalScroll.Maximum stays at the correct value.
I'll post back later with any clarifications, and answers to questions.
Try setting e.NewValue instead of horizontalScroll.Value. The control will then respect this value when executing its own logic.
It's clearly being caused by the ScrollableControl setting the value after the Scroll event is fired. You could try extending the control you are using and overriding the OnScroll virtual method.
protected override void OnScroll(ScrollEventArgs se)
{
base.OnScroll(se);
// Do stuff now
}
Edit
You should probably be aware that clicking a scroll bar's buttons does not raise the Scroll event, it only raises the ValueChanged event. (Using the mouse generates both though.)
Edit Again
Ah ha! I knew there was a way to do it. What you want to do instead of changing horizontalScroll.Value, you want to set the NewValue on the ScrollEventArgs parameter. This should work:
private void horizontalScroll_Scroll(object sender, ScrollEventArgs e)
{
if (LeftArrowClicked())
{
horizontalScroll.Maximum = calculateNewMaximum(earliestDate, latestDate);
e.NewValue = calculateNewPosition(currentDate.AddDays(-1), earliestDate);
}
}

Categories