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:
|
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:
|
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!
JonathanPosted Mar 19, 2010, 9:57 AM
I have found out that my solution (the code above) works in a clean project, and it's only within my main application that it doesn't anymore. I also found out that for some reason, the TextChanged event was being triggered before the TextBox even existed in memory, thus pointing to an invalid reference. I changed my routine to something like "If txtbox isnot nothing then..." and it works now (but this still doesn't explain why my code works in a clean project).
I suspect VB being at fault here, so I'm going to try all of this in C# instead.
Sam HobbsPosted Mar 18, 2010, 10:10 PM
Perhaps my sample is totally irrelevant to your project; I hope not. Oh, sorry; I just now noticed that your code is VB, not C#. I don't think that makes a difference, except my sample uses C#. I hope my sample helps you to determine how to do it in VB too.