+ Reply to Thread
Results 1 to 6 of 6

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

  1. #1
    muik is offline VB.NET Forum Newbie muik is on a distinguished programming path ahead
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2009
    Age
    27
    Posts
    13
    Reputation
    10

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

    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)

    Code:
    ...
    <UserControl.Resources.Style TargetType="{x:Key TextBox}">
        <EventSetter Event="TextChanged" Handler="Refresh" />
    ...
    <TextBox Name="Hours" />
    <Label Name="TotalHours" />
    ...
    Then in my code-behind file:

    Code:
    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:

    Code:
    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!

  2. #2
    r3plica's Avatar
    r3plica is offline VB.NET Forum Enthusiast r3plica done a little coding in his/her time r3plica done a little coding in his/her time r3plica done a little coding in his/her time
    .NET Framework
    .NET 3.5
    Join Date
    Mar 2010
    Age
    30
    Posts
    81
    Reputation
    63

    Default

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

    Code:
    <Label Name="TotalHours" />
    actually

    Code:
    <asp:Label runat="server" id="TotalHours" />
    If it is, then shouldn't

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

    Code:
    Private Sub Refresh(ByVal sender As System.Object, ByVal e As System.Windows.Input.TextCompositionEventArgs)
        TotalHours.Text= Hours.Text * 40
    End Sub
    If I solve your issue or you are happy with my response, please provide me with reputation by clicking the reputation link at the top right of my posts - thanks

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Age
    37
    Posts
    10,843
    Reputation
    1443

  4. #4
    muik is offline VB.NET Forum Newbie muik is on a distinguished programming path ahead
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2009
    Age
    27
    Posts
    13
    Reputation
    10

    Default

    Replica: No, Labels use .Content and TextBox uses .Text. Also, this is VB, not ASP.

  5. #5
    muik is offline VB.NET Forum Newbie muik is on a distinguished programming path ahead
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2009
    Age
    27
    Posts
    13
    Reputation
    10

    Default

    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.

  6. #6
    r3plica's Avatar
    r3plica is offline VB.NET Forum Enthusiast r3plica done a little coding in his/her time r3plica done a little coding in his/her time r3plica done a little coding in his/her time
    .NET Framework
    .NET 3.5
    Join Date
    Mar 2010
    Age
    30
    Posts
    81
    Reputation
    63

    Default

    oops, silly me, I stand corrected
    If I solve your issue or you are happy with my response, please provide me with reputation by clicking the reputation link at the top right of my posts - thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts