Mega Code Archive

 
Categories / Delphi / VCL
 

How to add a built in editor to a TListBox

Title: How to add a built - in editor to a TListBox Question: When using a TListBox and when a user can or must modify an item of the list usually you must add another TEdit control to get the value of the selected item and after the user has set the value he wants you have to update the item by code. Well here is a solution for you to implement an editor that appears on the item you want to edit and lets you edit the item and by pressing enter you can save the value of the item. Answer: {*******************************************************} { } { GT Delphi Components } { ListBox Inplace Editor Demo } { } { Copyright (c) GT Delphi Components } { http://www.gtdelphicomponents.gr } { } { } {*******************************************************} unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) ListBox1: TListBox; Button1: TButton; chkBoxAllowEdit: TCheckBox; procedure ListBox1Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } protected FListBoxEditor : TEdit; procedure DisplayEditor(AListBox : TListBox); procedure InternalOnExit(Sender : TObject); procedure InternalOnKeyPress(Sender : TObject;var Key: Char); public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} {-----------------------------------------------------------------------------} procedure TForm1.ListBox1Click(Sender: TObject); begin //If we allow editing if chkBoxAllowEdit.Checked then begin DisplayEditor(TListBox(Sender)); end; end; {-----------------------------------------------------------------------------} procedure TForm1.DisplayEditor(AListBox: TListBox); begin //Checking if really an item is selected if AListBox.ItemIndex =0 then begin //If the editor is not created we are creating it for the first time if not Assigned(FListBoxEditor) then begin FListBoxEditor := TEdit.Create(Self); FListBoxEditor.Parent := AListBox; FListBoxEditor.OnExit := InternalOnExit; FListBoxEditor.OnKeyPress := InternalOnKeyPress; end; //Setting The Color FListBoxEditor.Color := clSkyBlue; //This is crusial it places the editor exactly at the selected item FListBoxEditor.BoundsRect := AListBox.ItemRect(AListBox.ItemIndex); //Get the text of the item FListBoxEditor.Text := AListBox.Items[AListBox.ItemIndex]; //Show the editor; FListBoxEditor.Visible := True; end; end; {-----------------------------------------------------------------------------} procedure TForm1.InternalOnExit(Sender: TObject); begin //TEdit(Sender).Visible := False; end; {-----------------------------------------------------------------------------} procedure TForm1.InternalOnKeyPress(Sender: TObject; var Key: Char); begin //if the Enter key is pressed if key = #13 then begin //Set the new text for the selected item ListBox1.Items[ListBox1.ItemIndex] := TEdit(Sender).Text; //hide the editor TEdit(Sender).Visible := False; //Set focus to the listbox ListBox1.SetFocus; ListBox1.ItemIndex :=ListBox1.ItemIndex; chkBoxAllowEdit.Checked := False; end; end; {-----------------------------------------------------------------------------} procedure TForm1.Button1Click(Sender: TObject); begin Close; end; {-----------------------------------------------------------------------------} end.