views:

68

answers:

2

is there a way in delphi 2009 to make form controls private? eg: if i add a label at design time, i'd like for it to impossible for code outside the form to access the label.

(does delphi 2010 have this?)

A: 

control is field of form. put it in private section:

  TForm1 = class(TForm)
  Private 
    TLabel label;
  Public
    procedure FormCreate(Sender: TObject);
  end;
Andrey
you should try it... :-)
X-Ray
(when you do that, you get a strange error at runtime.)
X-Ray
sorry, i don't have delphi at hand. what is the error?
Andrey
@Andrey: First of all, your program won't compile. If you fix that and run it, you'll get an EClassNotFound exception. You can fix that by adding `initialization RegisterClass(TLabel);` to your unit, but then the label variable won't be assigned to the instance that was created from the DFM.
Ulrich Gerhardt
+1  A: 

Design-time controls cannot be made private. They must be published in order for DFM streaming to work correctly.

Remy Lebeau - TeamB
that's been my experience but i had hoped to hear that's changed in d2010 or that there's a way to do that in d2009 that i haven't heard of.thank you for your answer!
X-Ray
You would have to instantiate and initialize the controls manally in your code. But then you lose the design-time support. Unless you use a third-party plugin/expert that translates DFM contents into compilable source code. GExperts has such a plugin available.
Remy Lebeau - TeamB
thought so. thank you for your informative comments and for all your work with Team B and SO!
X-Ray
Since Delphi 2010 has attributes, it would be nice if in future versions the visibility of components in the form designer could be specified by attributes. For example, a label could be in the private section but have a [DesignerVisible] attribute so the form designer finds it. I assume the form designer uses RTTI to work so it wouldn't be all that hard at first glance to change it. Also when upgrading old forms it would automatically add [DesignerVisible] to existing forms. Anyway, just a thought!
Alan Clark
Yes, the Form Designer uses RTTI to get its information. Although 2010 introduces a new Exhanced RTTI that outputs information for non-published members, the DFM system is still based on the RTTI of published items only. Maybe in the next version, that will change, who knows.
Remy Lebeau - TeamB