VCL Delphi

Title: How to place a TEdit inside a TListBox to simulate inplace editing of ListBox items
By design, a TListBox component displays a collection of items (string values) in a scrollable list. Items of a ListBox cannot be edited directly from "inside" the ListBox. If you ever needed to enable inplace editing of ListBox strings, here's a trick to place an ordinary TEdit inside a ListBox.
We grab the idea from a series of articles on placing any component into a cell of a DBGrid.
In short, we dynamically create a TEdit component named "ListEdit" in the Form's OnCreate event. ListBox1 (the name of the TListBox component already on the form) has set to be ListEdit's parent (in the TEdit's constructor "Self", the Form, was assigned to be the Owner). Hint: Owner vs. Parent.
Next, in the OnClick event of the ListBox1 (when an item gets selected) we place the edit right on top of it.
The ListEdit has the focus, when enter key is pressed (hint Enter Key = #13) the "old" item is removed from the ListBox, the new item is inserted.
That's all. Tricky and simple.
~~~~~~~~~~~~~~~~~~~~~~~~~
type
TForm1 = class(TForm)
...
private
ListEdit : TEdit;
procedure ListEditKeyPress(Sender: TObject; var Key: Char) ;
end;
...
//create the TEdit and make ListBox its parent
procedure TForm1.FormCreate(Sender: TObject) ;
begin
ListEdit := TEdit.Create(self) ;
ListEdit.Visible := false;
ListEdit.Ctl3D := false;
ListEdit.BorderStyle := bsNone;
ListEdit.Parent := ListBox1;
ListEdit.Width := ListBox1.ClientWidth;
ListEdit.OnKeyPress := ListEditKeyPress;
end;
//ListView Item selected - position the Edit
procedure TForm1.ListBox1Click(Sender: TObject) ;
var
ii : integer;
lRect: TRect;
begin
ii := ListBox1.ItemIndex;
if ii = -1 then exit;
lRect := ListBox1.ItemRect(ii) ;
ListEdit.Top := lRect.Top + 1;
ListEdit.Left := lRect.Left + 1;
ListEdit.Height := (lRect.Bottom - lRect.Top) + 1;
ListEdit.Text := ListBox1.Items.Strings[ii];
ListBox1.Selected[ii] := False;
ListEdit.Visible := True;
ListEdit.SelectAll;
ListEdit.SetFocus;
end;
//apply editing when enter key is pressed
procedure TForm1.ListEditKeyPress(Sender: TObject; var Key: Char) ;
var
ii: Integer;
begin
if Key = #13 then
begin
ii := ListBox1.ItemIndex;
ListBox1.Items.Delete(ii) ;
ListBox1.Items.Insert(ii, ListEdit.Text) ;
ListEdit.Visible := False;
Key := #0;
end;
end;
//hide Edit when ListBox looses the focus
procedure TForm1.ListBox1Exit(Sender: TObject) ;
begin
ListEdit.Visible := false;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~