Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help please with Radio-group. 2

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA
I have the following radio group which reacts to a BitBtn.
I would prefer to replace the BitBtn with (perhaps) a
re-iterating procedure. Whereby mere selection of a
radio button has the desired effect.

Other than replacing the Radio Buttons with buttons, along with an eventhandler for each, I can't find how to do this.

Can someone please make a suggestion?

procedure TfrmRegister.BitBtn1Click(Sender: TObject);
begin
with RadioGroup1, Items do
begin
if Strings[ItemIndex] = 'Option1' then
begin
Panel2.Visible := False;
Panel3.Visible := False;
Panel1.Visible := True;
end;
if Strings[ItemIndex] = 'Option2 then
begin
Panel1.Visible := False;
Panel3.Visible := False;
Panel2.Visible := True;
end;
if Strings[ItemIndex] = 'Option3 then
begin
Panel1.Visible := False;
Panel2.Visible := False;
Panel3.Visible := True;
end;
end;
end;
 
If I understand what you want to do, then you should be able to remove the BitBtn altogether and add the following code to the RadioGroup1.OnClick event:
Code:
procedure TfrmRegister.RadioGroup1Click(Sender: TObject);
begin
  Panel1.Visible := (RadioGroup1.ItemIndex = 0);
  Panel2.Visible := (RadioGroup1.ItemIndex = 1);
  Panel3.Visible := (RadioGroup1.ItemIndex = 2);
end;
The OnClick event of a RadioGroup responds to any change to the selected item (RadioGroup.ItemIndex) including use of the arrow keys to change the selection.

The previous example assumes that your text 'Option1' is the first element (ItemIndex = 0) of the RadioGroup and that 'Option2' is second (ItemIndex = 1) and 'Option3' is third (ItemIndex = 2). However, if you wanted the code to reflect each item's text value, it would look like this:
Code:
procedure TfrmRegister.RadioGroup1Click(Sender: TObject);
begin
  with RadioGroup1 do begin
    Panel1.Visible := (Items[ItemIndex] = 'Option1');
    Panel2.Visible := (Items[ItemIndex] = 'Option2');
    Panel3.Visible := (Items[ItemIndex] = 'Option3');
  end;
end;
Good Luck!
 
If you had a lot of panels or whatever and they were given the same name as the buttons in your RadioGroup then the following code, based on dhoernig above, would be more concise:
Code:
procedure TForm1.RadioGroup1Click(Sender: TObject);
var
  rg: integer;
  name: string;
  control: TControl;
begin
  for rg := 0 to radiogroup1.items.count - 1 do begin
    control := FindComponent ( radiogroup1.items[rg] ) as TControl;
    control.visible := btn.name = radiogroup1.items [ radiogroup1.itemindex ];
  end;
end;

Note that I've used TControl which allows the same code to be used for Panels, Buttons, Edit boxes and so on. They wouldn't all have to be the same type providing they inherited from TControl.

Andrew


 
Andrew, I think you mean:

[tt]control.visible := control.name = radiogroup1.items [ radiogroup1.itemindex ];[/tt]

Just trying to keep you on your toes! [smile]

Dave
 
Yes, you're quite right, Dave. I tested it out using buttons and manually changed the code.

I should test amended code before posting it.
I should test amended code before posting it.
I should test amended code before posting it.
I should test amended code before posting it.
I should test amended code before posting it.
...

Do they still do lines as a punishment in schools these days?

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top