FMX.ExtCtrls.TPopupBox Sample
Contents
Description
This example shows how to use some properties of a TPopupBox component.
To test this example, you need to:
- Create a new application:
- Delphi: File > New > Multi-Device Application - Delphi
- C++Builder: File > New > Multi-Device Application - C++Builder
- Choose Blank Application.
- Add to the form:
- A TPopupMenu component and fill it with several items.
- A TPopupBox component.
- Three TButton components.
- Change the text of the first button to Show PopupMenu, the second to Add Item and the third to Remove Item.
- Add the following code for the buttons' OnClick events and for the form's OnCreate event.
Code
- When you run the application, the FormCreate procedure is called. In this example, the TPopupBox component is populated with four items, and the PopupMenu property is set to the TPopupMenu component.
Note: The TPopupMenu component only works for desktop applications.
Delphi:
procedure TForm1.FormCreate(Sender: TObject);
begin
//Add the items
PopupBox1.Items.Add('Item1');
PopupBox1.Items.Add('Item2');
PopupBox1.Items.Add('Item3');
PopupBox1.Items.Add('Item4');
//Set the first item to be shown
PopupBox1.ItemIndex := 0;
//Set the PopupMenu property - only for desktop applications
PopupBox1.PopupMenu := PopupMenu1;
end;
C++Builder
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//Add the items
PopupBox1->Items->Add("Item1");
PopupBox1->Items->Add("Item2");
PopupBox1->Items->Add("Item3");
PopupBox1->Items->Add("Item4");
//Set the first item to be shown
PopupBox1->ItemIndex = 0;
//Set the PopupMenu property - only for desktop applications
PopupBox1->PopupMenu = PopupMenu1;
}
- The Show PopupMenu OnClick event:
Delphi:
procedure TForm1.Button1Click(Sender: TObject);
begin
//Set the position of the PopupMenu the same as the position of the PopupBox
PopupMenu1.Popup(PopupBox1.Position.Point.X, PopupBox1.Position.Point.Y);
end;
C++Builder
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//Set the position of the PopupMenu the same as the position of the PopupBox
PopupMenu1->Popup(PopupBox1->Position->Point.X, PopupBox1->Position->Point.Y);
}
- The Add Item OnClick event:
Delphi:
procedure TForm1.Button2Click(Sender: TObject);
begin
//Adding items with corresponding numbers
PopupBox1.Items.Add('Item' + (PopupBox1.Items.Count + 1).ToString());
end;
C++Builder
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//Adding items with corresponding numbers
PopupBox1->Items->Add("Item" + IntToStr(PopupBox1->Items->Count + 1));
}
- The Remove Item OnClick event:
Delphi:
procedure TForm1.Button3Click(Sender: TObject);
begin
//Remove the selected item
PopupBox1.Items.Delete(PopupBox1.ItemIndex);
end;
C++Builder
void __fastcall TForm1::Button3Click(TObject *Sender)
{
//Remove the selected item
PopupBox1->Items->Delete(PopupBox1->ItemIndex);
}