FileSelectBtnEdit (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

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.

Code

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,
    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;

Uses