Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ComboBox events

ComboBox events

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elena12345
    wrote on last edited by
    #1

    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

    N H E 3 Replies Last reply
    0
    • E elena12345

      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

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      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

      E 1 Reply Last reply
      0
      • N Nick Parker

        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

        E Offline
        E Offline
        elena12345
        wrote on last edited by
        #3

        Hi 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

        1 Reply Last reply
        0
        • E elena12345

          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

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          The TextChanged event fires if your ComboBox.DropDownStyle is set to ComboBoxStyle.DropDown. The unfortunate part is that is fires for every character typed, so you might want to handle the LostFocus event or the Validating 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-----

          1 Reply Last reply
          0
          • E elena12345

            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

            E Offline
            E Offline
            elena12345
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups