Reputation: 3057
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?
Upvotes: 1
Views: 945
Reputation: 72504
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 config
uration 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.
Upvotes: 3
Reputation: 18815
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.
Upvotes: 2
Reputation: 2237
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
Upvotes: 5