Microsoft KB Archive/816186

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

Article ID: 816186

Article Last Modified on 1/4/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-





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
 * Learn more about automatic text completion
 * Use the sample code



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
 Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. 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. In the Name box, type Q816186 . In the Location box, type C:\Test, and then click OK. Form1 is created automatically. Add a ComboBox control to Form1.</li>  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 = &quot;&quot;; this->comboBox1->Items->Add(S&quot;a&quot;); this->comboBox1->Items->Add(S&quot;aaa&quot;); this->comboBox1->Items->Add(S&quot;combo&quot;); this->comboBox1->Items->Add(S&quot;combobox&quot;); this->comboBox1->Items->Add(S&quot;combobox test&quot;); this->comboBox1->Items->Add(S&quot;common&quot;); this->comboBox1->Items->Add(S&quot;common dialog&quot;); </li> Right-click the comboBox1 control, and then click Properties.</li> Click Events.</li> Double-click the KeyUp event to add a comboBox1_KeyUp event handler to the code window.</li>  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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

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

</li> Press CTRL+SHIFT+S to save the project.</li> Press CTRL+SHIFT+B to build the solution.</li> Press CTRL+F5 to run the project.</li> 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:

</li></ol>

back to the top

Additional query words: Autocomplete Auto

Keywords: kbhowtomaster kbwindowsforms kbprogramming kbsample kbcombobox kbcode KB816186

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.