Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have an slider to display the time selected into a textblock with format 00:00:00 and to send it with format 000000.00 as a string.

C#
<WrapPanel  Margin="5" Orientation="Horizontal" x:Name="timeRow" >
            <Label TextOptions.TextFormattingMode="Ideal" Width="140" Foreground="#2B3856" FontWeight="Bold" FontSize="16"   Margin="3">Time </Label>
            <Slider HorizontalAlignment="Left" x:Name="timeSlider_15" 
                 Margin="3"
                 VerticalAlignment="Top"
                 Width="250"
                 ValueChanged="Slider_TimeValueChanged" Minimum="0" Maximum="24" TickFrequency="1" AutoToolTipPlacement="BottomRight" />
            <TextBlock TextOptions.TextFormattingMode="Ideal" Width="75"  Foreground="#2B3856"  FontSize="16" Text="00:00:00" x:Name="TimeText" ></TextBlock>
          </WrapPanel>
        </WrapPanel>



C#
private void Slider_TimeValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
      {
          var slider = sender as Slider;
          TimeSpan Time = TimeSpan.FromHours(slider.Value);
          double tiest = slider.Value ;
          this.TimeText.Text = Time.ToString("hh\\:mm\\:ss");
          ///Create MSO sentence
          int index = Convert.ToInt32(Regex.Match(slider.Name, @"\d+").Value);
          //......
          double timeconverted = slider.Value * 10000;
          sentenceToSent.SetField(index, Time.ToString("hh\\:mm\\:ss\\.ff").Replace(":", ""));

          _myTrucker.Bridge.SendTime(port, host, sentenceToSent.ToString());
      }

Formats are now undercontrol. My problem now is the precision of the selection. With my code when you move the slider the textbox doesn't shows values incresing regulari like 00:00:01 , 00:00:02 ,... , Instead values grows like 00:06:13 , 00:12:28 ,...How can I solve it preserving my formats?
Posted

1 solution

Change Maximum to "86400".
That is 24 hours in seconds.
This will give you a granularity of 1 second.

Please have a look at the example below:

The conversion from seconds to e.g. 12:22:45
will be done by a value converter.

TimeConverter.cs
C#
using System;
using System.Windows.Data;

namespace WpfApplicationSliderTest
{
    class TimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string timeString = string.Empty;

            int seconds = System.Convert.ToInt32((double)value);

            TimeSpan ts = new TimeSpan(0, 0, seconds);

            timeString = ts.ToString(@"hh\:mm\:ss");

            return timeString;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}


MainWindow.xaml:
C#
<Window x:Class="WpfApplicationSliderTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplicationSliderTest"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.Resources>
        <local:TimeConverter x:Key="tc"></local:TimeConverter>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical">
            <Slider x:Name="slider" Minimum="0" Maximum="86400" SmallChange="1" LargeChange="1" TickFrequency="1" IsSnapToTickEnabled="True" Margin="10"></Slider>
            <TextBlock Text="{Binding ElementName=slider,Path=Value,Converter={StaticResource tc}}"></TextBlock>
        </StackPanel>
    </Grid>
</Window>


Dragging the slider does not show the real granularity.
The steps seem to be bigger.

I added SmallChange="1". When you use the arrow up/down keys on your keyboard
you see each key press is changing the value by 1.

I also added LargeChange="1". When you click left or right of the slider button
the value is also increased/decreased by 1.
 
Share this answer
 
v5
Comments
Kinna-10626331 11-Dec-14 9:28am    
Thanks but still growing with granularity of 6 seconds doing that.
TheRealSteveJudge 11-Dec-14 9:50am    
You're welcome.
I think this behaviour is by design of the slider control.
I added SmallChange="1". When you use the arrow up/down keys on your keyboard
you see each keypress is changing the value by 1.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900