Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TComboBox - Select item in drop down without setting text?



Permlink Replies: 4 - Last Post: Feb 4, 2016 11:06 AM Last Post By: steven chesser Threads: [ Previous | Next ]
steven chesser

Posts: 84
Registered: 7/27/00
TComboBox - Select item in drop down without setting text?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 3, 2016 11:26 PM
Using XE8.

I have a flavor of TComboBox with a certain filtering down system and key handling that was requested of me to do, as competitor product does so.

I have it down to one last problem.

User types in two letters of text.
Drop down is shown.
I build a dropdown list of items that contain those 2 letters.
I scroll into view the first item that begins with those 2 letters. So could be half way down the list.
Using the CB_SETTOPINDEX call, I can scroll into view that item.
I'd like to set the active item in that drop to be selected... BUT, not have it set the text in the user input box to that selected items text.

Basically, I need something like the CB_SETCURSEL call.... but minus the setting the input box to that text. ( https://msdn.microsoft.com/en-us/library/windows/desktop/bb775899(v=vs.85).aspx )

So not sure if missing an optional call ...
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: TComboBox - Select item in drop down without setting text?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 4, 2016 4:58 AM   in response to: steven chesser in response to: steven chesser
steven chesser wrote:

Using XE8.

I have a flavor of TComboBox with a certain filtering down system and
key handling that was requested of me to do, as competitor product
does so.

I have it down to one last problem.

User types in two letters of text.
Drop down is shown.
I build a dropdown list of items that contain those 2 letters.
I scroll into view the first item that begins with those 2 letters.
So could be half way down the list. Using the CB_SETTOPINDEX call, I
can scroll into view that item. I'd like to set the active item in
that drop to be selected... BUT, not have it set the text in the user
input box to that selected items text.

Basically, I need something like the CB_SETCURSEL call.... but minus
the setting the input box to that text. (
https://msdn.microsoft.com/en-us/library/windows/desktop/bb775899(v=vs
.85).aspx )

So not sure if missing an optional call ...

The easiest way could be to write your own combobox implementation. The
default behavior is autocomplete. IF you can turn that off it's easy to
position the selection in the listbox part. In order to positon in the
listbox you must obtain the handle of the listbox (FComboBoxListWnd:
THandle). To get this handle you can add to formcreate:

var
  Info: TComboBoxInfo;
begin
  ZeroMemory(@Info, SizeOf(Info));
  Info.cbSize := SizeOf(Info);
  GetComboBoxInfo(ComboBox1.Handle, Info);
  FComboBoxListWnd := Info.hwndList;
end;

Now yuo can send message to change selection in the listbox (wparam is
the itemindex to be selected):

  SendMessage(FComboBoxListWnd, LB_SETCURSEL, lNewIndex, 0);
steven chesser

Posts: 84
Registered: 7/27/00
Re: TComboBox - Select item in drop down without setting text?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 4, 2016 8:40 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
{quote:title=Lajos Juhasz wrote:}

var
  Info: TComboBoxInfo;
begin
  ZeroMemory(@Info, SizeOf(Info));
  Info.cbSize := SizeOf(Info);
  GetComboBoxInfo(ComboBox1.Handle, Info);
  FComboBoxListWnd := Info.hwndList;
end;

Now yuo can send message to change selection in the listbox (wparam is
the itemindex to be selected):

  SendMessage(FComboBoxListWnd, LB_SETCURSEL, lNewIndex, 0);

Excellent, that worked exactly how I wanted.. I was pondering that I needed to talk to the listbox, not the combobox input control, but wasn't sure how to get access to it.
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: TComboBox - Select item in drop down without setting text?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 4, 2016 10:15 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
Lajos Juhasz wrote:

In addition to my previous post you should just have to set
AutoComplete to false. I've tried with the following code
(FComboBoxListWnd is from my previous post):

procedure TForm2.ComboBox1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var lInd: integer;
    lCount: integer;
begin
   lInd:=-1;
   lCount:=0;
   while (lInd=-1) and (lcount<combobox1.items.count) do
   begin
     if CompareText(combobox1.Text, copy(combobox1.items[lcount], 1,
length(combobox1.Text)))=0 then
       lInd:=lCount;
     inc(lcount);
   end;
   SendMessage(FComboBoxListWnd, LB_SETCURSEL, lInd, 0);
 
end;
steven chesser

Posts: 84
Registered: 7/27/00
Re: TComboBox - Select item in drop down without setting text?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 4, 2016 11:06 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
Lajos Juhasz wrote:
Lajos Juhasz wrote:

In addition to my previous post you should just have to set
AutoComplete to false. I've tried with the following code
(FComboBoxListWnd is from my previous post):

procedure TForm2.ComboBox1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var lInd: integer;
    lCount: integer;
begin
   lInd:=-1;
   lCount:=0;
   while (lInd=-1) and (lcount<combobox1.items.count) do
   begin
     if CompareText(combobox1.Text, copy(combobox1.items[lcount], 1,
length(combobox1.Text)))=0 then
       lInd:=lCount;
     inc(lcount);
   end;
   SendMessage(FComboBoxListWnd, LB_SETCURSEL, lInd, 0);
 
end;

Autocomplete is false... but I have a whole convoluted set of stuff that is going on that needed this..

And like your code, its doing that now... finds an index and sets it on the keyup.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02