Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make TextBlock.Text = Slider.Value on slider value change

I've tried this

   If Slider1.Value = 1 Then
        TextBlock1.Text = "1"
    End If
    If Slider1.Value = 2 Then
        TextBlock1.Text = "2"
    End If
    If Slider1.Value = 3 Then
        TextBlock1.Text = "3"
    End If
    If Slider1.Value = 4 Then
        TextBlock1.Text = "4"
    End If
    If Slider1.Value = 5 Then
        TextBlock1.Text = "5"
    End If

I actually get a couple of errors with this, especially when i have the

    If Slider1.Value = 1 Then
    TextBlock1.Text = "1"
    End If

code because it's already on value 1 when the program runs. I'm new to WPF and don't really know what to do here so i you could please show me or provide a code sample on how it's done thanks.

and also i have been using expression blend 4 for a couple of days and i know how to create a template and animate fades on mouse over and stuff for a button but say if the user clicks a button how would i animate a separate picturebox or image to fade in or fade out could you please provide me an example thankyou everyone :).

like image 237
happycamper1221 Avatar asked Nov 21 '25 10:11

happycamper1221


1 Answers

You don't need to do this in Codebehind just bind to your value in XAML and if you want to modify that value somehow, then use a ValueConverter.

see http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

  <Slider x:Name="mySlider"/>
  <TextBox x:Name="myTextBox" Text="{Binding ElementName=mySlider,Path=Value}"/>

Otherwise if you definitely want to do it in CodeBehind use the ValueChangedEvent:

XAML:

  <Slider x:Name="mySlider" ValueChanged="mySlider_ValueChanged"/>

CodeBehind

Private Sub mySlider_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double))
    myTextBox.Text = e.NewValue.ToString()
End Sub
like image 87
SvenG Avatar answered Nov 24 '25 04:11

SvenG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!