开发者

Delphi: TabStop problems of TRadioButton

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

If you will try to switch focus between many开发者_如何学JAVA 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)


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.


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.


I have found an interesting article of Craig Stuntz about this problem. As I can see, I'll need to create my own control to solve it.


By default only one RadioButon has property TabStop = True; All Radiobuttons are treated as one controll. When radiobutton has focus you can switch beetween radiobutons using arrow up and down.

Now when user choose one option they can press tab to switch to another controll (without changing radio options).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜