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 AutoComplete with id value

ComboBox AutoComplete with id value

Scheduled Pinned Locked Moved C#
databasehelpquestion
4 Posts 2 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.
  • N Offline
    N Offline
    Noctris
    wrote on last edited by
    #1

    Hi Everyone, I have a combobox that i need to populate with a datasource from my database. Because i want to make this autocomplete AND have the possibility to enter new things, i have done the following:

    Private void LoadAuthors()
    {

    cmbAuthors.DataSource = Dataset;
    cmbAuthors.DisplayMember = "Name";
    cmbAuthors.ValueMember = "Id";
    cmdAuthors.AutoCompleteCustomSource = AutoCompleteStringCollectionIMadeFromTheDataSet;
    cmbAuthors.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    cmbAuthors.AutoCompleteSource = AutoCompleteSource.CustomSource;

    }

    And all is well in Autocomplete land and datasource land.. however... to find out if they entered a new name ( and i need to pop up a form to complete extra info), i have a handler for the "leave" event on the combobox:

    private void cmbAuthors_Leave(object sender, EventArgs e)
    {
    if (cmbAuthors.SelectedValue != null)
    {
    MessageBox.Show("We know the dude...");
    }
    else
    {
    MessageBox.Show("Who's that ? better make a new one !");
    // FrmAuthorDetails FrmAddNewAuthor = new FrmAuthorDetails();
    }
    }

    now the problem is: When you leave the combobox, the selectedvalue always is null, i get the "who's that" message, and when i click ok to that THEN the selectedindex event fires on the combobox (and thus only getting the required data to make this check) Is there any way to make sure the selectedindex event is fired first or manually fire the autocomplete so it will complete BEFORE, it actually fires the leave event ? Any other intelligent solution i could not think of or have not found on google codeproject is welcome too obviously ;-)

    Do Or Don't, there is no "try catch ex as exception end try"

    M N 2 Replies Last reply
    0
    • N Noctris

      Hi Everyone, I have a combobox that i need to populate with a datasource from my database. Because i want to make this autocomplete AND have the possibility to enter new things, i have done the following:

      Private void LoadAuthors()
      {

      cmbAuthors.DataSource = Dataset;
      cmbAuthors.DisplayMember = "Name";
      cmbAuthors.ValueMember = "Id";
      cmdAuthors.AutoCompleteCustomSource = AutoCompleteStringCollectionIMadeFromTheDataSet;
      cmbAuthors.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
      cmbAuthors.AutoCompleteSource = AutoCompleteSource.CustomSource;

      }

      And all is well in Autocomplete land and datasource land.. however... to find out if they entered a new name ( and i need to pop up a form to complete extra info), i have a handler for the "leave" event on the combobox:

      private void cmbAuthors_Leave(object sender, EventArgs e)
      {
      if (cmbAuthors.SelectedValue != null)
      {
      MessageBox.Show("We know the dude...");
      }
      else
      {
      MessageBox.Show("Who's that ? better make a new one !");
      // FrmAuthorDetails FrmAddNewAuthor = new FrmAuthorDetails();
      }
      }

      now the problem is: When you leave the combobox, the selectedvalue always is null, i get the "who's that" message, and when i click ok to that THEN the selectedindex event fires on the combobox (and thus only getting the required data to make this check) Is there any way to make sure the selectedindex event is fired first or manually fire the autocomplete so it will complete BEFORE, it actually fires the leave event ? Any other intelligent solution i could not think of or have not found on google codeproject is welcome too obviously ;-)

      Do Or Don't, there is no "try catch ex as exception end try"

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      what about trying to replace

      if (cmbAuthors.SelectedValue != null)

      with

      if(cmbAuthors.Items.Contains(cmbAuthors.Text))//check if name is in the list

      Life goes very fast. Tomorrow, today is already yesterday.

      N 1 Reply Last reply
      0
      • M musefan

        what about trying to replace

        if (cmbAuthors.SelectedValue != null)

        with

        if(cmbAuthors.Items.Contains(cmbAuthors.Text))//check if name is in the list

        Life goes very fast. Tomorrow, today is already yesterday.

        N Offline
        N Offline
        Noctris
        wrote on last edited by
        #3

        Hmm.. unfortunately, it does not even recognize that :( Strange thing is, if i look at what cmbAuthors.text contains, it is the text i want.. but it does not work. so that got me thinking and i tryed:

        cmbAuthors.selectedtext = cmbauthors.text;

        But.. helas.. it only uses the actual typed text so for example "mad" if you are looking for madonna :s I am thinking that autocomplete uses the same event (leave) to do it's autocomplete magic.. When i change the handler to the "onSelectedValueChanged", it does work .. however, that event get's fired multiple times on Runtime (so when binding the datasource for example)... and does not fire when the value you type in is unknown in the dataset/autocomplete The problem is that i really have to do the check when leaving the combo ...

        Do Or Don't, there is no "try catch ex as exception end try"

        1 Reply Last reply
        0
        • N Noctris

          Hi Everyone, I have a combobox that i need to populate with a datasource from my database. Because i want to make this autocomplete AND have the possibility to enter new things, i have done the following:

          Private void LoadAuthors()
          {

          cmbAuthors.DataSource = Dataset;
          cmbAuthors.DisplayMember = "Name";
          cmbAuthors.ValueMember = "Id";
          cmdAuthors.AutoCompleteCustomSource = AutoCompleteStringCollectionIMadeFromTheDataSet;
          cmbAuthors.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
          cmbAuthors.AutoCompleteSource = AutoCompleteSource.CustomSource;

          }

          And all is well in Autocomplete land and datasource land.. however... to find out if they entered a new name ( and i need to pop up a form to complete extra info), i have a handler for the "leave" event on the combobox:

          private void cmbAuthors_Leave(object sender, EventArgs e)
          {
          if (cmbAuthors.SelectedValue != null)
          {
          MessageBox.Show("We know the dude...");
          }
          else
          {
          MessageBox.Show("Who's that ? better make a new one !");
          // FrmAuthorDetails FrmAddNewAuthor = new FrmAuthorDetails();
          }
          }

          now the problem is: When you leave the combobox, the selectedvalue always is null, i get the "who's that" message, and when i click ok to that THEN the selectedindex event fires on the combobox (and thus only getting the required data to make this check) Is there any way to make sure the selectedindex event is fired first or manually fire the autocomplete so it will complete BEFORE, it actually fires the leave event ? Any other intelligent solution i could not think of or have not found on google codeproject is welcome too obviously ;-)

          Do Or Don't, there is no "try catch ex as exception end try"

          N Offline
          N Offline
          Noctris
          wrote on last edited by
          #4

          For the people that want to know. Apparently the Leave event is fired before autocomplete can do it's magic however, Putting the handler on the LostFocus event solves the issue. It's just not visibile in the VS2005 UI... Solved ;-)

          Do Or Don't, there is no "try catch ex as exception end try"

          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