如何根据框架的子组件的属性设置框架的属性?
我的这个问题的第一个版本可能很具体,所以我会尝试以更一般的方式提问:
我有一个带有组件的框架,假设它是一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
覆盖“已加载”过程怎么样?然后,当所有子组件加载完毕后,扫描它们以查找您需要的子组件?
例如,
如果您需要的组件不是框架的直接后代,您将需要扫描子组件的子组件等。
How about overriding the "Loaded" procedures? Then, when all the child components are loaded, scan them for the one's you need?
e.g.
If the component you need isn't a direct descendant of the frame, you will need to scan the children of the children etc.
我将从框架中删除嵌入的 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.
另一个想法:用代码而不是视觉方式创建 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.