0

I have a WPF UserControl in which I am creating a TextBox in the code-behind, rather than in the xaml. The TextBox shows and works just fine, and I can access its methods and do things like getting the text from it.

Now, I want to handle that TextBox's TextChanged event. I can't use the OnTextChanged method, as I'm trying to handle the child of the UserControl, rather than the UserControl itself. Because the TextBox was created dynamically, rather than in the xaml, I can't use the xaml Click to specify the event's handler in my UserControl code-behind.

I've finally tried using the TextBox.AddHandler method of the TextBox, and that appears to be promising, if I can get it to work.

Here's some snippets from my code:

I included this line in order to allow my code to recognize the TextBoxBase event group:

using System.Windows.Controls.Primitives;

This is the class definition:

    public partial class UniversalControl : UserControl
    {

This is the line within the class where I instantiate the TextBox:

        private TextBox theText = new TextBox();

This is a method that is called to set up some controls, of which the TextBox is one:

        private  void InitializeControl(ControlTypes Type)
        {

And, here is the line within that method that causes the error. I'm trying here to set the event handler to a method I define in the UniversalControl class.

            theText.AddHandler(TextBoxBase.TextChanged, new RoutedEventHandler(OnTextBoxTextChanged));
        }

This is the event handler itself:

        private void thePanel_TextChanged(object sender, TextChangedEventArgs e)
        {
            MessageBox.Show("The text changed.");
        }

The error that is thrown is:

CS0120  An object reference is required for the non-static field, method, or property, 'TextBoxBase.TextChanged'

Why am I getting this error? I've searched for hours and found lots of people encountering this error, but none with my particular set of circumstances.

Please provide solutions that do not require xaml other than in my UserControl. Remember, the TextBox is not defined in xaml. I'm also not well-enough versed in xaml to be building complicated styles or templates.

Perhaps there's another approach I could take. My desire is to create the TextBox dynamically and make it appear only when needed. Other controls may appear in its place in other instantiations of my UserControl.

  • This was not answered by the reference Ed provided, as that was explaining a null reference exception and what I'm getting is a compiler error. No way to use the debugger if the code won't even build. I suspect the problem has something to do with the way that TextBase is defined, but I couldn't find anything that addressed that. I tried a number of methods to instantiate an object based on the event code, but, of course, that won't work. I even tried defining the method and the class as static, but that didn't help. – harleygnuya Apr 15 '18 at 02:02
  • My mistake, I read it on my phone. – 15ee8f99-57ff-4f92-890c-b56153 Apr 15 '18 at 02:33
  • 1
    What's wrong with `theText.TextChanged += OnTextBoxTextChanged;`, again? – 15ee8f99-57ff-4f92-890c-b56153 Apr 15 '18 at 02:35
  • Alternatively, use the routed event: `tb.AddHandler(TextBox.TextChangedEvent, ...)` – 15ee8f99-57ff-4f92-890c-b56153 Apr 15 '18 at 02:39

1 Answers1

0

You can do this two ways. They both do the same thing.

TextChanged is an event, an instance member of TextBox. Assign a handler to it like this. You don't need new RoutedEventHandler(). This is conventional .NET event handling, not specific to XAML. I recommend this approach.

theText.TextChanged += OnTextBoxTextChanged;

The approach you're trying to use is a XAML thing. The static routed event property is named, by convention, TextBoxBase.TextChangedEvent. That's used by XAML for adding event handlers with EventSetter and so on. No need for you to bother with it, but if you're a glutton for punishment, that would look like this:

theText.AddHandler(TextBoxBase.TextChangedEvent, 
    new TextChangedEventHandler(OnTextBoxTextChanged));