Should I use delphi tframes for multi-pages forms?

前端 未结 4 1367
轮回少年
轮回少年 2021-02-04 15:04

I have some forms in my application which have different \"states\" depending on what the user is doing; for exemple when listing through his files the form displays some data a

4条回答
  •  名媛妹妹
    2021-02-04 15:27

    I also use the approach described by @Tim Sullivan with some addition. In every frame I define the procedure for the frame initialization - setting default properties of its components.

    TAnyFrame = class(TFrame)
      public
        function initFrame() : boolean; // returns FALSE if somesthing goes wrong
        ...
    end;
    

    And after the frame was created I call this procedure.

    aFrame := TAnyFrame.Create(nil);
    if not aFrame.initFrame() then
      FreeAndNil(aFrame)
    else
      ... // Associate frame with form and do somthing usefull
    

    Also when you change visible frame it is not necessary to destroy previous one because it can contains useful data. Imagine that you input some data in a first frame/page, then go to the next, and then decide to change data on the first page again. If you destroy previous frame you lost the data it contains and need to restore them. The solution is to keep all created frames and creates new frame only when it is necessary.

    page_A : TFrameA;
    page_B : TFrameB;
    page_C : TFrameC;
    current_page : TFrame;
    
    // User click button and select a frame/page A
    if not assigned(page_A) then begin
      // create and initialize frame
      page_A := TFrameA.Create(nil);
      if not page_A.initFrame() then begin
        FreeAndNil(page_A);
        // show error message
        ...
        exit;
      end;
      // associate frame with form
      ...
    end;
    // hide previous frame
    if assigned(current_page) then
      current_page.hide();
    // show new page on the form
    current_page := page_A;
    current_page.Show();
    

提交回复
热议问题