doctorjellybean
Programmer
- May 5, 2003
- 145
I want to edit an item directly in a listbox, like one can in a memo. I found this code on the net (Delphi 3000), and it does what I want.
However, I use columns in my listbox. When I select an item to edit, the width of the editbox is that of the listbox, and not the column. I've tried a few ways to change it, but nothing appears to work.
Maybe a guru here can figure it out?
Thank you.
Code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls;
const
UM_DESTROYCONTROL = WM_USER +230;
type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure ListBox1DblClick(Sender: TObject);
private
{ Private declarations }
Procedure EditDone( Sender: TObject );
Procedure UmDestroyControl(Var msg: TMessage);
message UM_DESTROYCONTROL;
public
{ Public declarations }
procedure EditMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.EditDone(Sender: TObject);
begin
listbox1.Items[ listbox1.itemindex ] :=
(Sender As TEdit).Text;
PostMessage( handle, UM_DESTROYCONTROL, 0, Integer(Sender));
end;
procedure TForm1.EditMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
If not PtInrect( (Sender As TControl).ClientRect, Point(X,y) ) Then
EditDone(Sender);
end;
procedure TForm1.ListBox1DblClick(Sender: TObject);
var
r: TRect;
lb: TListbox;
ed: TEdit;
begin
lb := (sender as TListbox);
if lb.ItemIndex < 0 then exit;
ed:= Tedit.Create(self);
ed.Font.Size:=10;
r:= lb.ItemRect( lb.itemindex );
r.topleft := lb.ClientToScreen( r.topleft );
r.BottomRight := lb.clienttoscreen( r.bottomright );
r.topleft := ScreenToClient( r.topleft );
r.BottomRight := screenToClient( r.bottomright );
ed.text := lb.Items[lb.itemindex];
ed.setbounds( r.left, r.top-2,
lb.clientwidth,
r.bottom-r.top+4 );
ed.OnExit := EditDone;
ed.OnMouseDown:= EditMouseDown;
ed.Parent := Self;
SetCapturecontrol( ed );
ed.SetFocus;
end;
procedure TForm1.UmDestroyControl(var msg: TMessage);
begin
TObject(msg.lparam).Free;
end;
end.
However, I use columns in my listbox. When I select an item to edit, the width of the editbox is that of the listbox, and not the column. I've tried a few ways to change it, but nothing appears to work.
Maybe a guru here can figure it out?
Thank you.