Which switch is selected in the TRADIOGroup?

As you can see in my background, I’m developing an e-book manager that will be open source, and I will release it in about 10 days, but I have TRadioGroup , as you can see: <sh> TRADIOGroup Used in my http form : //img85.imageshack.us/img85/1830/radiogroup.png

And I want to save something in a variable (which should be an Integer ) that will be "linked" to this TRadioGroup .

I need to make an if function as follows:

Caption of the TRadioButton β†’ Number to be stored in a variable

Fit 2xWidth - Default -> 0
Fit 2xHeight β†’ 1
Fit Width β†’ 2
Height Height β†’ 3

But I just used TRadioGroup and TRadioButton once, different from C #, which I used more than 20 times. Then I want to know what I need to enable the if function, because I will know how to do it:

 var num: Integer; begin if(TRadioButton1 checked?) begin num := 0; end; end. 

What do I need to insert in the brackets of the if function?

PS: I am going to put loans in the program for people who helped me in this small project.

+6
integer radio-button delphi lazarus radio-group
source share
2 answers

TRadioButton has a Checked property. But TRADIOGroup has an ItemIndex property.

Elements in a TRadioGroup are stored using TStrings. That way, you can associate an object with each option, and you can distinguish an integer from a TObject for storage.

Example:

 // fill the radiogroup radiogroup.Items.AddObject('Fit 2xWidth', TObject(0)); radiogroup.Items.AddObject('Fit 2xHeight', TObject(1)); radiogroup.Items.AddObject('Fit Width', TObject(2)); radiogroup.Items.AddObject('Fit Height', TObject(3)); radiogroup.ItemIndex := 0; 

To read the current setting:

 value := radiogroup.ItemIndex; 

Or get the associated integer:

 index := radiogroup.ItemIndex; Assert(index>=0); // Sanity check value := Integer(radiogroup.Items.Objects[index]); 

In your case, the values ​​are from 0 to 3, so you can use ItemIndex.

As a note, if this is not a function. A function is a piece of code that returns a value based on input parameters. If it is an operator that is a command that can be executed. The if statement is special because it allows you to execute another statement based on the if condition.

+9
source share

A TIP: Setting .ItemIndex does not send keyboard focus to a radio element, I know how to fix it, read on.

Instead of selecting the radio code in the radio group by installing .ItemIndex , it is much better to do this by sending focus to the radio element; just to be very clear: I mean sending focus only to radio control, and not to the entire group of radio stations.

Instead: radiogroup.itemindex:=TheIndex;

Do it like this: TRadioButton(radiogroup.Controls[TheIndex]).SetFocus;

The radio point will be selected and send it the keyboard focus , so it will display a dotted rectangle as if the user clicked on it.

Note 1. To see it in action, use the arrow keys and compare the action only with the .ItemIndex setting and sending focus to the radio node.

TRadioButton(radiogroup.Controls[TheIndex]).SetFocus; : If you use TRadioButton(radiogroup.Controls[TheIndex]).SetFocus; , then there is no need to install .ItemIndex at all, this will also be done.

We hope this helps someone having the same problem as me when you need to install it by code, for example, to avoid a circular keyboard, for example, to make it stay on the last element of the radio when the last element of the radio station is selected and right keyboard cursor pressed, same for first.

+5
source share

All Articles