delphi 在 ListBox 中放置一��可�� item 的 TEdit �M件


本文整理自网络,侵删。

  
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;

相关阅读 >>

Delphi编程之显示桌面分辨率

Delphi字符串隐藏

Delphi 遍历某字段并插入到combobox

Delphi程序只允许运行一个实例的三种方法

Delphi 每年、月、周、日的开始与结束的时间

Delphi txt日志log

Delphi 7 + gdiplus 简单实现双缓冲绘制移动图形

Delphi idftp连不上ftp服务器的解决方法

sqlite报错database is locked的解决办法

Delphi 多线程(tthread类的实现)实例

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...