Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: TabStop problems of TRadioButton

When TRadioButton has TabStop=True, it's acting very strange.

If you will try to switch focus between many radio buttons on a form using Tab key, you would do it only 1 time per app session. The tabulation is one-way, never returning back to the first radio button. Also when the focus is moving across radio buttons, they becoming "checked" automatically.

Can this behavior be fixed without creating my own component?

I want standard radio buttons to

  1. switch focus cyclically
  2. prevent radio button from checking when the focus comes into it (I want my users to check them using Space key)
like image 255
Andrew Avatar asked Oct 24 '22 11:10

Andrew


2 Answers

I understand that you're working with existing code, which is a real world constraint that's too often dismissed in these forums.

Sounds to me like checkboxes would suit you better. You can enforce the exclusivity normally expected of RadioButtons in the OnChecked event. That should solve your tabbing/focus and selection/deselection issues.

Checkboxes won't be checked automatically upon receiving focus, and your users can check/uncheck them with the space key.

like image 55
Sam Avatar answered Oct 30 '22 16:10

Sam


You can put code in the OnEnter event to prevent the checkbox from selecting.
You'll need to store the previously selected RadioButton somehow though.

var
  SelectedRadioButton: TRadioButton;

//event shared by all radiobuttons
procedure TForm1.RadioButton1Enter(Sender: TObject);
begin
  if Sender <> SelectedRadioButton then begin
    SelectedRadioButton.Checked:= true;
  end;
end;

procedure TFrameOrder.RadioButton1Click(Sender: TObject);
begin
  SelectedRadioButton:= (Sender as TRadioButton);
end;

procedure TFrameOrder.RadioButton1KeyPress(Sender: TObject; var Key: Char);
var
  MyRadioButton: TRadioButton;
begin
  MyRadioButton:= (Sender as TRadioButton);
  if Key in [#32,#13] then begin 
    MyRadioButton.Checked:= true;
    RadioButton1Click(MyRadioButton);
  end; {if}
end;

It probably clearer to create a new TMyRadioButton component though because this will clutter up your regular code.

like image 25
Johan Avatar answered Oct 30 '22 16:10

Johan