Use C ++ Builder to implement ComboBox similar to IE address bar

zhaozj2021-02-17  57

Everyone must be familiar with the address bar of the IE browser. If you have no use, when you enter a string of characters, the drop-down list will list the first few characters and input characters that are stored in all historical records. .

We also need this technology in design programs to make it easy for users. What is it implemented?

Let me talk about how this effect I use CBuilder:

First create a new application, on the form, the name is set to: ComboBox1

Declaring a StringList in header files

Tstringlist * mydropdownlist;

The application initialization department stores all list data into mydropdownlist;

Add the following code to the keypress of ComboBoX1:

NT I, IinputLength, ISELSTARTRESTORE, ISELLENGTHRESTORE; ANSISTRING STRINPUT; TSTRINGLIST * TEMPLIST

Strinput = ComboBox1-> text;

IF (Key == VK_ESCAPE) {key = 0x0; // no more beeping after pressing escape.

IF (key == vk_return) {key = 0x0; if (ComboBox1-> items-> indexof (strinput) == -1) ComboBox1-> items-> add (strInput); comboBoX1-> DroppedDown = false;

ComboBox1-> SELSTART = ComboBox1-> text.length ();

} Else {iSelStartRestore = ComboBox1-> SelStart; iSelLengthRestore = ComboBox1-> SelLength; if (Key == VK_BACK) {// Handle backspace: if ((ComboBox1-> SelLength == 0) && (ComboBox1-> SelStart> 0) ) {ComboBoX1-> SELSTART = ComboBox1-> SELSTART - 1; ComboBoX1-> Sellength = ComboBox1-> Sellength 1;}} Strinput.delete (ComboBoX1-> SELSTART 1, ComboBox1-> sellength);

IF (key! = vk_back) {STRINPUT.INSERT (key, comboBox1-> selstart 1);} IinputLength = Strinput.Length (); ComboBox1-> items-> clear (); if (IINPUTLENGTH> 0) {

Templist = new tstringlist; try {for (i = 0; I count - 1; i ) {= ((mydropdownlist-> strings [i] .substring (1, IinputLength)). Uppercase () == Strinput .UPPERCASE ()) Templist-> add (mydropdownlist-> strings [i]);} if (templist-> count> 0) {for (i = 0; i <7; i ) ComboBox1-> items-> add ( ""); ComboBox1-> DropdownCount = 8; ComboBox1-> DroppedDown = true; comboBoX1-> items-> clear (); comboBoX1-> items = Templist;} else comboBox1-> DroppedDown = false;}

__finally {templist-> free ();}} else

ComboBOX1-> DroppedDown = false; // restore the position of the carrot and the selected text: ComboBox1-> selstart = iselstartRestore; ComboBox1-> selLength = iSellength;

}

Implementing C Builder Win2000, others should also.

转载请注明原文地址:https://www.9cbs.com/read-29440.html

New Post(0)