This is the latest version of my new component TGroupRadioButton
in GroupRadioButton.pas (Note the new property GroupIndex
):
unit GroupRadioButton;
interface
uses
System.SysUtils, System.Classes, Vcl.Controls, Vcl.StdCtrls;
type
TGroupRadioButton = class(Vcl.StdCtrls.TRadioButton)
private
{ Private declarations }
FChecked: Boolean;
FGroupIndex: Integer;
procedure SetGroupIndex(const Value: Integer);
protected
{ Protected declarations }
procedure SetChecked(Value: Boolean); override;
function GetChecked: Boolean; override;
procedure CreateWnd; override;
public
{ Public declarations }
published
{ Published declarations }
property GroupIndex: Integer read FGroupIndex write SetGroupIndex;
end;
procedure Register;
implementation
uses
Winapi.Windows, Vcl.ActnList, Winapi.Messages;
{ TGroupRadioButton }
function TGroupRadioButton.GetChecked: Boolean;
begin
Result := FChecked;
end;
procedure TGroupRadioButton.SetChecked(Value: Boolean);
procedure TurnSiblingsOff;
var
I: Integer;
Sibling: TControl;
begin
if Parent <> nil then
begin
with Parent do
begin
for I := 0 to ControlCount - 1 do
begin
Sibling := Controls[I];
if (Sibling <> Self) and (Sibling is TGroupRadioButton) and (TGroupRadioButton(Sibling).GroupIndex = Self.GroupIndex) then
begin
with TGroupRadioButton(Sibling) do
begin
if Assigned(Action) and (Action is TCustomAction) and TCustomAction(Action).AutoCheck then
TCustomAction(Action).Checked := False;
SetChecked(False);
end;
end;
end;
end;
end;
end;
begin
if FChecked <> Value then
begin
FChecked := Value;
TabStop := Value;
if HandleAllocated then
begin
SendMessage(Handle, BM_SETCHECK, WPARAM(Checked), 0);
if not (csLoading in ComponentState) and IsCustomStyleActive and Visible then
SendMessage(Handle, WM_SETREDRAW, 1, 0);
end;
if Value then
begin
TurnSiblingsOff;
inherited Changed;
if not ClicksDisabled then
Click;
end;
end;
end;
procedure TGroupRadioButton.CreateWnd;
begin
inherited CreateWnd;
SendMessage(Handle, BM_SETCHECK, WPARAM(FChecked), 0);
end;
procedure TGroupRadioButton.SetGroupIndex(const Value: Integer);
begin
FGroupIndex := Value;
end;
procedure Register;
begin
RegisterComponents('PASoft', [TGroupRadioButton]);
end;
end.
And this is the package PackageGroupRadioButton.dpk:
package PackageGroupRadioButton;
{$R *.res}
{$IFDEF IMPLICITBUILDING This IFDEF should not be used by users}
{$ALIGN 8}
{$ASSERTIONS ON}
{$BOOLEVAL OFF}
{$DEBUGINFO OFF}
{$EXTENDEDSYNTAX ON}
{$IMPORTEDDATA ON}
{$IOCHECKS ON}
{$LOCALSYMBOLS ON}
{$LONGSTRINGS ON}
{$OPENSTRINGS ON}
{$OPTIMIZATION OFF}
{$OVERFLOWCHECKS ON}
{$RANGECHECKS ON}
{$REFERENCEINFO ON}
{$SAFEDIVIDE OFF}
{$STACKFRAMES ON}
{$TYPEDADDRESS OFF}
{$VARSTRINGCHECKS ON}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$IMAGEBASE $400000}
{$DEFINE DEBUG}
{$ENDIF IMPLICITBUILDING}
{$IMPLICITBUILD ON}
requires
rtl,
vclimg,
vcl,
soaprtl;
contains
GroupRadioButton in 'GroupRadioButton.pas';
end.
So now I have created this demo app:
Here is the DPR:
program Demo;
uses
Vcl.Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Here is the PAS:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, GroupRadioButton, Vcl.StdCtrls;
type
TForm1 = class(TForm)
GroupRadioButton1: TGroupRadioButton;
GroupRadioButton2: TGroupRadioButton;
GroupRadioButton3: TGroupRadioButton;
GroupRadioButton4: TGroupRadioButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
end.
And here is the DFM:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 255
ClientWidth = 392
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -15
Font.Name = 'Segoe UI'
Font.Style = []
Position = poScreenCenter
PixelsPerInch = 120
TextHeight = 20
object GroupRadioButton1: TGroupRadioButton
Left = 61
Top = 140
Width = 277
Height = 21
Margins.Left = 4
Margins.Top = 4
Margins.Right = 4
Margins.Bottom = 4
Caption = 'GroupRadioButton1 (GroupIndex=1)'
TabOrder = 2
GroupIndex = 1
end
object GroupRadioButton2: TGroupRadioButton
Left = 61
Top = 180
Width = 277
Height = 21
Margins.Left = 4
Margins.Top = 4
Margins.Right = 4
Margins.Bottom = 4
Caption = 'GroupRadioButton2 (GroupIndex=1)'
TabOrder = 3
GroupIndex = 1
end
object GroupRadioButton3: TGroupRadioButton
Left = 61
Top = 30
Width = 277
Height = 21
Margins.Left = 4
Margins.Top = 4
Margins.Right = 4
Margins.Bottom = 4
Caption = 'GroupRadioButton3 (GroupIndex=0)'
Checked = True
TabOrder = 0
TabStop = True
GroupIndex = 0
end
object GroupRadioButton4: TGroupRadioButton
Left = 61
Top = 70
Width = 277
Height = 21
Margins.Left = 4
Margins.Top = 4
Margins.Right = 4
Margins.Bottom = 4
Caption = 'GroupRadioButton4 (GroupIndex=0)'
TabOrder = 1
GroupIndex = 0
end
end
Here is a short demonstration video:
