Jonathan

Jonathan

  • NA
  • 3
  • 0

Problem with the "TextChanged" event contained within a UserControl

Mar 18 2010 4:42 PM



Hi,

I am building a UserControl that is made up of several TextBox. I want some labels to change automatically as the user inputs text within any of the textboxes. I have succeeded at applying several events (validations and auto-selections) to every textbox using the following:

 
 

<
UserControl x:Class="PhaseCalculator"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
   <UserControl.Resources>
      <Style TargetType="{x:Type TextBox}">
         <EventSetter Event="PreviewTextInput" Handler="Validation_DigitOnly" />
         <EventSetter Event="LostFocus" Handler="Validation_NotEmpty" />
         <EventSetter Event="MouseDoubleClick" Handler="TextBox_GotFocus" />
         <EventSetter Event="GotKeyboardFocus" Handler="TextBox_GotFocus" />
         <EventSetter Event="PreviewMouseLeftButtonDown" Handler="TextBox_GotFocusSelective" />

      </Style>
   </UserControl.Resources>
(...)
</UserControl>



So far, everything works perfectly when I use the UserControl in my application. However, I cannot seem to do anything within the "TextChanged" event that has a reference to another control within the UserControl. As soon as I reference a control within the sub that is triggered by a TextChanged event, the application that references the UserControl gives me a "Cannot create an instance of PhaseCalculator" until I comment out the control references.
Consider the following addition to the code above:

 
(in UserControl.Resources.Style, above)
      <EventSetter Event="TextChanged" Handler="Refresh" />
 
 
(in the content part of the UserControl)

<StackPanel>
     
   <TextBox Name="Hours" />
   <Label Name="TotalHours" />
</StackPanel>
 

(In the code-behind file)
 
Private Sub Refresh(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
   TotalHours.Content = Hours.Text * 40
End Sub



Adding the above code will result in an error message in my application although the UserControl builds fine. However if I comment out the "TotalHours.Content" line, the application starts working again. I tried adding "MsgBox("test")" to the Refresh() sub to see if the event triggers, and it does. However, as soon as the Refresh() sub has to access any control (apart from "sender"), the application cannot build anymore.

I have tried several workarounds, such as placing my refresh into the PreviewTextInput, but of course reading the control's text at that time gives me the old value. (However, PreviewTextInput _can_ read other controls values at least...) I also tried using the TextInput event instead of TextChanged, and oddly, it doesn't do anything (i can't even fire a lone MsgBox out of it).

What exactly am I doing wrong? I also noticed that explicitely changing the TextChanged event of the "Hours" textbox specifically (with no XAML, just the codebehind file's default handler) works fine - it has access to other members. But when I define the handler, either through the style as described above or by adding "TextChanged="Refresh"" to the textbox's xaml markup, it doesn't work anymore.

If nothing works, i'm going to use the long way and create each textbox's handler individually, but I can't believe there's no way to define this globally, just like I do with my validation events, which works fine accross all textboxes through the UserControl.Resource.Style markup.

Or if someone has another event suggestion that I could use instead of TextChanged (even a UserControl event that can react to keypresses instead of a TextBox event), it would be a viable workaround. Thanks a lot! I hope someone can shed some light on this!

Answers (2)