How to implement automatic text completion for a ComboBox control in Visual C++ .NET or in Visual C++ 2005 (816186)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)

For a Microsoft Visual C# .NET version of this article, see 319946.
For a Microsoft Visual Basic .NET version of this article, see 320107.

IN THIS TASK

SUMMARY

This step-by-step article describes how to implement an automatic text-completion feature in a ComboBox control by using Microsoft Visual C++ .NET or Microsoft Visual C++ 2005.

back to the top

Learn more about automatic text completion

You can enter data in a ComboBox control either by typing a value or by clicking a value in a list. When you type a value, it is faster to type only the first few characters of the value and then have the ComboBox control automatically display the closest match from the list of values. Many Microsoft products use this feature. For example, Microsoft Money uses this feature to select the Payee value when you write a check. Microsoft Internet Explorer uses this feature when you type a Web address. Microsoft Visual Studio .NET uses this feature for Microsoft IntelliSense. You can see this feature in use if you type the following line of code in the Code editor in Microsoft Visual Studio .NET:
System::cons


Typing this line automatically displays System::Console. This automatic text-completion feature can save time and can help to prevent data-entry errors. This article describes how to implement this functionality in your Visual C++ .NET application.

back to the top

Use the sample code

  1. Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual C++ Projects under Project Types, and then click Windows Forms Application (.NET) under Templates.

    Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates.
  4. In the Name box, type Q816186.
  5. In the Location box, type C:\Test, and then click OK. Form1 is created automatically.
  6. Add a ComboBox control to Form1.
  7. Switch to Design view (Press SHIFT+F7), and then double-click Form1. Add the following code to the Form1_Load event:
    // Add some items to the ComboBox list.
    this->comboBox1->Text = "";
    this->comboBox1->Items->Add(S"a");
    this->comboBox1->Items->Add(S"aaa");
    this->comboBox1->Items->Add(S"combo");
    this->comboBox1->Items->Add(S"combobox");
    this->comboBox1->Items->Add(S"combobox test");
    this->comboBox1->Items->Add(S"common");
    this->comboBox1->Items->Add(S"common dialog");
  8. Right-click the comboBox1 control, and then click Properties.
  9. Click Events.
  10. Double-click the KeyUp event to add a comboBox1_KeyUp event handler to the code window.
  11. Add the following code to the comboBox1_KeyUp event handler:
    int index;
    String *actual;
    String *found;
    
    // Do nothing for certain keys, such as navigation keys.
    if ((e->KeyCode == Keys::Back) ||
    	(e->KeyCode == Keys::Left) ||
    	(e->KeyCode == Keys::Right) ||
    	(e->KeyCode == Keys::Up) ||
    	(e->KeyCode == Keys::Down) ||
    	(e->KeyCode == Keys::Delete) ||
    	(e->KeyCode == Keys::PageUp) ||
    	(e->KeyCode == Keys::PageDown) ||
    	(e->KeyCode == Keys::Home) ||
    	(e->KeyCode == Keys::End))
    {
    	return;
    }
    
    // Store the actual text that has been typed.
    actual = this->comboBox1->Text;
    
    // Find the first match for the typed value.
    index = this->comboBox1->FindString(actual);
    
    // Get the text of the first match.
    if (index > -1)
    {
    	found = this->comboBox1->Items->get_Item(index)->ToString();
    	
    	// Select this item from the list.
    	this->comboBox1->SelectedIndex = index;
    	
    	// Select the part of the text that was automatically
    	// added so that additional typing replaces it.
    	
    	this->comboBox1->SelectionStart = actual->Length;
    	this->comboBox1->SelectionLength = found->Length;
    }
    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

  12. Press CTRL+SHIFT+S to save the project.
  13. Press CTRL+SHIFT+B to build the solution.
  14. Press CTRL+F5 to run the project.
  15. Type some values. As you type the text, a value is automatically selected if the list of values contains an exact match. If a value in the list begins with the characters that you type, that value appears. The part of the value that you typed is selected so that additional typing replaces it. The following table describes the results from using this sample code:

    Typed charactersResulting string
    aa
    aaaaa
    comcombo
    commcommon
    combobcombobox
    combobox<SPACEBAR>combobox test
back to the top

Modification Type:MajorLast Reviewed:1/4/2006
Keywords:kbSample kbProgramming kbcode kbComboBox kbWindowsForms kbHOWTOmaster KB816186 kbAudDeveloper