FileSelectBtnEdit (Delphi)
Description
This example demonstrates the use of a TButtonedEdit edit control. The code will load two bitmaps located as resources in the executable and generate an image list at run time. The two loaded images are then used for the left and right buttons of the edit control.
Note: Add a TButtonedEdit and a TOpendialog component before implementing the code below.
Code
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TMainForm = class(TForm)
OpenDialog: TOpenDialog;
edtSelectFile: TButtonedEdit;
function MakeImageList(const ResNames: array of String): TImageList;
procedure EditLeftButtonClick(Sender: TObject);
procedure EditRightButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
function TMainForm.MakeImageList(const ResNames: array of String): TImageList;
var
ResBmp: TBitmap;
I: Integer;
begin
{ Create an image list. }
Result := TImageList.Create(Self);
for I := 0 to Length(ResNames) - 1 do
begin
{ Create a new bitmap image. }
ResBmp := TBitmap.Create();
{ Try to load the bitmap from the resource. }
try
ResBmp.LoadFromResourceName(HInstance, ResNames[I]);
ResBmp.Transparent := true;
except
ResBmp.Free();
Result.Free();
Exit;
end;
Result.Add(ResBmp, nil);
end;
end;
procedure TMainForm.EditLeftButtonClick(Sender: TObject);
begin
{ Clear the contents of the edit box. }
edtSelectFile.Clear;
end;
procedure TMainForm.EditRightButtonClick(Sender: TObject);
begin
{ Get the text from the edit. }
OpenDialog.FileName := edtSelectFile.Text;
if OpenDialog.Execute then
edtSelectFile.Text := OpenDialog.FileName;
end;
procedure TMainForm.FormCreate(Sender: TObject);
var
RightBtn: TEditButton;,
LeftBtn: TEditButton;
begin
{ Assign a new image list, loaded from resources. }
edtSelectFile.Images := MakeImageList(
['OpenFile_Image', 'ClearFile_Image']);
{ Register right and left buttons for the edit. }
RightBtn := edtSelectFile.RightButton;
RightBtn.ImageIndex := 0;
RightBtn.Visible := true;
LeftBtn := edtSelectFile.LeftButton;
LeftBtn.ImageIndex := 1;
LeftBtn.Visible := true;
{ Register OnClick events for the buttons. }
edtSelectFile.OnRightButtonClick := EditRightButtonClick;
edtSelectFile.OnLeftButtonClick := EditLeftButtonClick;
end;
end.
Tip: When implementing, remember you may need to refactor the components you previously added.
Warning: You may not be able to see the images, the reason is that they are too small to view them. As a workaround, close and save the project, then reopen RAD Studio.
Uses
- Vcl.Controls.TImageList ( fr | de | ja )
- Vcl.ImgList.TCustomImageList.Add ( fr | de | ja )
- Vcl.Graphics.TBitmap ( fr | de | ja )
- Vcl.Graphics.TBitmap.Create ( fr | de | ja )
- Vcl.Graphics.TBitmap.LoadFromResourceName ( fr | de | ja )
- Vcl.Graphics.TGraphic.Transparent ( fr | de | ja )
- Vcl.StdCtrls.TCustomEdit.Clear ( fr | de | ja )
- Vcl.ExtCtrls.TEditButton ( fr | de | ja )
- Vcl.ExtCtrls.TEditButton.Visible ( fr | de | ja )
- Vcl.ExtCtrls.TEditButton.ImageIndex ( fr | de | ja )
- Vcl.ExtCtrls.TButtonedEdit ( fr | de | ja )
- Vcl.ExtCtrls.TCustomButtonedEdit.Images ( fr | de | ja )
- Vcl.ExtCtrls.TCustomButtonedEdit.RightButton ( fr | de | ja )
- Vcl.ExtCtrls.TCustomButtonedEdit.LeftButton ( fr | de | ja )
- Vcl.ExtCtrls.TCustomButtonedEdit.OnRightButtonClick ( fr | de | ja )
- Vcl.ExtCtrls.TCustomButtonedEdit.OnLeftButtonClick ( fr | de | ja )