Display on the ComboBox

In the Combobox Item, It has items, like 'SBW    Seacon Bonded Warehouse ', all lines start with 3 characters.
When I select it, I wish the box shows the first 3 characters only. How can do this?
Thank you.
petershaw9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
hi petershaw9,

this will do it, if you leave the combobox

procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
  combobox1.Text := copy(combobox1.Items[combobox1.ItemIndex],1,3);
end;

meikl
0
simonetCommented:
Following...®
0
victor_christovCommented:
Hi petershaw9,

I had the same problem 2 mounths ago. Proposed by Kretzschmar way was not acceptable for me, so I applied a diffrent approach.

I use 3 component for this purpose : TEdit, TSpeedButton and TComboBox. TEdit is ReadOnly, TComboBox is initially unvisible. TSpeedButton activates TComboBox. Here is a simple code of my program. I hope it will be usefull for you.

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Edit1: TEdit;
    SpeedButton1: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  With Edit1 do begin
     Left := 56; Top := 112 ; Width := 156 ;
     ReadOnly := True ;
  end;
  With SpeedButton1 do begin
     Left := 212 ; Top := 112 ; Caption := 'u' ;
     Font.Name := 'Marlett' ;
     Font.Size := 10        ;
  end;
  With ComboBox1 do begin
     Left := 56; Top := 112 ; Width := 173 ;
     Visible := False ;
  end;


end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
Var i,Ix : Integer ;
begin
  With ComboBox1 do begin
    Visible := True ;
    Ix := -1 ;
    With Items do
      for i := 0 to Count -1 do
        if Copy(Strings[i],1,3)= Trim(Edit1.Text) then
           Ix := i ;
    ItemIndex := Ix;
    SetFocus;
    KeyBd_Event(Vk_F4,0,0,0);
    KeyBd_Event(Vk_F4,0,KEYEVENTF_KeyUp,0);

  end;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  With ComboBox1 do begin
    Edit1.Text := Copy(Text,1,3);
    Visible := False ;
  end;
end;

End.



Regards Victor.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
men xinCEOCommented:
Is it your means?

set combobox1's style property =csOwnerDrawFixed

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  ComboBox1.Canvas.FillRect(rect);
  if State=[odselected,odfocused,odComboboxEdit] then begin
    ComboBox1.Canvas.Textout(rect.left,Rect.top,ComboBox1.Items[Index]);
    exit;
  end;
  if state=[odselected,odfocused] then begin
    ComboBox1.Canvas.Textout(rect.left,Rect.top,copy(ComboBox1.Items[Index],0,3));
  end else begin
    ComboBox1.Canvas.TextOut(Rect.Left,Rect.top,ComboBox1.Items[Index]);
  end;
end;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.