ComboBox events
-
Problem: I need to call a function when either a combo box selection is changed or some new text is typed into the combo box. SelectedIndexChanged takes care of the first half, but I can't figure out the OnTextChanged part. Does anyone know what event is triggered when a user types in text into the ComboBox? I know there are Enter and Leave events... but then I will have to keep track of whether the text changed myself, and that's my very last resort, because I have a gut feeling (called laziness ;P) that there has to be a better way to do this :) Does anyone have any ideas? Thank you, Elena Elena
-
Problem: I need to call a function when either a combo box selection is changed or some new text is typed into the combo box. SelectedIndexChanged takes care of the first half, but I can't figure out the OnTextChanged part. Does anyone know what event is triggered when a user types in text into the ComboBox? I know there are Enter and Leave events... but then I will have to keep track of whether the text changed myself, and that's my very last resort, because I have a gut feeling (called laziness ;P) that there has to be a better way to do this :) Does anyone have any ideas? Thank you, Elena Elena
elena12345 wrote: Does anyone have any ideas? When you form loads up, store an intial value within a public variable, the add an event handler for
KeyDown
, something like this should work:public string buffer = "something";
private void comboBox1\_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { ComboBox cb = sender as ComboBox; if(cb != null) { if(cb.Text != buffer) { // Call your function here if text changed... } } } }
- Nick Parker
My Blog -
elena12345 wrote: Does anyone have any ideas? When you form loads up, store an intial value within a public variable, the add an event handler for
KeyDown
, something like this should work:public string buffer = "something";
private void comboBox1\_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { ComboBox cb = sender as ComboBox; if(cb != null) { if(cb.Text != buffer) { // Call your function here if text changed... } } } }
- Nick Parker
My BlogHi Nick, Thank you for your reply. I don't see the advantage of using the KeyDown event instead of the Leave event. I got to store the value and do everytyhing manually anyway. Plus neither KeyDOwn nor Leave will catch the cases when the ComboBox is set programmatically. I guess there is no ComboBox.OnSelectedValueChaged silver bullet. :( Thanks again, Elena
-
Problem: I need to call a function when either a combo box selection is changed or some new text is typed into the combo box. SelectedIndexChanged takes care of the first half, but I can't figure out the OnTextChanged part. Does anyone know what event is triggered when a user types in text into the ComboBox? I know there are Enter and Leave events... but then I will have to keep track of whether the text changed myself, and that's my very last resort, because I have a gut feeling (called laziness ;P) that there has to be a better way to do this :) Does anyone have any ideas? Thank you, Elena Elena
The
TextChanged
event fires if yourComboBox.DropDownStyle
is set toComboBoxStyle.DropDown
. The unfortunate part is that is fires for every character typed, so you might want to handle theLostFocus
event or theValidating
event in order to add the new text or to call the function after the text is entered in its entirety.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Problem: I need to call a function when either a combo box selection is changed or some new text is typed into the combo box. SelectedIndexChanged takes care of the first half, but I can't figure out the OnTextChanged part. Does anyone know what event is triggered when a user types in text into the ComboBox? I know there are Enter and Leave events... but then I will have to keep track of whether the text changed myself, and that's my very last resort, because I have a gut feeling (called laziness ;P) that there has to be a better way to do this :) Does anyone have any ideas? Thank you, Elena Elena
For those interested, here is what I ended up doing. But first a little mode details on my case: it's a login dialog, and whenever the value in ServerComboBox changes I have to go and pull a list of databases off the server and put it into the DBComboBox. This might look like an overkill with ServerComboBox.Leave, ServerComboBox.SelectedIndexChanged, and DBComboBox.DropDown handlers. I would skip Leave & SelectedIndexChanged and just do DropDown if I didn't have to make a call to the server. When I do the call the the server on DropDown, the GUI hangs a little, so it's better to do it right when the ServerComboBox is changed. Why do I have the DropDown handler at all: there is one case when Leave & SelectedIndexChanged don't do it: on startup when there is no history in the registry and we use the hardcoded default value for the ServerComboBox. I don't know if I am making sence or it's too specific to my problem. Here it is anyway: Let me know if you see any way to improve the code. So here it is: /// /// The DbComboBox is dependent on the ServerComboBox and has to display the databases /// available for the selected server. /// class DbComboBox : ComboBox { string m_onEnterServerName = ""; //the name of the server we pulled databases from the last time ComboBox m_ServerComboBox; /// /// Constructor /// /// server combo box that we need to display Dbs for public DbComboBox(ComboBox ServerComboBox):base() { m_ServerComboBox = ServerComboBox; //handles user changes but not programatic updates //programatic updates are handled by SelectedIndexChange and a call to RefreshDbHistory //from Server property in the SignIn dialog m_ServerComboBox.Leave += new System.EventHandler(ServerChangedEventHandler); //handles selection change, but not when the user types in something new m_ServerComboBox.SelectedIndexChanged += new System.EventHandler(ServerChangedEventHandler); //in case there is no history and login defaults we need to load DBs for the //hardcoded default. this.DropDown += new System.EventHandler(ServerChangedEventHandler); } /// /// Event handler that is called when we suspect that the selection for the server combo box ha