Get User Input from a Slider : Slider « Windows Presentation Foundation « C# / C Sharp






Get User Input from a Slider

Get User Input from a Slider
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="200" Width="300">
    <StackPanel>
        <TextBlock Margin="5" Text="0" FontSize="20" 
                   HorizontalAlignment="Center" Name="txtSliderValue" />
        <Slider LargeChange="10" Margin="5" Maximum="1000" Minimum="0" 
                Name="slider1" TickPlacement="TopLeft" 
                Ticks="100, 200, 400, 800" Value="0"
                ValueChanged="slider_ValueChanged" />
        <Button Name="btnGetSliderValue1" Width="100" 
                Click="GetSliderValue_Click">Get Slider 1 Value</Button>
        <Slider IsSnapToTickEnabled="True" Margin="5" Maximum="1000" 
                Minimum="0" Name="slider2" TickFrequency="25" 
                TickPlacement="BottomRight" Value="1000" 
                ValueChanged="slider_ValueChanged" />
        <Button Name="btnGetSliderValue2" Width="100" 
                Click="GetSliderValue_Click">Get Slider 2 Value</Button>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void GetSliderValue_Click(object sender, RoutedEventArgs e)
        {
            Button button = e.OriginalSource as Button;
            string message = "Unknown slider.";

            if (button == btnGetSliderValue1)
            {
                message = "Slider1 value = " + slider1.Value;
            }
            else if (button == btnGetSliderValue2)
            {
                message = "Slider2 value = " + slider2.Value;
            }

            MessageBox.Show(message, Title);
        }
        private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Slider slider = e.OriginalSource as Slider;

            if (slider != null)
            {
                txtSliderValue.Text = slider.Value.ToString();
            }
        }
    }
}

   
    
  








Related examples in the same category

1.Use Slider to control the ScaleTransformUse Slider to control the ScaleTransform
2.Use Slider to control SkewTransformUse Slider to control SkewTransform
3.Use Slider to control TranslateTransformUse Slider to control TranslateTransform
4.Use Slider to control the TransformationUse Slider to control the Transformation
5.Bind Slider value to TextBlockBind Slider value to TextBlock
6.Use Slider to control the BlurUse Slider to control the Blur
7.Use Slider to control Drop ShadowUse Slider to control Drop Shadow
8.Use Slider to control the EmbossUse Slider to control the Emboss
9.Use Slider to control the GlowUse Slider to control the Glow
10.Use Slider to control the BevelUse Slider to control the Bevel
11.Vertical/Horizontal SliderVertical/Horizontal Slider
12.TickPlacement and TickFrequency for Slider
13.Binding ProgressBar with SliderBinding ProgressBar with Slider
14.Two way data binding between Slider and ProgressBarTwo way data binding between Slider and ProgressBar
15.Set Minimun/Maximum value for SliderSet Minimun/Maximum value for Slider
16.Use Slider to control Path ScalingUse Slider to control Path Scaling
17.Add a slider control and a border control to the content of the StackPanel, and add a canvas to the border control.Add a slider control and a border control to the content of the StackPanel, and add a canvas to the border control.
18.Slider AttributesSlider Attributes
19.slider value changed eventslider value changed event