tags:

views:

443

answers:

4

I want my Form1 to have a Options button that opens up Form2. In Form2, there will be 3 Radio buttons. When a radio button is pushed, I need one of my procedures to check using:

if (RadioButton1.Pushed) then begin

for it to continue with one portion of the code, or if Radiobutton2 is pushed, a different portion, and so on. The thing is, I have no idea where to start. Any suggestions?

+5  A: 

Might be easier to use a RadioGroup. Then, you can just set your options by adding to the Items list in the Object Inspector. You can tell which button has been set by looking at the ItemIndex like:

Case MyRadioGroup.ItemIndex of
  1: DoSomething;
  2: DoSomethingElse;
  3: DoAnotherThing;
End;

You don't have to use a RadioGroup. All the buttons in any windowed control will have the mutual exclusion property that you expect a set of RadioButtons to have.

Jack

Keep in mind that the item index starts counting at zero, not one.
Rob Kennedy
A: 

You can set the OnClick notify event for all the buttons to the same function, then check the sender to see who sent it like so

procedure radioChangeClick(Sender : TObject);
begin        

       if Sender  = Radiobutton1 then //do code
       else if Sender = Radiobutton2 then //do code

end;

You can also check to see if its enabled too in the if if that's a requirement.

Re0sless
This should be if Sender = Radiobutton1 thenSender is an object reference (pointer), so it is guaranteed unique
Gerry
Why have only one change event if you're going to sort them out separately anyway??
Loren Pechtel
It depends on what else you are doing in the change event I guess, if you are updating the main form or doing some other casting ie form1.caption = (sender as TRadioButton ).caption
Re0sless
-1 for syntax errors, not being a method, unnecessary type-casting (YAGNI), and overly complicated type-casting. Not a good example to give someone who's obviously a beginner.
Rob Kennedy
I agree, that's really a lousy example.
DR
I've fixed the unnecessary typecasting and other syntax errors, so its alot cleaner and easier for beginners.
Re0sless
+2  A: 

So to re-phrase your question slightly, you are saying that

Pressing a radio button puts my application into a certain state. Later, based on that state, I want some specific code to run.

When phrased like this it becomes very simple. In the case of Jack's answer, he suggests (quite rightly) that a simple way (to query the state) is to use a Radio Group. The ItemIndex property tells you the state of the buttons.

Tim Jarvis
+2  A: 

You can use this snippet:

if Form2.RadioButton1.Checked then
begin
  // Do something
end else
if Form2.RadioButton2.Checked then
begin
  // Do something else
end;

If this is going to be a bigger application, you should consider creating a global settings object, which can be changed by your options screen and is read by the procedures which need to know about certain settings.

Important: Directly accessing your forms from all over your code just increases coupling. When your application get's a little large it'll be a nightmare to maintain it.

// Form2
Config.DoSomething = RadioButton1.Checked
Config.DoSomethingElse = RadioButton2.Checked

// Form1
if Config.DoSomething then
begin
  // Do something
end else
if Config.DoSomethingElse then
begin
  // Do something else
end;

You could also add methods to your configuration object to save the settings to disk and reload them the next time your application starts.

Others suggested using a RadioGroup, but personally I don't like them as a long term solution, because I find them hard to adapt to my personal UI needs. (Mostly borders and distances) They may also become problematic if someday you want to reorder the items or insert a new item anywhere else than the end: Suddenly ItemIndex 2 means something completly different :) But as a quick-and-dirty solution they sure are useful.

DR