its*_*ols 0 delphi control-array element freepascal lazarus
我在表单上有一组PANEL,它们用作按钮.分配了一个事件过程.所以当我点击一个按钮时,我可以看到它的标题:
procedure TForm1.MenuAction0Click(Sender: TObject);
begin
TPanel(Sender).Font.Bold:= true;
ShowMessage( TPanel(Sender).Caption);
end;
Run Code Online (Sandbox Code Playgroud)
我想知道按钮编号(如数组元素编号)而不是标题.这怎么可能?
谢谢!
如果你的按钮在一个数组中,那是因为你把它放在一个数组中.该按钮没有数组的固有知识,在您的程序中也没有任何其他功能.要在数组中找到按钮,请搜索它:
function GetButtonArrayIndex(const ButtonArray: array of TButton; Button: TButton): Integer;
begin
for Result := 0 to High(ButtonArray) do
if ButtonArray[Result] = Button then
Exit;
Result := -1;
end;
Run Code Online (Sandbox Code Playgroud)
另一种方法是放弃对数组的任何直接操作,只将按钮的数组索引存储在其Tag属性中.
如果您已经在使用Tag其他东西,或者您不喜欢它的名称在程序中没有表明它的特定用途,那么您可以使用a TDictionary<TButton, Integer>来将按钮映射到数组索引而无需搜索数组:只需查看从给定按钮上升索引.一旦你使用了a TDictionary,你就可以跳过数组索引并直接将按钮映射到数组索引应该指向的任何其他内容,例如保存与按钮相关的信息的数据结构.
| 归档时间: |
|
| 查看次数: |
1082 次 |
| 最近记录: |