views:

1189

answers:

2

I'm trying to get a textbox keydown event to trap the backspace key down event. I have that working by adding a Class that overrides the textbox. What i don't know how to do is have that communicate with the class where the textbox is in the user control.

When a user types in the text box... say abcd or backspace, i need to update something on the usercontrol. let's just say i want to have something that displays how many characters are in the textbox. can someone help me with that. Here is what i have so far

Option Strict On
Imports System.Text.RegularExpressions

Partial Public Class Page
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
        Dim textbox As New MyTextBox() With {.Width = 300, .Height = 100}
        LayoutRoot.Children.Add(textbox)
    End Sub
End Class

Public Class MyTextBox
    Inherits TextBox
    Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
        MyBase.OnKeyDown(e)
        If e.Key = Key.Back Then
            e.Handled = True
            MyBase.OnKeyDown(e)

        ElseIf e.Key = Key.Delete Then
            e.Handled = True
            MyBase.OnKeyDown(e)
        End If
    End Sub
End Class

thanks shannon

A: 

You may be thinking the wrong way. Since you're in Silverlight, you can get this information with binding expressions. Just as a simple example, you said you want to know how many characters are in a text box. You can achieve this with the following xaml:

    <TextBox x:Name="txtInput" />
    <TextBlock Text="{Binding ElementName=txtInput, Path=Text.Length}" />
Rich
probably missing something.. however.. i am needing to now the length of the text box on each key stroke so that i can do some logic in the code behind. Can you help me understand how i can do this with what you have above.. if that is possible. Or what you have above is bound and in this example would be displaying an up to date length in the Textblock. thanks
jvcoach23
If you want to do work in the code behind in response to text input, add an event handler for KeyUp event. There, you can reference the TextBox by name or by the sender. txtInput.Text.Length, for example.
Rich
i tried the keyup event on the textbox.. it works for a lot of keyups.. but the backspace is ignored... and i need to catch that. Thanks
jvcoach23
+1  A: 

You shouldn't need to subclass TextBox to do this. Instead, add handler for the TextBox.TextChanged event right in your UserControl class. When this is called, the sender of the event should be your TextBox. You can then get the text from it and do what you need to do.

Update: Based on the comment, the following should work:

Partial Public Class Page
    Inherits UserControl

    Private TextBox1 as TextBox

    Public Sub New()
        InitializeComponent()
        TextBox1 = New TextBox() With {.Width = 300, .Height = 100}
        LayoutRoot.Children.Add(textbox)
    End Sub

    Private Sub OnTextChanged(sender as Object, e as TextChangedEventArgs) Handles TextBox1.TextChanged
        If e.Key = Key.Back Then
            e.Handled = True
        ElseIf e.Key = Key.Delete Then
            e.Handled = True
        End If
    End Sub
End Class

(My VB is a bit rusty, so the event handler syntax might not be completely correct.)

The basic idea is to get notified when text changes in the TextBox within your UserControl. This way you can modify the other parts of the UserControl as necessary.

Andy
would you be willing to give an example.. i tried to do what your talking about.. but i wasn't doing it correctly, because the textbox won't behave like it does in the override.
jvcoach23
See my update. Does that answer your question?
Andy
Andy.. very sorry i haven't gotten back to this.. I've looked (not real long or hard) and i'm not quite there yet. The OnTextChanged gives an errror that i need a withevents variable. i was thinking that tat was normally in the zaml.. hopefully you'll have interest in continuing my education. Thanks
jvcoach23
As I said, I don't use VB much so I probably don't have the syntax right. I think that you need to add WithEvents at the end of the line that declares TextBox1, but I'm not positive on that.
Andy
Hey.. i was taking a look around again.. you mention using the textchanged event. That event doesn't allow for the e.key or e.handled. The textChanged also doesn't pick up when you click on the return key.. So i'm thinking the textchanged is not the event to use.. boy.. i wish i knew more :). I guess i go back to the original question. is there a way i can get the subclass to talk to the user control so that i can pick up on what is going on in the subclass. or maybe you have a different idea. sorry to ask so much.thanks again
jvcoach23
There are other events that you can use off of the TextBox in addition to TextChanged (you can see the full list on MSDN, from the link I provided in my answer). As long as the event is handled within the UserControl itself, you can then do whatever you need to in the UserControl.
Andy