3

I have this simple slider project. I wanna show slider value in TextBox.

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
     <TextBox x:Name="sliderValue" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
     <Slider x:Name="slider" Minimum="0" Maximum="20" Value="5" ValueChanged="slider_ValueChanged"/></Grid>

        // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
            var slider = sender as Slider;
            string value = string.Format("{0}", slider.Value);
            sliderValue.Text = value;
    }

In last line I am getting this error:

System.NullReferenceException: Object reference not set to an instance of an object. at slider_test.MainPage.slider_ValueChanged(Object sender, RoutedPropertyChangedEventArgs`1 e) at .......

Could anyone explain me what is the problem? Thank you.

petrppe
  • 145
  • 1
  • 14
  • You are trying access null object. Please check for null slider object after you cast. var slider = sender as Slider; if(slider != null) { string value = string.Format("{0}", slider.Value); sliderValue.Text = value; } – Sampath Apr 10 '14 at 09:18
  • Slider is not null but I got the same error. TextBox sliderValue is null though. – petrppe Apr 10 '14 at 09:26
  • @petrppe Update code string value = string.Format("{0}", slider.Value); by string value = slider.Value.ToString(); debug code and see result. IF I guess right prblem in your string format. – Jaihind Apr 10 '14 at 09:32
  • @Jaihind I tried but same error. – petrppe Apr 10 '14 at 09:43
  • @petrppe Use e.Newvalue istead of slider value. I geva an answer give a look. – Jaihind Apr 10 '14 at 10:05
  • @Jaihind Thanks, but no progress again. I guess there must be some problem with that TextBox... – petrppe Apr 10 '14 at 10:08
  • @petrppe Seems Ok Textbox. I made update in my ans give a look. mat there is some cross thread problem. – Jaihind Apr 10 '14 at 11:06
  • @Jaihind Wow! Such a little thing and "so complicated" solution. I have never heard about Dispatcher class. I guess I should learn about it. Thank you very much! – petrppe Apr 10 '14 at 11:23
  • @petrppe Welcome bro :) – Jaihind Apr 10 '14 at 11:31

3 Answers3

3

You can do it using XAML by Binding slider value to TextBlock text.

        <Slider x:Name="SliderValueText" ValueChanged="SliderValueText_ValueChanged"
                VerticalAlignment="Top"
                Width="440"
                Minimum="0"
                Maximum="20"
                Value="5" />
        <TextBlock Height="30"
                   Text="{Binding Value,  ElementName=SliderValueText}"
                   VerticalAlignment="Top"
                   HorizontalAlignment="Center" />


private void SliderValueText_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e)
{
    string value = string.Format("{0}", e.NewValue);
    MessageBox.Show(value);
}

It is one of the option. Here you need the slider value. So I am storing in the variable "value". That's what I am displaying in MessageBox. But it is least case to try...

Kumar
  • 901
  • 1
  • 18
  • 44
  • I know about this way but I can't use this because I need to work more with value later and put some other string to textbox and so on. – petrppe Apr 10 '14 at 09:45
  • Yes, but still I need to set that "value" variable to the text property of textbox and do that inside slider_ValueChanged method. – petrppe Apr 10 '14 at 10:26
  • So... YOu don't want to use Bind the slider value using XAML, lLike this. `Text="{Binding Value, ElementName=SliderValueText}"` – Kumar Apr 10 '14 at 10:28
2

I am not sure but may be this will help you.

private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        { 
          Dispatcher.BeginInvoke(() =>
                {
                    string value = string.Format("{0}",  e.NewValue);
                    sliderValue.Text = value;
                }); 

        }
Jaihind
  • 2,762
  • 1
  • 10
  • 19
0

In this way use e.NewValue for polling changing a value:

private void slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        Debug.WriteLine("Value changed and value is " + e.NewValue.ToString());  
    }

But for labels simply use XAML bindings:

<TextBlock x:Name="someLabel"  
           Text="{Binding ElementName=slider, Path=Value}"
/>