Search code examples
wpfvb.netsliderwpf-controls

Slider Control Update Label Based on Value Error on Slider_ValueChanged


Trying to do something super simple and I am not getting it. New to this programming stuff a bit. I have a slider control. I have two labels. One I have bound to the value of the slider to show the chosen value. I have another label that I have verbiage in. If the slider value is 1 I want it to say 'Player'. If it's greater than 1 I want it to say 'Players'

  <Viewbox Grid.Column="1" Margin="10">
  <Slider x:Name="sliderhunter" Minimum  = "1" Maximum = "8" Width="200" TickFrequency = "1" Value="2" 
   TickPlacement = "BottomRight" ValueChanged = "sliderhunter_ValueChanged" Margin = "10" IsMoveToPointEnabled="True" IsSnapToTickEnabled="True"/>


  </Viewbox>
  <TextBox Grid.Column="1" IsHitTestVisible="False"  Foreground="White" Text="{Binding Value, ElementName=sliderhunter, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Left" Height="68" VerticalAlignment="Top" FontSize="50" Background="Transparent" HorizontalContentAlignment="Left" BorderBrush="Transparent" Margin="43,34,469,0" />
  <Label Grid.Column="1" IsHitTestVisible="False" x:FieldModifier="Public"   Content="Hunters" HorizontalContentAlignment="Left" Height="80" VerticalAlignment="Top" FontSize="50" Background="Transparent" BorderBrush="Transparent" Margin="95,34,343,0" x:Name="txtHunter" />
               

And my simple code behind.

  Private Sub sliderhunter_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double)) Handles sliderhunter.ValueChanged
        If e.NewValue > 1 Then

            txtHunter.Content = "Players"
        Else
            txtHunter.Content = "Player"
        End If

    End Sub

It gets hung up on the else with the 'Object reference not set to an instance of an object.' I don't get that, either. I have the value set to 2 so the else shouldn't even come into it, yet. I have spent the last two hours on this. If you can help, please use crayons in your explanation. Like I said, still trying to learn all this stuff.

@emoacht

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=GameProject
  StackTrace:
   at GameProject.winSetGameSettings.sliderhunter_ValueChanged(Object sender, RoutedPropertyChangedEventArgs`1 e) in C:\Users\mgobl\source\repos\GameScoreBoard\GameScoreboard\winSetGameSettings.xaml.vb:line 2571

Here is the error screen: Error Details


Solution

  • To answer your question why there's Object reference not set to an instance of an object exception, it's simply because XAML language is also order sensitive language.

    What means if You'd put sliderhunter slider after txtHunter lable there won't be any exception, beacaue when ValueChanged on slider is called txtHunter is already initialized.

    But in general if You want to make this app 'the right way', I'd suggest dig in MVVM pattern and move the logic from code behind to the ViewModel.

    There's a ton of examples and tutorials about MVVM, here's first link that pops up under MVVM and WPF search: https://intellitect.com/blog/getting-started-model-view-viewmodel-mvvm-pattern-using-windows-presentation-framework-wpf/