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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using CASE statments with radio button group? 1

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
0
0
AU
I have a group of about 10 radio buttons. My button click method checks which one is checked, e.g.
Code:
if RadioButton1.checked then
   begin
    ....
   end
else if RadioButton2.checked then
   begin
   ....
   end
else ....
I like having neat code and this way is messy.

Is there a way you can use a case statment like in VB, e.g.
Code:
Select Case Checked 
Case is RadioButton1
case ...

Thanks in advance,
Adam




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Use the ItemIndex property to find which is chosen. viz:
Code:
  ChosenItem := RadioGroup1.ItemIndex; // its a zero based array
  case ChosenItem of 
    0: // its a zero based array 
      begin
        beep; // do something!
      end;
    1: 
      begin
        beep;beep; // do something!
      end;
    2:
      begin
        beep;beep;beep; // do something!
      end;
    3:
      begin
        beep;beep;beep;beep; // do something!
      end;
  end;
  Label1.Caption := 'You chose item ' + inttostr(ChosenItem + 1);

Hope that helps.....

Cheers,
Chris [pc2]
 
Radio buton checked? I am afraid that this property does not exist, it is a property of checkboxes

use ItemIndex to determine which item is selected
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  case radiogroup1.ItemIndex of
  0: label1.Caption := 'first';
  1: label1.Caption := 'second';
  2: label1.Caption := 'third';
  end   //case
end;

Steven
 
Steven,

Checked is a property of TRadioButton (at least it is in Delphi 7 and I am fairly sure it has always been a property of TRadioButton).


Adam,

An alternative to using the ItemIndex property in a case statement to call the required processing is to let Delphi do this work. Code an OnClick event handler for each of the Radio buttons. You may find that this approach produces clearer and more maintainable code as case statements can sometimes get big and messy.

For example, if you wanted your radio buttons to change the form's caption:
Code:
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  Form1.Caption := 'radio button 1 pressed';
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  Form1.Caption := 'radio button 2 pressed';
end;

procedure TForm1.RadioButton3Click(Sender: TObject);
begin
  Form1.Caption := 'radio button 3 pressed';
end;

Andrew
Hampshire, UK
 
Thanks guys. Problem solved




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Andrew, I didn't formulated my statement very well.
The RadioButton has indeed the property checked
The RadioGroup doesn't
The checked in the first is used to activate (fill) the radiobutton.

In the RadioButtonGroup the itemIndex is used, since only one radiobutton can be selected (or decision) , otherwise you have to use the CheckListBox (or/and decision)

I mixed the terms RadioButton and RadioGroup which are two distinct components.

By the way AP81, which component you need?

Fiddling around with radiobuttons, I discovered that checking (clicking) on one, clears another. The drawback is trhat you have to write a separate handler for each. (Towerbase example).

Another disadvantage is that if you have 20 radiobuttons on a form, only one can be selected. But at least you are free to place them anywhere.

Steven
 
Hi everyone
Another disadvantage is that if you have 20 radiobuttons on a form, only one can be selected. But at least you are free to place them anywhere.
Small correction here: cleared only RadioButtons placed on the same control (as far as i remember), i.e. if you place some RBs on a panel and some on the form then you'll have two "groups" of RBs.

HTH
--- McMerfy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top