如何根据框架的子组件的属性设置框架的属性?

发布于 2024-10-04 09:25:06 字数 1056 浏览 6 评论 0原文

我的这个问题的第一个版本可能很具体,所以我会尝试以更一般的方式提问:

我有一个带有组件的框架,假设它是一个 TButton。将框架放置在表单上后,我更改了组件的属性,假设我将按钮上的标签设置为 100。

在框架的构造函数中,我想做这样的事情:

constructor TMyFrame.Create(AOwner: TComponent);
begin
  inherited;

  if Button1.Tag = 100
    then DoSomething
    else DoSomethingElse;
end;

我发现此时(在创建)按钮的标签仍然是0。有人可以推荐另一种方法来做到这一点吗?

在我们的应用程序中,用户可以在多个位置输入 SQL 语句。为了实现这一点,我们使用 SynEdit 组件。我们为此目的创建了一个框架,

并具有一些扩展功能。 有时我们需要数据感知版本 (TDBSYnEdit),有时我们需要“常规”版本 (TSynEdit)。我们通过在框架上设置页面控件并在设计时切换到正确的页面来解决这个问题。选项卡是隐藏的,因此用户不知道发生了这种情况。

问题是有时我们的开发人员忘记在页面控件上设置正确的页面,或者不小心选择了错误的页面。

我想通过将以下代码添加到框架上的 Create 事件来解决此问题

if DBSQLMemo.DataField > ''
  then pcMemos.ActivePage := tsDataAware
  else pcMemos.ActivePage := tsNonDataAware;

我的理论是,如果开发人员设置编辑器的数据源/数据字段属性,则框架应该在“数据感知模式”下使用,并且数据-感知编辑器应该是可见的。事实证明,在创建时这些属性尚未设置。

有人对我应该如何解决这个问题有好的建议吗?

My first version of this question may have been to specific, so I will try to ask in a more general way:

I have a frame with a component on it, let's say it's a TButton. After placing the frame on the form I change a property of the component, let's say I set the tag on the button to 100.

In the frames' constructor I would like to do something like this:

constructor TMyFrame.Create(AOwner: TComponent);
begin
  inherited;

  if Button1.Tag = 100
    then DoSomething
    else DoSomethingElse;
end;

I found out that at this time (during creation) the tag of the button is still 0. Can someone recommend another way to do this?


In our application there are several places where the users can enter SQL statements. To facilitate this we use the SynEdit component. We have created a frame for this purpose, with some extended functionality.

Sometimes we need the data-aware version (TDBSYnEdit), and at other times we need the "regular" version (TSynEdit). We have solved this by having a pagecontrol on the frame, and switch to the correct page at design time. The tabs are hid, so the user has no idea this happens.

The problem is that sometimes our developers forget to set the correct page on the page control, or accidentaly selects the wrong page.

I wanted to fix this by adding the following code to the Create event on the frame

if DBSQLMemo.DataField > ''
  then pcMemos.ActivePage := tsDataAware
  else pcMemos.ActivePage := tsNonDataAware;

My theory was that if a developer set the datasource/datafield properties of the editor the frame should be used in "data-aware mode", and the data-aware editor should be visible. As it turns out, at the moment of creation these properties aren't set.

Does anyone have a good suggestion to how I should solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄断弥空 2024-10-11 09:25:06

覆盖“已加载”过程怎么样?然后,当所有子组件加载完毕后,扫描它们以查找您需要的子组件?

例如,

interface

...
protected
  procedure Loaded; override;
...

implementation

procedure Loaded;
var
  i: Integer;
begin
  inherited;

  for i := 0 to pred(Self.ComponentCount) do
    if Self.Components[i] is TSynEdit then
    begin
      // do something
    end;
end;

如果您需要的组件不是框架的直接后代,您将需要扫描子组件的子组件等。

How about overriding the "Loaded" procedures? Then, when all the child components are loaded, scan them for the one's you need?

e.g.

interface

...
protected
  procedure Loaded; override;
...

implementation

procedure Loaded;
var
  i: Integer;
begin
  inherited;

  for i := 0 to pred(Self.ComponentCount) do
    if Self.Components[i] is TSynEdit then
    begin
      // do something
    end;
end;

If the component you need isn't a direct descendant of the frame, you will need to scan the children of the children etc.

灰色世界里的红玫瑰 2024-10-11 09:25:06

我将从框架中删除嵌入的 SynEdit,并为框架提供一个属性引用到 SynEdit 实例。这样,您就不会总是拥有无用的 SynEdit 实例,用户甚至可能决定使用您甚至没有想到的 SynEdit 衍生产品。

I would remove the embedded SynEdit from the frame and instead give the frame a property referring to a SynEdit instance. That way you don't always have an useless SynEdit instance and the user might even decide to use a SynEdit derivative you didn't even think of.

青衫负雪 2024-10-11 09:25:06

另一个想法:用代码而不是视觉方式创建 SynEdit 怎么样? (取决于您想通过表单设计器对其进行多少更改。)然后您可以删除 PageControl,并且每帧只有一个编辑器实例。

Another idea: How about creating the SynEdit in code instead of visually? (Depends on how much you want to change it via the form designer.) Then you could drop the PageControl and have only one editor instance per frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文