Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to set the item index in an alpha sorted TComboBox when searching incrementa Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Tomas Rutkauskas

I have some alpha sorted items in a combo box (style csDropDown). When the user 
types in text, the combo box incrementally searches but it does not set the 
itemindex property. Is there a way of making it do this?


You could use the OnChange handler to perform a CB_FINDSTRING with the current edit 
text, then set the itemindex to the found item. But that is disruptive to typing, 
if the found item is not the one the user wants he has no way to change it, since 
each change triggers OnChange, which again finds the item. So you have to invest 
considerably more effort into this. Attach these handlers to the OnKeyPress and the 
OnChange event of the combobox, that seems to work fairly well.
2   procedure TForm1.ComboBox1Change(Sender: TObject);
3   var
4     oldpos: Integer;
5     item: Integer;
6   begin
7     with sender as TComboBox do
8     begin
9       oldpos := selstart;
10      item := Perform(CB_FINDSTRING, -1, lparam(Pchar(text)));
11      if item >= 0 then
12      begin
13        onchange := nil;
14        text := items[item];
15        selstart := oldpos;
16        sellength := gettextlen - selstart;
17        onchange := combobox1change;
18      end;
19    end;
20  end;
22  procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
23  var
24    oldlen: Integer;
25  begin
26    if key = #8 then
27      with sender as TComboBox do
28      begin
29        oldlen := sellength;
30        if selstart > 0 then
31        begin
32          selstart := selstart - 1;
33          sellength := oldlen + 1;
34        end;
35      end;
36  end;

This works with Win2000, but on a Win98 machine, the ItemIndex is getting set 
incorrectly after the first search. To make it work under both Win2000 and Win98, 
you could do something like this:
38  procedure TMainForm.ComboBoxKeyDown(Sender: TObject; var Key: Word; Shift: 
39  TShiftState);
40  var
41    s: string;
42  begin
43    if key = VK_RETURN then
44    begin
45      s := TComboBox(Sender).Text;
46      {The search doesn't work in Win 98 with DroppedDown set to true}
47      TComboBox(Sender).DroppedDown := false;
48      TComboBox(Sender).Text := s;
49    end;
50  end;

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC