15

So basically, I have a bunch of TextBoxes that the user gets to fill out. I've got a button that I want to keep disabled until all the TextBoxes have had text entered in them. Here is a sample XAML TextBox that I'm using:

<TextBox Name="DelayedRecallScore" TextInput="CheckTextBoxFilled" Width="24" />

And here is the function that I'm trying to trigger:

  //Disables the OK button until all score textboxes have content
    private void CheckTextBoxFilled(object sender, RoutedEventArgs e)
    {
        /*
        foreach (TextBox scorebox in TextBoxList)
        {
            if (string.IsNullOrEmpty(scorebox.Text))
            {
                Ok_Button.IsEnabled = false;
                return;
            }
        }
        Ok_Button.IsEnabled = true;
         */
        MessageBox.Show("THIS MAKES NO SENSE");
    }

The MessageBox is not showing up when TextInput should be getting triggered. As an experiment I tried triggering CheckTextBoxFilled() on PreviewTextInput, and it worked fine then, meaning that for whatever reason, the function just isn't getting called. I also have a validation function that is triggered by PreviewTextInput, which works as it should. At first I thought PreviewTextInput might somehow be interfering with TextInput, so I took PreviewTextInput off the TextBox, but that hasn't managed to fix anything. I'm completely befuddled by why this might happen, so any help would be appreciated.

Kay Ell
  • 155
  • 1
  • 1
  • 4

4 Answers4

16

Your handler for the TextInput event is not fired because the TextBox is handling the event. You could try using the TextChanged event instead, since really you just want to know when characters were added or removed from the TextBox.

Andy
  • 30,088
  • 6
  • 78
  • 89
  • 3
    I didn't know that, thanks. I just Figured that since PreviewTextInput worked that way, there was nothing to stop TextInput from functioning the same way. TextChanged works just fine. – Kay Ell Sep 03 '09 at 18:21
  • 2
    WPF really does like to make what should be simply tasks overly difficult. I mean, why expose an event if it will never fire? Ridiculous... yeah, I'm venting :D – Ed S. Apr 15 '11 at 19:19
  • @Ed FWIW, if you subclass TextBox, then you could handle the event. But I do see your point, though. – Andy Apr 16 '11 at 15:18
  • Yeah, there's always a workaround, and I generally like WPF, I have just been bitten so many times by what should be simple things made overly difficult by the framework on this recent project that I am getting a bit bitter. – Ed S. Apr 17 '11 at 08:04
8
InitializeComponent();
textbox.AddHandler(TextBox.TextInputEvent, 
                   new TextCompositionEventHandler(TextBox_TextInput_1), 
                   true);
K Mehta
  • 10,323
  • 4
  • 46
  • 76
Lavesh
  • 81
  • 1
  • 1
2

Use "PreviewTextInput" instead, it will work.

Rankit Dua
  • 51
  • 6
0

Create a new class derived from TextBox. In the new class override the OnTextInput method. Your OnTextInput method will get called before the TextBox gets it.