Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Rename a tab of a pagecontrol

Dear Experts,

I have a little example that loads and saves the treeview-nodes
along with its associated text that will be displayed in the dbrichedit
to/from a database.

I have some code to rename the tab of a PageControl.

var
 NewName: String;
begin
 InputQuery('New Name','Enter something',NewName);
 PageControl1.ActivePage.Caption := NewName;
end;

But when I close the application, and start it again, the renaming is gone.
I have in the example for every tab is linked to a database column.
and the data in the column is displayed in the dbrichedit on the tab.

Is it possible, is there way to remember the renaming of the tabs that is
associated with a database column.
I know the names between the pagecontrol's tab and the database column can
be diffirent, because their linked.

Who can help me, I have put a little example in the code-section.

Peter Kiers
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, DB, ADODB, StdCtrls, DBCtrls;


const
  Item_Image_Index = 1;
  FolderClose_Image_Index = 2;
  FolderOpen_Image_Index = 3;

type
  TMainForm = class(TForm)
    TreeView1: TTreeView;
    PageControl1: TPageControl;
    Panel1: TPanel;
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    AddTab: TButton;
    ADOTable1ID: TAutoIncField;
    ADOTable1Parent: TIntegerField;
    ADOTable1Status: TWordField;
    ADOTable1Name: TWideStringField;
    ADOTable1Image_Index: TSmallintField;
    ADOTable1Data1: TBlobField;
    ADOTable1Data2: TBlobField;
    ADOTable1Data3: TBlobField;
    ADOTable1Data4: TBlobField;
    ADOTable1Data5: TBlobField;
    ADOTable1Data6: TBlobField;
    ADOTable1Data7: TBlobField;
    procedure AddTabClick(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    DBPath: string; 
    DBName: string; 
    procedure OpenDatabase;  
    function FindNode(ni: integer): TTreeNode;
  public
    { Public declarations }
    Procedure NewTab;
    Procedure LoadTree;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

function CustomSortProc(Node1, Node2: TTreeNode; Data: Integer): Integer; stdcall;
begin
  if Node1.ImageIndex <> Node2.ImageIndex then
  begin
    if Node1.ImageIndex = Item_Image_Index then Result := -1 else Result := 1;
  end else
    Result := AnsiCompareText(Node1.Text, Node2.Text);
end;
(*---------------------------------------------------*)
procedure TMainForm.AddTabClick(Sender: TObject);
begin
  NewTab;
end;
(*---------------------------------------------------*)
function TMainForm.FindNode(ni: integer): TTreeNode;
var
  i: integer;
begin
  with treeview1 do
   for i := 0 to Items.Count - 1 do
    if ni = Integer(Items[i].Data) then
      begin
        Result := Items[i];
        Exit;
      end;
  Result := nil;
end;
(*---------------------------------------------------*)
procedure TMainForm.FormCreate(Sender: TObject);
begin
  DBPath := IncludeTrailingPathDelimiter(ExtractFilePath(Application.Exename));
  DBName := 'dbtree.pkr';
  OpenDatabase; //Procedure die de connectie opzet
end;
(*---------------------------------------------------*)
procedure TMainForm.FormDestroy(Sender: TObject);
begin
  AdoTable1.First;
end;
(*---------------------------------------------------*)
procedure TMainForm.LoadTree;
var
  i: integer;
  n, n2: TTreeNode;
begin
  ADOTable1.First;
  with TreeView1 do
  begin
    Items.BeginUpdate;
    Items.Clear;
    while not ADOTable1.Eof do
    begin
      n2 := FindNode(ADOTable1Parent.Value);
      n := Items.AddChild(n2, ADOTable1Name.Value);
      n.ImageIndex := ADOTable1Image_Index.Value;
      if n.ImageIndex = FolderClose_Image_Index
        then n.SelectedIndex := FolderOpen_Image_Index
      else n.SelectedIndex := n.ImageIndex;
      n.Data := Pointer(ADOTable1ID.Value);
      ADOTable1.Next;
    end;
    for i := 0 to Items.Count - 1 do Items[i].Expand(True);
    CustomSort(@CustomSortProc, 0);
    Items.EndUpdate;
    if items.Count > 0 then
    begin
      selected := items[0];
      if assigned(selected) then
      begin
        selected.Collapse(true);
        selected.Expand(False);
      end;
    end;
  end;
  with TreeView1 do if Items.Count > 0 then Selected := Items[0];
  NewTab;
end;
(*---------------------------------------------------*)
procedure TMainForm.NewTab;
var
  tabSheet: TTabSheet;       
  dbrichEdit: TDBRichEdit;   
begin
  tabSheet := TTabSheet.Create(PageControl1);
  tabSheet.PageControl := PageControl1;

  dbrichEdit := TDBRichEdit.Create(tabSheet);
  dbrichEdit.Parent := tabSheet;
  dbrichEdit.Name := 'DBRichedit' + IntToStr(tabSheet.tabindex+1);
  dbrichEdit.Align := alClient;
  dbrichEdit.Visible := True;
  dbrichEdit.DataSource := DataSource1;
  dbrichEdit.DataField := 'Data' + IntToStr(tabSheet.tabindex+1);
  tabSheet.Name := 'TabSheet' + IntToStr(tabSheet.tabindex+1);
  tabSheet.caption := 'Page' + ' (' + IntToStr(tabSheet.tabindex+1) + ')';
  PageControl1.ActivePage := tabSheet;
end;

(*---------------------------------------------------*)
procedure TMainForm.OpenDatabase;
var
  conn_str: string; 
begin
  conn_str :=
     Format('provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=%s%s;',
    [IncludeTrailingPathDelimiter(DBPath), DBName]);
  ADOConnection1.Connected := False;  
  ADOConnection1.ConnectionString := conn_str;
  ADOConnection1.Connected := True; 
  ADOTable1.Open;   
  LoadTree;  
end;
(*---------------------------------------------------*)
procedure TMainForm.TreeView1Change(Sender: TObject; Node: TTreeNode);
Var
 V:Variant;
begin
 if Assigned(TreeView1.Selected) then
  begin
   V:=Integer(TreeView1.Selected.Data);
   if V>0 then ADOTable1.Locate('ID',V,[]);
  end;
end;

(*---------------------------------------------------*)
end.

Open in new window

Avatar of tigin44
tigin44
Flag of Türkiye image

you should store the new names entered by the user. An ini file or a db table can be used for this... If you application is not an db application use and ini file.
Avatar of Peter Kiers

ASKER

My application is a db-application.
so you can store the names in a table and assign them on the form creation process
How can i do that?

Peter
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Ferruccio68.
500 p's are comming to you.

Greetings,

Peter Kiers
Incompatible types: string and tabsheet at line:
  tabSheet := TTabSheet.Create(PageControl1);
Typo in the code.
Remove this part  
 If Name = '' then Name :=