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)
Then in my code-behind file:
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:
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!
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!