Delphi 10.1 BelrinのFireMonkeyアプリケーションで文字列を選択するリストピッカーを表示するには

リストピッカーはIFMXPickerServiceのCreateListPickerメソッドで作成できます。

uses
  FMX.Platform;

  if TPlatformServices.Current.SupportsPlatformService(IFMXPickerService, PickerService) then
  begin
    FListPicker := PickerService.CreateListPicker;
    …

作成したリストピッカーのイベントや選択項目を設定します。

FListPicker.Parent := Button1;

// イベントの設定
FListPicker.OnValueChanged := DoListPickerValueChanged;
FListPicker.OnHide := DoListPickerHide;
FListPicker.OnShow := DoListPickerShow;

// 選択項目の設定
FListPicker.Values.Add('Delphi');
FListPicker.Values.Add('C++Builder');
FListPicker.Values.Add('RAD Studio');

// 選択されている項目の設定
FListPicker.ItemIndex := 1;

Showメソッドを呼び出すと、リストピッカーを表示できます。

FListPicker.Show;

サンプルアプリケーション

フォームにボタン(TButton)とメモ(TMemo)を配置します。

interfaceのusesにFMX.Pickersを追加します。

interface

uses
…, FMX.Pickers; //追加

フォームにprivate変数を追加します。

type
TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
private
    { private 宣言 }
    FListPicker: TCustomListPicker; //追加

フォームのOnCreateイベントを追加します。

リストピッカーを作成し、イベントや選択項目を設定します。

uses
  FMX.Platform;

procedure TForm1.FormCreate(Sender: TObject);
var
PickerService: IFMXPickerService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXPickerService, PickerService) then
  begin
    FListPicker := PickerService.CreateListPicker;
    FListPicker.Parent := Button1;

    // イベントの設定
    FListPicker.OnValueChanged := DoListPickerValueChanged;
    FListPicker.OnHide := DoListPickerHide;
    FListPicker.OnShow := DoListPickerShow;

    // 選択項目の設定
    FListPicker.Values.Add('Delphi');
    FListPicker.Values.Add('C++Builder');
    FListPicker.Values.Add('RAD Studio');

    // 選択されている項目の設定
    FListPicker.ItemIndex := 1;
  end;
end;

リストピッカーのイベントを追加します。
イベントが発生するとMemo1に出力します。

type
TForm1 = class(TForm)
  Button1: TButton;
  Memo1: TMemo;
  procedure FormCreate(Sender: TObject);
private
  { private 宣言 }
  FListPicker: TCustomListPicker;
  procedure DoListPickerValueChanged(Sender: TObject; const AValueIndex: Integer); // 追加
  procedure DoListPickerHide(Sender: TObject); // 追加
  procedure DoListPickerShow(Sender: TObject); // 追加
public
  { public 宣言 }
end;

procedure TForm1.DoListPickerHide(Sender: TObject);
begin
  Memo1.Lines.Add('DoListPickerHide');
end;

procedure TForm1.DoListPickerShow(Sender: TObject);
begin
  Memo1.Lines.Add('DoListPickerShow');
end;

procedure TForm1.DoListPickerValueChanged(Sender: TObject; const AValueIndex: Integer);
begin
  Memo1.Lines.Add('DoListPickerValueChanged AValueIndex=' + IntToStr(AValueIndex));
end;

ボタン(Button1)のOnClickイベントを追加します。
ボタンを押すとリストピッカーを表示します。

procedure TForm1.Button1Click(Sender: TObject);
begin
  FListPicker.Show;
end;

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください