Answered by:
Label = Something if checkbox is checked > Issues.

Question
-
Sorry if this is simplistic - First time attempting to write a Windows Store App and I haven't used Visual Basics that much / in a long time.
What I'm trying to do, is when a checkbox is checked, have a word (or string) added to the text of a label/textbox.
Thus far I have: (Sorry if formatting looks weird too)
Public SundayChk As CheckBox
Dim Sunday As String = ""
Private Sub SundayChk_Checked(sender As Object, e As RoutedEventArgs)
If SundayChk.IsChecked = True Then
Sunday = "Sunday"
Else
Sunday = ""
End If
End SubPrivate Sub ClassDaysValue_SelectionChanged(sender As Object, e As RoutedEventArgs)
ClassDaysValue.Text = Sunday
End Sub(Next I have to figure out how to get the data from a DatePicker and TimePicker into a string so I can pass that into a Label/Text box as well - if you have any insight into that it'd also be appreciated)
Thanks for any help/insight you may be able to provide.
- Edited by AvenZrk Wednesday, May 21, 2014 5:49 AM Formatting update
Wednesday, May 21, 2014 5:47 AM
Answers
-
Does the app create the SundayChk object? Normally this would be created in Xaml, but it looks like you declare it in code but don't actually new it up.
Here's a quick mock-up of binding a TextBlock to a Checkbox with a ValueConverter that sets a string or blank based on the Checkbox's state. The only code is the ValueConverter. The rest is Xaml.
Xaml:
<Page.Resources> <local:BoolToStringParam x:Name="CheckedConverter" /> </Page.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock x:Name="ClassDaysValue" Text="{Binding ElementName=SundayChk, Path=IsChecked, Converter={StaticResource CheckedConverter},ConverterParameter=Sunday}"/> <CheckBox x:Name="SundayChk">Check for Sunday</CheckBox> </StackPanel> </Grid>
Code:
Public Class BoolToStringParam Implements IValueConverter Public Function Convert(value As Object, targetType As System.Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert If CBool(value) Then Return parameter Else Return "" End If End Function Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack Throw New NotImplementedException() End Function End Class
- Marked as answer by Jamles HezModerator Thursday, May 29, 2014 9:39 AM
Friday, May 23, 2014 1:46 AMModerator
All replies
-
What object is ClassDaysValue? I am feeling that it's a listview.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Wednesday, May 21, 2014 2:08 PMModerator -
You can use a ValueConverter to convert data from one type to another when data binding.
This will allow both converting the SundayChked.IsCheĉked property to a string or Visibility for your Sunday text and your date and time results.
See the remarks in the ValueConverter documentation, the Data binding overview, and the XAML data binding sample
Wednesday, May 21, 2014 2:13 PMModerator -
@ Matt Small - ClassDaysValue is a TextBlock but I didn't Dim ClassDaysValue as Textbox > I just tried it and it threw an error.
@ Rob Caplan - This seems interesting and would probably fix my Date and TimePicker if i'm not mistaken?
I am curious however as to why setting a String value then setting the text equal to that value doesn't work as shown above - just so I know what I'm doing wrong in the future.
I will try and utilize the ValueConverter and get back to you.
Thank you both.
Wednesday, May 21, 2014 2:32 PM -
Does ClassDaysValue_SelectionChanged ever fire? I suspect the code to set ClassDaysValue.Text = Sunday never gets called. That's probably not the event you want.
If you're setting this in code rather than data binding then you probably want to set it when you change the text in SundayChk_Checked
Wednesday, May 21, 2014 2:49 PMModerator -
Rob, you were right - I hadn't called it so it wouldn't even run, however; even now it still doesn't work.
I've altered my code to just apply the text whenever it is true/false respectively (Not sure if I need the Return line though?)
I now get
System.NullReferenceException was unhandled by user code
Code:
Public Function SundayChk_Checked(ClassDaysValue) As Object
'//Determines whether Sunday Checkbox (SundayChk) is marked or not
'//Then Assigns String to (ClassDaysValue) based on True/False
'//SundayChk is Dim'd as CheckboxIf SundayChk.IsChecked = True Then
ClassDaysValue.Text = "Sunday"
Else
ClassDaysValue.Text = "Blank"
End If
Return ClassDaysValue.Text'//This Function is called by the (Update) button
End FunctionPublic Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
'//(Update) button calls all functions in relation to updating (Class Informaiton) Heading under (Class_Edit_Hub)SundayChk_Checked(ClassDaysValue)
End SubI keep getting a lot of those NullReferenceExceptions - is there an easy way to dump them into an error log or something I'm blatantly doing wrong that I can fix?
Your continued patience and assistance is appreciated,
Thanks.
Wednesday, May 21, 2014 6:48 PM -
Does the app create the SundayChk object? Normally this would be created in Xaml, but it looks like you declare it in code but don't actually new it up.
Here's a quick mock-up of binding a TextBlock to a Checkbox with a ValueConverter that sets a string or blank based on the Checkbox's state. The only code is the ValueConverter. The rest is Xaml.
Xaml:
<Page.Resources> <local:BoolToStringParam x:Name="CheckedConverter" /> </Page.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock x:Name="ClassDaysValue" Text="{Binding ElementName=SundayChk, Path=IsChecked, Converter={StaticResource CheckedConverter},ConverterParameter=Sunday}"/> <CheckBox x:Name="SundayChk">Check for Sunday</CheckBox> </StackPanel> </Grid>
Code:
Public Class BoolToStringParam Implements IValueConverter Public Function Convert(value As Object, targetType As System.Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert If CBool(value) Then Return parameter Else Return "" End If End Function Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack Throw New NotImplementedException() End Function End Class
- Marked as answer by Jamles HezModerator Thursday, May 29, 2014 9:39 AM
Friday, May 23, 2014 1:46 AMModerator