locked
Handling Text Changed event for TextBox RRS feed

  • Question

  • Hi,

    I want to handle TextChanged event for a TextBox derievd custom control in silvelight. I can do it easily by handling TextChanged of each individual TextBox, but then it will be a lengthy preocess as I have hundreds of textbox controls. What I want is handle the TextChanged event in the derived class (From TextBox). I dont see any override OnTextChanged() funtcion altough I can see OnTextInput() etc.

    Can anybody suggest how to do that?

    Thursday, November 17, 2011 7:53 PM

Answers

  • Are you asking or suggesting. I want to handle all the textChanged event in the derived class.

    Suggesting, and asking if that will work for you.  Just subscribe to the event in the derived class and handle it like you would any event.

    Saturday, November 19, 2011 1:09 PM
  • Hi,

    yes, in constructor:

        public class TextBoxChg : TextBox
        {
            public TextBoxChg()
            {
                base.TextChanged += TextChangedHandler;
            }
    
            private void TextChangedHandler(object sender, TextChangedEventArgs e)
            {
                Debug.WriteLine("Changed");
            }
        }
    

    That's enough.

    Sunday, November 20, 2011 11:46 AM

All replies

  • Subscribe to this.TextChanged  and handle the event in your derived class?

    Friday, November 18, 2011 12:16 PM
  • Sorry, I did not get you. Are you asking or suggesting. I want to handle all the textChanged event in the derived class.

    Saturday, November 19, 2011 11:33 AM
  • Are you asking or suggesting. I want to handle all the textChanged event in the derived class.

    Suggesting, and asking if that will work for you.  Just subscribe to the event in the derived class and handle it like you would any event.

    Saturday, November 19, 2011 1:09 PM
  • Ok,

    But how to subscribe for the self control? Can I subscribe to the TextChanged event in class which is derived from TextBox?

    If yes where to subscribe, constructor?

    Can you give me some sample code?

    Below is my class

    public class CgtEditBox : TextBox
        {
            public CgtEditBox()
                : base()
            {
            }
    
            //Custom dependecy properties
            private DependencyProperty SchemaProperty =
                          DependencyProperty.Register("Schema", typeof(object), typeof(CgtEditBox)
                 , new PropertyMetadata(OnSchemaChanged));
    
            private static void OnSchemaChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
            }
            public string Schema
            {
                get { return (string)GetValue(SchemaProperty); }
                set { SetValue(SchemaProperty, value); }
            }
    Sunday, November 20, 2011 9:28 AM
  • Hi,

    yes, in constructor:

        public class TextBoxChg : TextBox
        {
            public TextBoxChg()
            {
                base.TextChanged += TextChangedHandler;
            }
    
            private void TextChangedHandler(object sender, TextChangedEventArgs e)
            {
                Debug.WriteLine("Changed");
            }
        }
    

    That's enough.

    Sunday, November 20, 2011 11:46 AM
  • ok,
    let me try this and I will come back.

    Sunday, November 20, 2011 1:38 PM
  • Great!  It does work.

    Thanks

    Monday, November 21, 2011 1:28 PM