根据触发事件的组合框更新相应的标签 [英] Update corresponding label depending on which combobox fired the event
问题描述
我有一个带有 n
ComboBoxes
和 n
Labels
的程序,我想更新相应的 Label
取决于相邻 ComboBox
中的选择,即 ComboBox2
将更新 Label2
.
I have a program with n
ComboBoxes
and n
Labels
and I want to update the corresponding Label
depending on the selection from the adjacent ComboBox
i.e ComboBox2
would update Label2
.
我为每个 ComboBox
使用相同的事件处理程序,并且当前正在检查 Combobox1
或 Combobox2
是否触发了该事件处理程序.是否可以使用传递给过程的 ComboBox
的 ItemIndex
,例如 Sender.ItemIndex
?当前这不是一个选项,并且给出错误'TObject'不包含名为'ItemIndex'
的成员.
I am using the same event handler for every ComboBox
and currently checking if Combobox1
or Combobox2
has fired the event handler. Is there a way to use the ItemIndex
of the ComboBox
passed to the procedure, such as Sender.ItemIndex
? This is not currently an option and gives the error 'TObject' does not contain a member named 'ItemIndex'
.
procedure TForm2.ComboBoxChange(Sender: TObject);
begin
if Sender = ComboBox1 then
Label1.Caption := ComboBox1.Items.Strings[ComboBox1.ItemIndex]
else
Label2.Caption := ComboBox2.Items.Strings[ComboBox2.ItemIndex];
end;
此代码具有所需的行为,但显然不可扩展.
This code has the desired behavior but is obviously not scale-able.
推荐答案
选项1
这是最坚固的.
Option 1
This is the most robust one.
让您的表单具有私人成员
Let your form have private members
private
FControlPairs: TArray<TPair<TComboBox, TLabel>>;
procedure InitControlPairs;
并在创建表单时(在其构造函数中或在其 OnCreate
处理程序中)调用 InitControlPairs
:
and call InitControlPairs
when the form is created (either in its constructor, or in its OnCreate
handler):
procedure TForm1.InitControlPairs;
begin
FControlPairs :=
[
TPair<TComboBox, TLabel>.Create(ComboBox1, Label1),
TPair<TComboBox, TLabel>.Create(ComboBox2, Label2),
TPair<TComboBox, TLabel>.Create(ComboBox3, Label3)
]
end;
您需要手动将控件添加到此阵列.这就是这种方法的缺点.但是,您只需在这里执行一次即可.然后可以自动完成其他所有操作.
You need to add the controls to this array manually. That's the downside of this approach. But you only need to do this once, right here. Then everything else can be done automagically.
现在,这变得非常不错:让所有组合框共享此 OnChange
处理程序:
Now, this is where it gets really nice: Let all your comboboxes share this OnChange
handler:
procedure TForm1.ComboBoxChanged(Sender: TObject);
var
i: Integer;
begin
for i := 0 to High(FControlPairs) do
if FControlPairs[i].Key = Sender then
FControlPairs[i].Value.Caption := FControlPairs[i].Key.Text;
end;
选项2
忘记任何私有字段.现在改为确保每对都有唯一的 Tag
.因此,第一个组合框和标签都具有 Tag = 1
,第二对组合框具有 Tag = 2
,依此类推.然后,您可以简单地做
Option 2
Forget about any private fields. Now instead make sure that each pair has a unique Tag
. So the first combo box and label both have Tag = 1
, the second pair has Tag = 2
, and so on. Then you can do simply
procedure TForm1.ComboBoxChanged(Sender: TObject);
var
TargetTag: Integer;
CB: TComboBox;
i: Integer;
begin
if Sender is TComboBox then
begin
CB := TComboBox(Sender);
TargetTag := CB.Tag;
for i := 0 to ControlCount - 1 do
if (Controls[i].Tag = TargetTag) and (Controls[i] is TLabel) then
begin
TLabel(Controls[i]).Caption := CB.Text;
Break;
end;
end;
end;
作为共享组合框事件处理程序.不利之处在于,您必须确保控制表单上所有控件的Tag属性(至少与标签具有相同的父级).而且,它们都必须具有相同的父控件.
as the shared combo-box event handler. The downside here is that you must be sure that you control the Tag properties of all your controls on the form (at least with the same parent as your labels). Also, they must all have the same parent control.
这篇关于根据触发事件的组合框更新相应的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!