Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit item in a listbox 1

Status
Not open for further replies.

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.

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.
 
Code:
ed.setbounds( r.left, r.top-2, lb.clientwidth, r.bottom-r.top+4 );

The code is simply putting a TEdit over the top of the TListBox. It has to set up the display of the control, etc. Walking through the help reveals that this thing is what sets the size of the TEdit control. lb.ClientWidth is the width of the listbox.

I presume you can go from there.

----------
Those who work hard are rewarded with more work and remembered come time to downsize. Those who hardly work are given a paycheck and ignored completely.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top