正如您在我的问题历史记录中看到的那样,我正在开发一个电子书经理,这将是开源的,我将在大约10天内发布它,但是我有一个 TRadioGroup, , 如你看到的:
我的表格上使用的tradiogroup http://img85.imageshack.us/img85/1830/radiogroup.png

我想在变量中存储一些东西(这需要是一个 Integer)将与此“链接” TRadioGroup.

我需要做一个 if 这样的功能:

tradiobutton的标题 - >数字需要存储在变量中

拟合2xWidth-默认值 - > 0
适合2xheight-> 1
适合宽度 - > 2
适合高度 - > 3

但是我只是用了 TRadioGroupTRadioButton 一次,与我使用了20次以上的C#不同。然后,我想知道我需要在IF函数上施加什么,因为它将做什么我已经知道如何做:

var
  num: Integer;

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

我需要放入 if 功能?

PS:我将为在这个小项目中帮助我的人提供学分。

有帮助吗?

解决方案

TradioButton具有签到的财产。但是Tradiogroup具有ItemIndex属性。

传统中的项目使用tstrings存储。因此,您可以将一个对象与每个选项相关联,并且可以将整数施加到要存储的图形。

例子:

// 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;

读取当前设置:

value := radiogroup.ItemIndex;

或获得关联的整数:

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

在您的情况下,值为0到3,因此您可以使用itemIndex。

注意,如果不是函数。函数是根据输入参数返回值的代码。如果是语句,则可以执行的命令。 if语句是特殊的,因为它使您能够基于IF条件执行其他语句。

其他提示

只是一个小技巧:设置 .ItemIndex 不会将键盘焦点发送到无线电项目,我知道如何修复它,提前阅读。

而不是通过设置在radiogroup中选择无线电 .ItemIndex 通过将焦点发送到无线电项目来做到这一点要好得多。只是要清楚:我的意思是将重点发送到无线电项目,而不是整个广播组。

代替: radiogroup.itemindex:=TheIndex;

这样做: TRadioButton(radiogroup.Controls[TheIndex]).SetFocus;

它将使无线电项目被选中,并且 将键盘焦点发送到它, ,因此它将显示虚线的矩形ralood ralood,就像用户单击它一样。

注意1:要在操作中查看它使用键盘光标键,并比较仅设置的behavor .ItemIndex 并将重点发送到无线电项目。

注意2:如果您使用 TRadioButton(radiogroup.Controls[TheIndex]).SetFocus; 那么无需设置 .ItemIndex 根本也将完成。

Hope that helps someone having the same problem as me, when need to set it by code, for example to avoid circular keyboard behavor, for example for making it to stay on last radio item when last radio item is selected and keyboard right cursor is pressed ,第一。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top