[SOLVED] Pagecontrol and tabs lazarus / 61-Beginners ---------------------------------------------------------------------- scons · 2017-01-15 09:38 Hi all, I have a form with Pagecontrol and tabs are created on runtime. I can add tabs and delete tabs. I can find which tab is the active tab. But I run into 2 problems: 1. If I create for example 7 tabs, then select the 4th tab, and I want to do an action on it, the action is always performed on the last created tab2. If I create for example 7 tabs, go to the 6th tab and delete this one, after adding again to I get an error because which says tab already exists If I want to edit the font of SynEdit on tab 4 when I am on it, it changes the font on tab 7. If I want to open a file, it opens it in tab 7, etc. ... Probably I am missing something and at least doing something wrong here. The error I get after deleting and adding a new tab again. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Class EComponentErrorDuplicate Name: A component named "New 3" already exists The code I have so far: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit editor; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, SynEdit, Forms, Controls, Graphics, Dialogs,  ComCtrls, Menus, LazUTF8, Unit2; type   { TForm1 }   TForm1 = class(TForm)   ...     procedure BtnAddPageClick(Sender: TObject);    procedure BtnClosePageClick(Sender: TObject);    procedure BtnFontClick(Sender: TObject);    procedure BtnOpenClick(Sender: TObject);    procedure BtnQuitClick(Sender: TObject);    procedure FontDialog1ApplyClicked(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure ToolBtn18NCharClick(Sender: TObject);    procedure ToolBtn5CloseTabClick(Sender: TObject);  private    { private declarations }  public    { public declarations }    Editor: TSynEdit;    TS: TTabSheet;    Filename: string;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.BtnAddPageClick(Sender: TObject);var  untitled: TTabSheet;begin  untitled := TTabSheet.Create(self);  with untitled do  begin     TS := TTabSheet.Create(PageControl1);    TS.Name := 'New' + IntToStr(PageControl1.ActivePageIndex + 2);    TS.Parent := PageControl1;    TS.Caption := 'untitled ' + IntToStr(PageControl1.ActivePageIndex + 2);     Editor := TSynEdit.Create(TS);    Editor.Parent := TS;    Editor.Name := 'SynEdit' + IntToStr(PageControl1.ActivePageIndex + 2);    Editor.Align := alclient;     Editor.Lines.Clear;    Editor.Lines.Add('@ UTF-8 id string 䰶 do not delete');    Editor.Lines.Add('');    Editor.CaretY := 2;    Editor.Font := Form1.Font;    PageControl1.ActivePage := TS;    Editor.PopupMenu := PopupMenu1;  end;end; procedure TForm1.BtnClosePageClick(Sender: TObject);begin  PageControl1.ActivePage.Free;end; procedure TForm1.BtnFontClick(Sender: TObject);begin  if FontDialog1.Execute then  begin    PageControl1.ActivePageIndex := TS.PageControl.ActivePageIndex;    //ShowMessage(IntToStr(TS.PageControl.ActivePageIndex));     Editor.Font := FontDialog1.Font;  end;end; procedure TForm1.BtnOpenClick(Sender: TObject);begin  OpenDialog1.Filter := 'Load FFEIN file|*.met';  if not OpenDialog1.Execute then    exit;  Editor.Lines.LoadFromFile(UTF8ToSys(OpenDialog1.FileName));  self.Caption := OpenDialog1.FileName;  Filename := OpenDialog1.FileName;end; procedure TForm1.BtnQuitClick(Sender: TObject);begin  Close;end; procedure TForm1.FormCreate(Sender: TObject);begin  //Parent:=PageControl1;   TS := TTabSheet.Create(PageControl1);  TS.Name := 'New' + IntToStr(PageControl1.ActivePageIndex + 1);  TS.Parent := PageControl1;  TS.Caption := 'untitled ' + IntToStr(PageControl1.ActivePageIndex + 1);   Editor := TSynEdit.Create(TS);  Editor.Parent := TS;  Editor.Name := 'SynEdit' + IntToStr(PageControl1.ActivePageIndex + 1);  Editor.Align := alclient;   PageControl1.ParentFont := False;  PageControl1.Font.Style := [fsBold];  TS.ParentFont := False;  //TS.Font       := Form1.Font;   Editor.Lines.Clear;  Editor.Lines.Add('@ UTF-8 id string 䰶 do not delete');  Editor.Lines.Add('');  Editor.CaretY := 2;  Editor.Font := Form1.Font;  PageControl1.ActivePage := TS;  Editor.PopupMenu := PopupMenu1;end; procedure TForm1.ToolBtn18NCharClick(Sender: TObject);begin  Editor.Options := Editor.Options + [eoShowSpecialChars];end; procedure TForm1.ToolBtn5CloseTabClick(Sender: TObject);begin  PageControl1.ActivePage.Free;end; end. ---------------------------------------------------------------------- derek.john.evans · 2017-01-15 12:02 1) You are caching Editor: TSynEdit; You should set the name of Editor to 'Editor' and use this code to find the active editor. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TSynEdit(PageControl1.ActivePage.FindComponent('Editor')))  Normally I create a method call "ActiveEditor: TSynEdit" to make this easier. 2) You are setting the name of a TTabSheet based on the ActivePageIndex. This isn't going to be unique, hence your error. There is no need to set the name of the TTabSheet. You only need to set the name of the TSynEdit control of the TTabSheet. Since each TSynEdit is owned by a different TTabSheet (Owner), this name can be the same. 3) Why are you creating 2 TTabSheets in BtnAddPageClick()? Note: You should be writing this is a more modular way. ie: Write seperate functions to add, remove and access the current active editor. It will make your life a lot easier. ---------------------------------------------------------------------- Bart · 2017-01-15 12:18 You can take a look a LazEdit (see LazEdit wikipage for download links), which implements this. The source is LGPL2, so you can use what you need or expand as you see fit. Bart ---------------------------------------------------------------------- scons · 2017-01-16 21:32 Hi guys, thanks for your answers !!! Quote from: Geepster on January 15, 2017, 01:02:52 pm Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TSynEdit(PageControl1.ActivePage.FindComponent('Editor')))  Yes, this works like magic ! QuoteThere is no need to set the name of the TTabSheet. You only need to set the name of the TSynEdit control of the TTabSheet. Since each TSynEdit is owned by a different TTabSheet (Owner), this name can be the same. Oke, thanks for explaning this. Leaving this "option" out resolve things. QuoteWhy are you creating 2 TTabSheets in BtnAddPageClick()? Hmm, dunno exactly anymore, I started with some code I have found around this forum, changed it a few times until I got stuck. I guess this was some unwanted and undeleted garbage. QuoteNote: You should be writing this is a more modular way. ie: Write seperate functions to add, remove and access the current active editor. It will make your life a lot easier. You are probably more then right, but since I have no pre-programming background, I am struggeling my way to try to understand how this all works. With falling and getting up again, for now there a mostly a lot of falls and getting up again. I think if you guys on the forum would look a the code of the few things I wrote so far, it would be hilarious to you. But at least some things come clearer every time I try to write something. To be short, I will give your solution to this a go in a few weeks/months. I first want to look into how I can split up my main Unit in different ones to avoid 1000 lines of code in 1 file, that alone will be a struggle. Quote from: Bart on January 15, 2017, 01:18:28 pmYou can take a look a LazEdit (see LazEdit wikipage for download links), which implements this. The source is LGPL2, so you can use what you need or expand as you see fit. Thanks for the tip, I didn't even realize there was LazEdit. I might have a look at it.