[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

Multiple pages on a form

I would like from my main form open another form that is contained in the main form area and continuing having the focus on the main form, something like a MDI form. I tried to use a MDI form but I found I can't make its border invisible (BorderStyle:=BsNone). For me it's very important open the new form (like a page) without any other border and without loosing the focus. I think using a standard SDI form it is not possible keep the focus on the main form (but it's possible to hide its border). Finally, I would like to show on the main form different things (using a toolbar to choose it); I think I can get something like I want with a TabControl (or maybe with a PageControl) but I don't know how can I create different tabs with different controls (if I move a control on the TabControl, it is displayed in every tabs). Is there a component where I can put all my controls and then use it to display on the form ??? I tried with DataModule, but it hold only non-visual components.
0
gianluca
Asked:
gianluca
  • 4
  • 3
  • 2
  • +3
1 Solution
 
LischkeCommented:
Hi gianluca,

you already had the right idea but somehow used in wrong :-) The key for this kind of application (apart from the focus issue) is to use a page control. Below is code I'm using in a project window which can contain a variable amount of edits (just like the editor in the Delphi IDE). For your case you mainly need to change what to create (a TForm I think) and its setup.

function TProjectForm.AddEdit(const FileName: String; Focus: Boolean = False; ReadOnly: Boolean = False): TSyntaxEdit;

// adds a new page control sheet with an edit window and loads the given file
// into the edit (if file name is not empty)

var NewSheet: TTabSheet;
    NewIndex: Integer;

begin
  Result := EditFromFilename(FileName);
  if Result = nil then
  begin
    // create new sheet where the edit will be placed on
    if PageControl.PageCount > 0 then NewIndex := PageControl.ActivePage.PageIndex + 1
                                 else NewIndex := 0;
    NewSheet := TTabSheet.Create(PageControl);
    NewSheet.PageControl := PageControl;
    NewSheet.Caption := PureName(FileName);
    NewSheet.PageIndex := NewIndex;
    NewSheet.ImageIndex := ApplicationData.SystemImages.GetImageIndex(Filename, False, False, []);

    // create new editor window
    Result := TSyntaxEdit.Create(NewSheet);
    with Result do
    begin
      Parent := NewSheet;
      Keystrokes := GlobalSettings.EditorCommands;
      Cursor := crIBeam;
      Align := alClient;
      BevelEdges := [beLeft, beTop];
      BevelInner := bvNone;
      BevelKind := bkFlat;
      BorderStyle := bsNone;
      BookMarkOptions.XOffset := 24;
      CharWidth := 8;
      Ctl3D := True;
      Font.Name := GlobalSettings.EditorFontName;
      Font.Size := GlobalSettings.EditorFontSize;
      GutterWidth := 30;
      Options := GlobalSettings.EditOptions + [eoWantTabs, eoReadOnlySelection];
      if ReadOnly then Options := Options + [eoReadOnly];
      TabSize := GlobalSettings.TabSize;
      IndentSize := GlobalSettings.BlockIndent;
      MaxUndo := GlobalSettings.UndoLimit;
      MarkColor.Foreground := GlobalSettings.EditorMarkForeground;
      MarkColor.Background := GlobalSettings.EditorMarkBackground;
      MaxRightChar := 256;
      PopupMenu := EditorPopup;
      ScrollbarMode := GlobalSettings.ScrollBarMode;
      Scrollbars := ssBoth;
      if FileExists(FileName) then
      begin
        Lines.LoadFromFile(FileName);
        NewSheet.Tag := FileAge(FileName);
      end
      else NewSheet.Tag := 0;
      Modified := False;
      Info := FileName;
      Highlighter := HighlighterFromFileExt(ExtractFileExt(FileName));
      OnCaretChange := EditCaretChange;
      OnChange := EditChange;
    end;
  end;

  ReindexSheets;
  if Focus then
  begin
    PageControl.ActivePage := PageControl.Pages[Result.Tag];
    PageControlChange(PageControl);
  end;
end;


Ciao, Mike
0
 
RBertoraCommented:
TPageControl in the Win32 tab of the component pallette is very good especially for what you want.

have a look at it
Rob ;-)
0
 
RBertoraCommented:
Oh yes TPageControl : remember to right click on the component to add/remove new pages.

Rob;-)
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
karooCommented:
qianlunca,

did the same in a project i'm working on now, try this:

override the constructor and Loaded and CreateParams methods of a form ie:

protected
  procedure CreateParams(var Params: TCreateParams); override;
  procedure Loaded; override;
public
  constructor Create(AOwner: TComponent; AParent: TWinControl; override;

now the constructor:
constructor TForm1.Create(AOwner: TComponent; AParent: TWinControl;
begin
  FTempParent := AParent;
  inherited Create(AOWner);
end;

the Loaded method:
procedure TForm1.Loaded;
begin
  inherited;
  Parent := FTempParent;
  Anchors := [];
  Align := alClient;
  BorderIcons := [];
  BorderStyle := bsNone;    
end;

the CreateParams method:
procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  {inform Windows to create the Form as a Child window}
  if FAsChild then Params.Style := Params.Style or WS_CHILD;
end;

add this form to the repository and inherit your new form from it.
Drop a TPanel on your main form and when you dynamically create the new form pass the TPanel as the AParent parameter. The new form will be created on the TPanel.

works like a charm.

regards Ben:)
0
 
karooCommented:
oh forgot to mention

add the private variable to form1:
private
  FTempParent: TWinControl;
0
 
karooCommented:
doh!

change:
>>if FAsChild then Params.Style := Params.Style or WS_CHILD;
to:
Params.Style := Params.Style or WS_CHILD;
0
 
RBertoraCommented:
Yuck to MDI!
Rob;-)
0
 
RadlerCommented:
spying...
Õ¿Õ
/o/

0
 
kretzschmarCommented:
its all said
0
 
gianlucaAuthor Commented:
Lischke answer is correct but I prefere the karoo answer because let you write your code in different units allowing a more stable and clear design. Regarding the Create method, I suppose you pass the TPanel object only to create the new form with the same dimensions, but can I use the MainForm object to do the same ?? In this way I only need to pass the owner.
0
 
karooCommented:
gianluca,

>>but can I use the MainForm object to do the same
yep you can pass any TWinControl as the parent,
i just like using a panel because the resizing and alligning can be encapsulated nicely... many ways to skin a cat.

Rob, i agree:)

Regards
Ben.
0
 
LischkeCommented:
gianluca, I can't quite follow your argumentation, but this is unimportant as long as you get the answer you need...

Next time perhaps,

Ciao, Mike
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now