Some way to rearrange components positions, sizes, ownership (properties in general) at run-time following a design-time rule
Asked Answered
Y

4

1

We have an application that have many components on the forms (panels, tabs, edits, comboboxes, etc...). But depending on the user profile, most of them could be filled automatically and/or not be visible. So, users could do their work faster.

The question: Is there any easier way to create, position, change ownership etc, at runtime? I would like to create 2 .dfm files for a given unit and then have something to tell the application what .dfm to use. Like: "Hey! User is advanced, use the Unit1Advanced.dfm!" A working example would be nice. I would like to use that in Delphi 7 too, but it has to work at least in Delphi XE.

What I know that exist till now:

  1. ComponentsToCode function from GExperts can create code from a given component as gabr pointed in this answer.
  2. I could create 2 forms and create the desired one at runtime. But that means one additional .pas file to each additional .dfm file. This would be harder to maintain.
  3. This answer seems to give a hint. But I am not used to TReader and TWriter classes...
Yatzeck answered 10/12, 2011 at 13:15 Comment(5)
Working with TFrames might be a good option, see here for a similar solution : should-i-use-delphi-tframes-for-multi-pages-forms.Phillisphilly
@LURD hmm... I'm not sure. When you create a TEdit1 in a Tpanel from Frame1 then I will have to write the same code to TEdit1 in Tpanel1 from Frame2. Right? In other words, components from TFrame1 cannot be referenced in the code as components from TFrame2.Yatzeck
If not a multi-page solution is good, make a frame of the simple form. Put this in in one form. Create another form, add the frame and all other controls to create the advanced menu. Yes, you will have two sources, but still based on the same frame. This will also make your code easier to read, since there will be no if advanced then.Phillisphilly
@LURD I think that is another (good) answer as alternative for VFI which is not given yet.Tour
Can someone explain why this question was downvoted? Just to me do better questions next time?Yatzeck
T
2

Warning: This answer is for completeness sake to the question and is only for experimental purposes. It should never be used in real world scenarios.

You want two separate form definition files for only one source code file.

The key is to make use of the CreateNew constructor. To quote the documentation on it:

Use CreateNew instead of Create to create a form without using the associated .DFM file to initialize it.

  1. First, write your advanced form:

    unit Advanced;
    
    interface
    
    uses
      Classes, Controls, Forms, StdCtrls;
    
    type
      TAdvancedForm = class(TForm)
        StandardGroupBox: TGroupBox;
          StandardButton: TButton;
        AdvancedGroupBox: TGroupBox;
          AdvancedButton: TButton;
        procedure StandardButtonClick(Sender: TObject);
        procedure AdvancedButtonClick(Sender: TObject);
      end;
    
    implementation
    
    {$R *.dfm}
    
    procedure TAdvancedForm.StandardButtonClick(Sender: TObject);
    begin
      Caption := Caption + ' Button1Click';
    end;
    
    procedure TAdvancedForm.AdvancedButtonClick(Sender: TObject);
    begin
      Caption := Caption + ' Button2Click';
    end;
    
    end.
    
  2. Build your app, and copy Advanced.dfm to Standard.dfm.

  3. Open Standard.dfm in a text editor and remove the advanced components (in this case the advanced group box containing a button), and rename the form and form type to (T)StandardForm:

    object StandardForm: TStandardForm
      ...
      object StandardGroupBox: TGroupBox
        ...
        object StandardButton: TButton
          ...
        end
      end
    end
    
  4. Add the resource for the standard form to Advanced.pas:

    {$R *.dfm}
    {$R Standard.dfm}
    
  5. And now with the following code, you can open both form definitions for the same source file:

    uses
      Advanced;
    
    procedure TForm1.OpenAdvancedFormClick(Sender: TObject);
    var
      Form: TAdvancedForm;
    begin
      Form := TAdvancedForm.Create(Application);
      Form.Show;
    end;
    
    procedure TForm1.OpenStandardFormClick(Sender: TObject);
    var
    {
      Form: TAdvancedForm; // This is tricky! The form we are about to create has
                           // no AdvancedGroupBox nor AdvancedButton, so make sure
                           // you are not calling it with code completion.
      Form: TStandardForm; // Compiler has no knowledge of TStandardForm!
    }
      Form: TForm;         // So declare your form as TForm!
    begin
      // But create it as TAdvancedForm, otherwise components will not be found!
      Form := TAdvancedForm.CreateNew(Application);
      ReadComponentRes('TStandardForm', Form);
      Form.Show;
    end;
    
Tour answered 13/12, 2011 at 0:23 Comment(2)
Hey! Learning one thing new every day. Thanks. That's what I was trying to achieve. Anyway, I can clearly see the many drawbacks of this. Just to explain your step 3, I don't want to remove (as delete) the advanced components but change drastically the layout (as hiding).Yatzeck
I must admit, its very crude, but if it was "automagically" made by an GUI inside the IDE, someone would find a good way to use it. :DYatzeck
P
4

I can give you a solution based on your point 2: Start with the form for the unexperienced user, place all controls as needed and implement the necessary code in the pas file. Then make a new form inherited from the first one and adjust it to the needs of the experienced user. If necessary you can also add some implementation.

A more flexible approach may be to inherit both forms from a common anchestor. The actual implementation of this scheme depends heavily on your situation.

Picture answered 10/12, 2011 at 13:37 Comment(1)
Probably it's the best way to solve this without getting too complex. Or writing another application to accomplish my needs. I am not sure what I had in mind could be done in Delphi. That was why I asked. Thanks.Yatzeck
T
3

Your desire to have only one .pas file for multiple .dfm files is nearly impossible but also a little incomprehensible: the code would be limited to the less advanced form possible, but really not to recommend.

Regarding your separation requirements:

  • Creation: Make two designs, and do this by Visual Form Inheritance (VFI) like Uwe mentions, so you do not have the separated maintenance problem.
  • Position and sizes: This is a very bad idea. Let controls stay at the same place for each user. Users might get advanced privileges, and be shocked by an unfamiliar arrangement. The only exception I would make for is the minimum size of the form: The advanced version may be taller and/or wider.
  • Change ownership: Yeah, I understand what you mean, but controls are not owned by the user, nor should they. Implement some kind of controller which deals with what type of form should be created.

In case VFI is not an option, then I suggest you design only the advanced form, and control the hiding of advanced controls by setting the Visible property of the linked actions. Do this in or via the setter for the Advanced: Boolean property, which should exist. Or group all advanced controls on one or more containers: group boxes, panels or frames like LU RD comments. Then simply set the visibility of that container. Present scroll bars will adjust or disappear accordingly automatically. Note that you still can address these controls, even if you not want to.

Tour answered 10/12, 2011 at 17:47 Comment(3)
Thanks for reply (and edit). The code will not be limited in any way. I can code depending on what the user is looking at. And I understand you think is bad idea change sizes and positions. But its not in my case. Let's just say that users do not get advanced privileges, they have different functions. And I it's not clear to me what you mean by user ownership... I've talking about components ownership/"parentship". Example: button1.Parent := panel3. Your last suggestion is just what I am trying to avoid. I am giving you +1 cause it made me think deeply on my needs. Thanks.Yatzeck
@Yatzeck What I ment about limited code: Suppose you cán have 2 dfm's and only 1 pas, then how do you compile a unit which references both dfm's? Maybe by a compiler switch you'll get the basics, but that leaves no runtime option for choosing which form you want. In other words: if you code for the less advanced form, then you can't address the components on the advanced form; but if you code for the advanced form, then you can't attach that to the less advanced form.Tour
@Yatzeck Edit about that last remark: it cán, see my other answer, but I hope you realize this is not the way to go.Tour
T
2

Warning: This answer is for completeness sake to the question and is only for experimental purposes. It should never be used in real world scenarios.

You want two separate form definition files for only one source code file.

The key is to make use of the CreateNew constructor. To quote the documentation on it:

Use CreateNew instead of Create to create a form without using the associated .DFM file to initialize it.

  1. First, write your advanced form:

    unit Advanced;
    
    interface
    
    uses
      Classes, Controls, Forms, StdCtrls;
    
    type
      TAdvancedForm = class(TForm)
        StandardGroupBox: TGroupBox;
          StandardButton: TButton;
        AdvancedGroupBox: TGroupBox;
          AdvancedButton: TButton;
        procedure StandardButtonClick(Sender: TObject);
        procedure AdvancedButtonClick(Sender: TObject);
      end;
    
    implementation
    
    {$R *.dfm}
    
    procedure TAdvancedForm.StandardButtonClick(Sender: TObject);
    begin
      Caption := Caption + ' Button1Click';
    end;
    
    procedure TAdvancedForm.AdvancedButtonClick(Sender: TObject);
    begin
      Caption := Caption + ' Button2Click';
    end;
    
    end.
    
  2. Build your app, and copy Advanced.dfm to Standard.dfm.

  3. Open Standard.dfm in a text editor and remove the advanced components (in this case the advanced group box containing a button), and rename the form and form type to (T)StandardForm:

    object StandardForm: TStandardForm
      ...
      object StandardGroupBox: TGroupBox
        ...
        object StandardButton: TButton
          ...
        end
      end
    end
    
  4. Add the resource for the standard form to Advanced.pas:

    {$R *.dfm}
    {$R Standard.dfm}
    
  5. And now with the following code, you can open both form definitions for the same source file:

    uses
      Advanced;
    
    procedure TForm1.OpenAdvancedFormClick(Sender: TObject);
    var
      Form: TAdvancedForm;
    begin
      Form := TAdvancedForm.Create(Application);
      Form.Show;
    end;
    
    procedure TForm1.OpenStandardFormClick(Sender: TObject);
    var
    {
      Form: TAdvancedForm; // This is tricky! The form we are about to create has
                           // no AdvancedGroupBox nor AdvancedButton, so make sure
                           // you are not calling it with code completion.
      Form: TStandardForm; // Compiler has no knowledge of TStandardForm!
    }
      Form: TForm;         // So declare your form as TForm!
    begin
      // But create it as TAdvancedForm, otherwise components will not be found!
      Form := TAdvancedForm.CreateNew(Application);
      ReadComponentRes('TStandardForm', Form);
      Form.Show;
    end;
    
Tour answered 13/12, 2011 at 0:23 Comment(2)
Hey! Learning one thing new every day. Thanks. That's what I was trying to achieve. Anyway, I can clearly see the many drawbacks of this. Just to explain your step 3, I don't want to remove (as delete) the advanced components but change drastically the layout (as hiding).Yatzeck
I must admit, its very crude, but if it was "automagically" made by an GUI inside the IDE, someone would find a good way to use it. :DYatzeck
P
0

The DevExpress ExpressLayout components might be helpful - see http://devexpress.com/Products/VCL/ExLayoutControl/

They provide Runtime Form Customization - Runtime Control Customization - Screen Resolution Independence, and much more

Platysma answered 10/12, 2011 at 13:28 Comment(1)
Hi. Thanks for your reply. I didn't know about that set. But I am not sure it will help. Quoting them "This is accomplished by allowing users to layout and manage their own UI - as their needs dictate - in a manner that suits their business requirements best - without seeking assistance from programmers or programmer oriented tools". As far I can see, the user change the layout. I don't want the user to do that. I want to specify in design time how should the appearance be for this or that. Not sure I was clear, thought.Yatzeck

© 2022 - 2024 — McMap. All rights reserved.