Question UserControl: Can a TextChange event access other elements within the UserControl?

muik

Member
Joined
Dec 3, 2009
Messages
13
Programming Experience
3-5
Hi,

I am building a UserControl that has several textboxes in it. By using <eventsetter> within <UserControl.Resources.Style>, I could register some additional handlers to PreviewTextInput and Focus events correctly (validation and stuff) to all textboxes - this works fine.

However when I try to do anything within a TextChanged handler that references an element other than "sender", my application cannot create an instance of my usercontrol anymore until I comment out the lines refering to other elements. Here's what I'm trying to achieve (i know the syntax is slightly wrong, i'm just tring to be brief)

VB.NET:
...
<UserControl.Resources.Style TargetType="{x:Key TextBox}">
    <EventSetter Event="TextChanged" Handler="Refresh" />
...
<TextBox Name="Hours" />
<Label Name="TotalHours" />
...

Then in my code-behind file:

VB.NET:
Private Sub Refresh(ByVal sender As System.Object, ByVal e As System.Windows.Input.TextCompositionEventArgs)
    TotalHours.Content = Hours.Text * 40
End Sub

Because of the "TotalHours.Content" line, my application cannot compile anymore (error: "Cannot create an instance of PhaseCalulator") even though the UserControl compiles fine on its own. Just to prove myself that my syntax and all was OK, I changed the sub for the following:

VB.NET:
Private Sub Refresh(ByVal sender As System.Object, ByVal e As System.Windows.Input.TextCompositionEventArgs)
    MsgBox("Hello World")
End Sub

The above works perfectly, the application could compile and changing the text in the textbox would display the MsgBox.

Is it normal that the TextChanged event cannot access any other element except itself (sender)? For the record, I could access and refresh other elements in my UserControl through the PreviewTextInput and GotKeyboardFocus events. But when it's through TextChanged, nope. Even just changing the working MsgBox to MsgBox(Hours.Text) would make my application fail.

The behavior i'm looking for, is that everytime the text changes within any textbox (there are several...), everything is recalculated and shown immediately to the user in real time. If someone knows of a different event that would catch the changes, or for example, a UserControl event that would catch a keypress within any of its controls, it would be a viable workaround.

And please ntoe that I'm not trying to expose any of the UserControl methods/properties/events within my application - the UserControl should work out-of-the-box without any code in the app and refresh on its own. I'm saying because I found several threads about exposing controls and it's not the goal here.

Thanks in advance!
 
now, this might be me being dumb, but you psudo code is throwing me a little.
is

VB.NET:
<Label Name="TotalHours" />

actually

VB.NET:
<asp:Label runat="server" id="TotalHours" />

If it is, then shouldn't

VB.NET:
Private Sub Refresh(ByVal sender As System.Object, ByVal e As System.Windows.Input.TextCompositionEventArgs)
    TotalHours.Content = Hours.Text * 40
End Sub

be

VB.NET:
Private Sub Refresh(ByVal sender As System.Object, ByVal e As System.Windows.Input.TextCompositionEventArgs)
    TotalHours.Text= Hours.Text * 40
End Sub
 
I found out that for some reason, in my project, the TextChanged event was being triggered before the TextBoxes were instanciated and thus was trying to access an invalid reference. I fixed the problem by adding a "isnot nothing" check in the refresh routine.

However, I also tried to copy/paste my own code into a clean project and it works (without the isnot nothing). I don't know why it doesn't in my full application - especially since I have no code-behind yet, just 1 XAML file... There are lots of containers (window, grid, tabcontrols, stackpanels, then my textboxes) but still, I don't see why it shouldn't work.
 
oops, silly me, I stand corrected :)
 
Back
Top