locked
Handle MaxLength changed event for TextBox RRS feed

  • Question

  • Hi,

    Is it it possible to handle the event when MaxLength property is changed on a TextBox?

    Thanks,

    cp

    Friday, September 3, 2010 2:18 AM

Answers

  • Well, looks like its quite simple to do with WPF using Dependency Property mechanism. The solution is here:

    http://www.switchonthecode.com/tutorials/wpf-tutorial-introduction-to-dependency-properties

    This is what I do in the derived class:

        protected void SetMaxLengthChanged()
        {
          //Handle MaxLength event
          DependencyPropertyDescriptor dpd;
          dpd = DependencyPropertyDescriptor.FromProperty(
            TextBox.MaxLengthProperty, typeof(TextBox));
    
          dpd.AddValueChanged(this, MaxLengthChanged); 
        }
    
        void MaxLengthChanged(object sender, EventArgs args)
        {
          //Handle your custom code using this.MaxLength;
        }
    
    • Marked as answer by cp.net Friday, September 3, 2010 8:30 PM
    Friday, September 3, 2010 8:30 PM

All replies

  • Hi,

    You need to handle TextChanged event and in that check the maxlength property of textbox with the number of characters entered in Textbox.


    Syed Mustehsan Ikram
    Friday, September 3, 2010 3:11 AM
  • Hi,

    I think there is no direct way to handle the event when MaxLength property is changed on a TextBox.

    May be you have to extend the textbox with your own class and override the MaxLength property and create your own event handler in it.

    The following links will help you to understand how to do it

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2ca46e71-c9c9-45e8-980a-b4d3b5656399

    http://www.c-sharpcorner.com/UploadFile/scottlysle/RegExTextBox01022007150131PM/RegExTextBox.aspx

    http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-custom-event-handlers

    Hope this helps!

    regards,

    Vimal


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This will help other members to find the solution easily.
    Friday, September 3, 2010 3:12 AM
  • Thanks for the replies.

    Vimal,

    Great links. I have already tried deriving from TextBox, and noticed that the MaxLength is not open (virtual) to override. I have a custom formatting class that needs to hook into whenever the MaxLength is changed programmatically.

    Regards,

    cp

    Friday, September 3, 2010 7:43 AM
  • Its great to hear that you already found a solution.

    If possible then can you please post some code. So it will be helpful for others too.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This will help other members to find the solution easily.
    Friday, September 3, 2010 8:53 AM
  • Hi Vimal,

    I have not found a solution yet. As mentioned, the MaxLength is not open (virtual) to override. So can't find a way to catch that event.

    Regards,

    cp

    Friday, September 3, 2010 6:39 PM
  • Well, looks like its quite simple to do with WPF using Dependency Property mechanism. The solution is here:

    http://www.switchonthecode.com/tutorials/wpf-tutorial-introduction-to-dependency-properties

    This is what I do in the derived class:

        protected void SetMaxLengthChanged()
        {
          //Handle MaxLength event
          DependencyPropertyDescriptor dpd;
          dpd = DependencyPropertyDescriptor.FromProperty(
            TextBox.MaxLengthProperty, typeof(TextBox));
    
          dpd.AddValueChanged(this, MaxLengthChanged); 
        }
    
        void MaxLengthChanged(object sender, EventArgs args)
        {
          //Handle your custom code using this.MaxLength;
        }
    
    • Marked as answer by cp.net Friday, September 3, 2010 8:30 PM
    Friday, September 3, 2010 8:30 PM
  • Hi,

    Thanks for your post. :-)


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This will help other members to find the solution easily.
    Sunday, September 5, 2010 1:39 PM