AdvComboBox how to limit selected item to 1 character

Hello,

I have a AdvComboBox with MaxLength set to 1. Using the OnDropDown event I add my items to the box that can be up to 50 characters in length.

When the user selects the item of their choice I only want the box to display the first character of the item. Right now it selects the right most characters of the item selected.

How do I display only the first character of an item when selected? I thought Max Length of 1 would do this?

Thank you
Chad

This is inherited from the standard VCL TComboBox and afaik, this MaxLength property applies to the combobox style csDropDown to prevent the user from typing a new value longer than MaxLength.

Hello Bruno,

Thank you for the reply.

So how do I handle this scenario?

Items look like this when dropped down:
A - Apple
B - Bravo
C - Charlie
E - Echo
etc.

My combobox is intended to only display one character when not dropped down. Right now when a user makes a selection of A I get "ple" showing in box. I want it to show "A".

Thank you,
Chad

TAdvComboBox can out of the box not display different content for the selected item in the edit part as in the dropdown part.

Just cheat it! :grinning_face_with_smiling_eyes:

procedure TForm1.cmbDropDown(Sender: TObject);
begin
  if cmb.tag > -1 then begin
    cmb.items.Delete(0);
    cmb.ItemIndex := cmb.tag;
  end;
end;

procedure TForm1.cmbDropUp(Sender: TObject);
begin
  cmb.tag := cmb.itemindex;
  cmb.items.Insert(0, cmb.items[cmb.tag].Substring(0, 1));
  cmb.itemindex := 0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  cmb.tag := -1;
end;

Awful, but fun. :grin:
Notice that this way ItemIndex will mostly equal 0.
Must be aware if your ItemIndex &/or Text could be changed by other ways. In this case, you should treat it too.

A more elegant solution (no tricks):
You can use a TColumnComboBox with 2 columns.
In the 1st column you load your letters, in the 2nd column, the descriptions, set a narrow Width, just wide enough to display the chosen letter, and set DropWidth wide enough to show both columns.

image