none
Getting the Text Entered in ComboBox as the text is changed RRS feed

  • Question

  • Hi,

     

      I need to get the text entered in combobox when it is changed. I know that ComboBox does not have textchanged event and I tried to use the TextInput to get the new text entered in the comboxbox but it looks like the event is not getting fired. So I came across the PreviewTextInput, but the issue with this is that event takes place before updating the ComboxBox text so I am not able to get the updated text. Is there any other way to get the text changed value from combobox?

     

    Thanks,

    banu

     

    Wednesday, November 14, 2007 6:46 PM

Answers

  • This can do the trick:

    public Window1()

    {

        InitializeComponent();

        this.comboBox.Loaded += delegate

        {

            TextBox editTextBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox;

            if (editTextBox != null)

            {

                editTextBox.TextChanged += delegate { MessageBox.Show(comboBox.Text); };

            }

        };

    }

    Hope this helps

    Friday, November 16, 2007 7:40 AM

All replies

  • This can do the trick:

    public Window1()

    {

        InitializeComponent();

        this.comboBox.Loaded += delegate

        {

            TextBox editTextBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox;

            if (editTextBox != null)

            {

                editTextBox.TextChanged += delegate { MessageBox.Show(comboBox.Text); };

            }

        };

    }

    Hope this helps

    Friday, November 16, 2007 7:40 AM
  • We've been using this technique for a while now in our app.

    Now we are finding that occasionally, for no apparent reason, this FindName call will sometimes simply return null.  There is no rhyme or reason to it.  The same code path for the same control will work once, and then it will fail the next.  It looks like a race condition of some sort, and seems to be more likely to happen if the CPU is under heavy load at the time.

    Under what circumstances would FindName("PART_EditableTextBox") return null for a normal ComboBox that was just loaded?  How can you troubleshoot a problem like this?
    Thursday, May 15, 2008 4:46 PM
  • Hi Roland.

    I've seen this happen , but only when i've forgotten to add the setting up of the text changed event to the *combo-boxes* load event, instead i've accidentally added it to the window's loaded event instead.

     

    I haven't had it happen since.

     

     

    cheers,

    Rob

     

    Thursday, May 15, 2008 6:20 PM
  • Thanks Rob, but no, we are definitely hooking up to the loaded event of the combo box itself.

    I've been able to work around this for now with an ugly hack of noting the failure and going back some time later and trying again to get the TextBox from the ComboBox.  The 2nd attempt works.  So, this suggests to me that the original solution is not 100% reliable; you can't guarantee that the FindName call is always going to work during the Loaded event of the combo box.

    I should mention that the ComboBox in question is not declared in xaml as a static component on the window, it is created at run time in response to external data.  Maybe this has something to do with it.
    Thursday, May 15, 2008 8:39 PM
  • The event render is better, the best will be to add to a combobox descendant, but i override the render on the page class (or window class) :

    protected override void OnRender(DrawingContext drawingContext)

    {

    base.OnRender(drawingContext);

    TextBox TB = (TextBox) combobox.Template.FindName("PART_EditableTextBox", combobox);

    if (TB != null)

    TB.TextChanged += combobox_TextChanged;

    }

    Friday, May 16, 2008 2:40 PM
  • The TextBox.TextChanged event will bubble up, so you can attach to it on the ComboBox. Something like this for codebehind:

    comboBox.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(OnTextChanged)); 

    Or you can attach to it in XAML like:

    <ComboBox TextBoxBase.TextChanged="OnTextChanged" ... /> 

    pm
    Monday, March 23, 2009 4:03 PM
  • I have not tested this, but I believe FindName("PART_EditableTextBox") will return null of the ComboBox.IsEditable is set to false.

    Kevin Humfreville

    Tuesday, July 9, 2013 4:31 PM