delphi - Transform a string to an object pointer?
问题描述
I have a string 'MyButton'
.
How can I get the OBJECT MyButton
from the STRING 'MyButton'
, so that I could write:
MyButton.Caption := 'My new Caption';
This would change the caption of the TButton
MyButton object instance.
解决方案
If the component has an Owner
assigned (as all components placed at design-time do), then you can use the Owner's FindComponent()
method, eg:
procedure TMyForm.DoSomething;
var
Cmp: TComponent;
begin
Cmp := Self.FindComponent('MyButton');
if Cmp <> nil then
(Cmp as TButton).Caption := 'My new Caption';
end;
推荐阅读
- node.js - Linux 中 package.json runtimeDependencies 的 url 资源存储在哪里?
- python-3.x - 加载一个大的 tif 文件,但在 openCV 中只加载第一个切片
- php - 重用 php 和 mysql 代码,但在新项目“警告未定义数组键”中出错
- angular - 如何在 Angular Typescript 中重新加载页面以使用 BE“等待”HTTP 调用
- python - 这个错误是什么意思?以及如何解决?
- python-3.x - 无法使用 opencv 读取 .tdms 文件
- lua - 无法打开 train.lua:没有这样的文件或目录
- javascript - 无法获取未定义或空引用的属性“customXmlParts”
- javascript - 取决于所选值还显示此数组中的其他值 - javascript
- python - 为什么依赖 django 的 dropdownn 不起作用