Create a combo box with C + + Builder

Source: Internet
Author: User

This article describes how to use C + + Builder and Windows API functions to create combo boxes that meet user-specific requirements in the win 98 environment, and expands the functionality of C + + Builer standard components through API functions.

Under C + + Builder, the combo control that it brings calls the OnChange Combo1change event handle when the content is changed. The event handle then sends a combo-based cb_findstring message to the system, passing the edit string content in the message. The SendMessage return value of the message is the index value of the first matching entry in the Drop-down box, and if a matching entry is found in the Drop-down list box, the Comobo1change handle sends a CB_SETEDITSEL message based on combo. The parameter of the message determines that the part from the insertion point to the end of the string is selected. The result is that the selected part is displayed as a message search result in the combo text value.

Specific implementation:

1, after entering C + + Builder 4.0, draw a CmoboBox1 on the form, enter Anlantic Faloncs, China, Japanese, Amercia, etc. in the items.

2, create the ComboBox1 onchange handle, and fill in the following code:

  void _fastcall TForm1::ComboBox1Change(TObject *Sender)
   {
    String value = ComboBox1->Text ;
    if (lastkey == '\b' || lastkey == VK_DELETE)// 如果用户输入的是Delete键或是Tab键,搜索不进行//
     {
      lastkey = 0 ;
     return ;
     }
    lastkey = 0 ;
    if (ComboBox1->SelStart != value.Length ())//如果用户的光标在输入字符串的中间,搜索不进行//
      return ;
    int index = SendMessage (ComboBox1->Handle, CB_FINDSTRING, -1, (LPARAM) value.c_str ()) ;
    //在下来列表框寻找与用户输入字符串相匹配的字符串的索引值//
    if (index >= 0)//如果索引值>0//
     {
     ComboBox1->ItemIndex = index ;
     SendMessage (ComboBox1->Handle, CB_SETEDITSEL, 0, MAKELPARAM (value.Length (), -1)) ;//发送CB_SETEDITSEL消息//
     }
    }

3, create the ComboBox1 KeyDown handle, and fill in the following code:

   void _fastcall TForm1::ComboBox1KeyDown(TObject *Sender, WORD &Key,TShiftState Shift)
   {
   lastkey = Key ;//保存最后的键//
   }

4. Add the following variables to the header file:

Private:word Lastkey;

5. Compile and run. When the user enters Chi in the input box, the entire string of China appears.

The above methods are implemented in the Pwin 98 and C++builder 4.0 Enterprise editions.

Sometimes you want your control to implement some unlikely functionality, try Windows API functions, maybe it will make your dream come true.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.