Windows Develop Bookmark and Share   
 index > Windows Forms General > Edit text in combo box...right event to use
 

Edit text in combo box...right event to use

I am trying to identify the right event when user has entered a text and pressed enter key to post the change (not in the combo box item list) into the edit box part of the combo box. From what I have seen, Text_Changed should be the event I should be looking for. However, this event is firing as I type into the text box. In other words, every (additional) letter I type is getting raised as an event (which I observe by simply placing a MessageBox.show(cb.text) in the event handler.

Can someone tell me what the right event is? If text_changed is the right event, is there some property that is leading to unposted changes raising the event.
  • Moved byCindy MeisterMVPMonday, May 18, 2009 3:34 PMQ about events for WinForm Combobox (From:Visual Studio Tools for Office)
  •  
kavm  Friday, May 15, 2009 3:03 PM
If you would like make an inherited comboBox for the "validating" event, I can propose the folowing:

    public class MyComboBoxSavedOnInput : System.Windows.Forms.ComboBox
    {

        public event EventHandler ValueUpdated;
        
        protected void OnValueUpdated(object sender, EventArgs e)
        {
            if (ValueUpdated != null)
            {
                ValueUpdated(sender, e);
            }
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            this.OnValueUpdated(this, e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Enter)
            {
                this.OnValueUpdated(this, e);
            }
        }
        
    }


Best regards, Sergiu
  • Marked As Answer bykavm Wednesday, June 03, 2009 3:04 PM
  •  
Sergiu Dudnic  Friday, May 22, 2009 4:05 PM
In the end, I settled for fixed length input in the text box. With that, I only process the text_Changed event when the string length reaches the target. I do validations at this stage. Seems to serve my scenario.
  • Marked As Answer bykavm Monday, May 25, 2009 4:44 PM
  •  
kavm  Monday, May 25, 2009 4:44 PM

Please specify the version of Office and VSTO involved.

I am assuming we're discussing a CommandBar object, from the context of your message, and not some other kind of ComboBox?


Cindy Meister, VSTO/Word MVP
Cindy Meister  Monday, May 18, 2009 9:14 AM

Thanks for responding.

It is Offce 2007, VSTO 2008 (what ships with VS 2008). The combo box is in a user control inside a document action pane in Excel. In case it was something to do with the action pane, I tried it in a form - with same results. I am thinking there must be some property that causes this behavior or TextChanged is not the right event.

Thanks again for your response!

kavm  Monday, May 18, 2009 12:47 PM
OK, I'm moving this message thread to the Windows Forms forum, as that's the best place to get information on how to work with such controls.
Cindy Meister, VSTO/Word MVP
Cindy Meister  Monday, May 18, 2009 3:33 PM
Thank you!
kavm  Monday, May 18, 2009 4:23 PM
There are several options here:

1) Use the Validating event. Here you can validate the value and post the change. However, this event only fires when the user leaves the control.

2) Use the TextChanged event. But as you saw, this will cause an update for each letter you typed.

Hope this helps.
www.insteptech.com
DeborahK  Monday, May 18, 2009 4:43 PM
OK. So, I am not going crazy and TextChanged fires with each letter one types. So, I am now lookingto usethe validating event.

Just to make sure I understand it right - I will need to monitor two events on the combobox: (1) SelectedIndexChanged and (2) Validating, with validating event firing after user leaves the control.

If the user types a value that is already in the combobox.items - the selectedIndexChanged will fire first - and in this event, one may ignore the validating event.

If user types something that is not in the combobox.items - the selectedIndexChanged will not fire andone would validate input and take desired action.

Is that about right?

Thanks a lot!

kavm  Monday, May 18, 2009 5:57 PM
The best way to determine the order of events in your particular situation is to put in Debug.WriteLine messages or break points. Then you will know for sure.
www.insteptech.com
DeborahK  Monday, May 18, 2009 6:41 PM
You can also use the KeyPress event and check to see if e.KeyChar == 13, which will detect if the user has pressed the enter key while within the control.
David Morton - http://blog.davemorton.net/ - @davidmmorton
David M Morton  Monday, May 18, 2009 6:44 PM
OK! I will try these options - and post back my experience on the first one that works!
kavm  Monday, May 18, 2009 6:50 PM
Sound to me you ought to set the DropDownStyle property to DropDownList. So that the user can never type anything wrong.

Hans Passant.
nobugz  Tuesday, May 19, 2009 2:43 AM
Thanks! That was indeed my first thought. However, when I am also interested in using AutoCompleteMode and AutoCompleteSource properties. [The values I am using are Append and ListItems, respectively.] The auto complete behavior short of what I wanted. I will describe that below in case I was doing something wrong:

When using
DropDownStyle property = DropDownList
AutoCompleteMode = Append (or other values I tried)
AutoCompleteSource = ListItems (the only value I tried)
I found the following: My test list had names - with two that began with a D - David Ames and David Beck (to test the behavior). As soon as typed D - the autocomplete would automatically select one of them as the item, whereas I was looking for the user to type David A before it completed to David Ames (and so on). I could not get rid of that behavior until I changed the DropDownStyle property = DropDown, which, of course, allows the user to type in something not in the list - not something I want, but leaves me with a validation task.

With all this, do you have a different advice?

Thanks again!
kavm  Tuesday, May 19, 2009 1:16 PM
AutoComplete is getting in the way here. Set it back to None and use DropDownStyle = DropDownList. Now you can repeatedly type "D" to cycle through the items that start with the letter D. This is the way most users would expect ComboBox to work.

Hans Passant.
nobugz  Tuesday, May 19, 2009 3:54 PM
Hi kavm,

Has your problem been solved with the suggestions currently provided? If not, please let us know your progress here.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, May 22, 2009 2:54 AM
Apologize for the delay. I have been following the suggestions but have not yet tested and confirmed the changes. Will post my experience shortly.
kavm  Friday, May 22, 2009 1:13 PM
OK. Here is the unhappy report of what my testing produced.

Validating works, but is weird as the user somehow has to leave control. That's really weird - the user has to go to another control (some of which might be enabled based upon the valid value being entered in the text box. Having seen it work, I am ready to look for an alternative.

KeyPress suggestion did not work - as pressing Enter keys multiple did not cause the event to fire. Read the detail and found The KeyPress event is not raised by noncharacter keys. So, following the example on MSDN - added the KeyDown event which captures the non-character keys. Of course, there was no defined precedence between when KeyDown occurs relative to TextChanged. Hoping it preceded it, I set a flag to enable full processing (validation) of textChanged. No luck! I can not get the KeyPress to fire upon entering Enter. I have had it fire but do not recall what other key I entered to make it do so. So the code does process it correctly, but I can not tell when it might fire in the first place. One might need to IsInputKey for it to recognize as a KeyDown event - but I am hesitant to get into going down that road without an example or even knowing the sequence of firings between keydown and textChanged.

So, the sum total is no good resolution. Unless I hear some other ideas here, I plan to reduce my expectations - and require a fixed legth of text input in the text box. I can then act on the textChanged when the input length equals the required length. Not ideal, but seems best of the bad alternatives.

I will leave the thread open - in case someone has a suggestions/advice.

BTW, I am taking nobugz's suggestion on not using the autocomplete. It is not ideal either as typing the first letter not only positions you to the first item starting with that letter, it also triggers the SelectedIndexChanged event. So, I do end up processing the event multiple times. But, I guess that's part of the cost. Thanks!
kavm  Friday, May 22, 2009 3:52 PM
If you would like make an inherited comboBox for the "validating" event, I can propose the folowing:

    public class MyComboBoxSavedOnInput : System.Windows.Forms.ComboBox
    {

        public event EventHandler ValueUpdated;
        
        protected void OnValueUpdated(object sender, EventArgs e)
        {
            if (ValueUpdated != null)
            {
                ValueUpdated(sender, e);
            }
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            this.OnValueUpdated(this, e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Enter)
            {
                this.OnValueUpdated(this, e);
            }
        }
        
    }


Best regards, Sergiu
  • Marked As Answer bykavm Wednesday, June 03, 2009 3:04 PM
  •  
Sergiu Dudnic  Friday, May 22, 2009 4:05 PM

This is not providing any additional solution for the problem, but I just wanted to clarify something for anyone who later reads this thread ...

The validation model used by the binding features *does* use the Validating events, which are only generated when the user leaves the field. So as much as this may seem weird, it is the way it is.

If you are using a binding source, you can change the way that this works by changing the Data Source Update Mode from OnValidate to OnPropertyChanged. The code will then execute as you change the data instead of when the user leaves the field.


www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, May 22, 2009 4:26 PM
Sergiu,

I was trying this on the textBox - as my queries on text and combo box got joined together - and was trying to use the key press event there (which I hope is analogous to OnKeyDown).

From MSDN:
To handle keyboard events only at the form level and not enable other controls to receive keyboard events, set the KeyPressEventArgs. . :: . Handled property in your form's KeyPress event-handling method to true . Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true .

Therefore, to text for Enter, one must first override the default IsInputKey.

So far, I am trying to stay away from creating custom controls - as I am already creating a user control and am trying to keep this simple.

DeborahK -

I agree. I realized how it worked, but it seemed unacceptable to me in what I am trying to do.

Thanks.

  • Edited bykavm Friday, May 22, 2009 5:20 PM
  • Edited bykavm Friday, May 22, 2009 5:22 PM
  •  
kavm  Friday, May 22, 2009 4:51 PM
In the end, I settled for fixed length input in the text box. With that, I only process the text_Changed event when the string length reaches the target. I do validations at this stage. Seems to serve my scenario.
  • Marked As Answer bykavm Monday, May 25, 2009 4:44 PM
  •  
kavm  Monday, May 25, 2009 4:44 PM
On revisiting this issue, I did take the approach Sergiu offered. It works beautifully - and it is the option I would recommend.

Thanks a lot!
kavm  Wednesday, June 03, 2009 3:05 PM

You can use google to search for other answers

Custom Search

More Threads

• Month calender Help!
• How does one export a Visual Basic 2008 Project
• One form/window - several interfaces
• TreeNode label edit
• App Settings & configurations
• Create icon?
• PC/SC (smart card / windows card) in .NET?
• How can you get the X/Y coordinates of the mouse relative to a picturebox on a click event?
• TabControl Not Counting Inserts
• Arrays Under Windows forms, C++