ButtonStyle (Delphi)

From RAD Studio
Jump to: navigation, search


Description

Select the File > New > VCL Forms Application - Delphi.

Drag and drop on the form from the 'ToolPalette' a TButton, a TComboBox and a TPopupMenu. Double click on the TPopupMenu and then click right and Insert on the window that appears or the Ins key. To set a name for each menu item that appears when you will set the button style to bsSplitButton click on the "Caption" option in the Object Inspector and write the name you want. Than double click on the form and it will switch to code editor at the FormCreate procedure.

Code

procedure TForm2.FormCreate(Sender: TObject);
begin
  //ComboBox options
  ComboBox1.AddItem('bsPushButton',nil);
  ComboBox1.AddItem('bsCommandLink',nil);
  ComboBox1.AddItem('bsSplitButton',nil);
end;

Switch back to Design view and select the combobox. In the Object Inspector click on the Events tab and then double click on OnChange event handler and insert this code into the ComboBox1Change procedure:

procedure TForm2.ComboBox1Change(Sender: TObject);
begin
  //set button style when change combobox option
  if ComboBox1.ItemIndex = 0 then
    Button1.Style := bsPushButton;
  if ComboBox1.ItemIndex = 1 then
    Button1.Style := bsCommandLink;
  if ComboBox1.ItemIndex = 2 then
    Button1.Style := bsSplitButton;
end;

From the Design view select the button and double click on the "OnDropDownClick" and put this code into "Button1DropDownClick" procedure:

procedure TForm2.Button1DropDownClick(Sender: TObject);
begin
  //when set style button to bsSplitButton set DropDownMenu
  Button1.DropDownMenu := PopupMenu1;
end;

Uses