• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1100
  • Last Modified:

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

0
peterkiers
Asked:
peterkiers
  • 4
  • 2
  • 2
1 Solution
 
tigin44Commented:
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.
0
 
peterkiersAuthor Commented:
My application is a db-application.
0
 
tigin44Commented:
so you can store the names in a table and assign them on the form creation process
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
peterkiersAuthor Commented:
How can i do that?

Peter
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
As I said in a recent your question, that's the difficult of your application.
But there's a solution.

As your TDBRichEdit are linked to TBlobField fields, you should loop after these in your table fields to get the name of the field when it's a TBlobfield and assign it to the created TTabsheet.

Your Newtab procedure could became like the attached.

How to get the name value when reopen?

For i := 0 to Adotable1.fieldcount-1 do
   if (AdoTable1.fields[i] Is TBlobField) then
      NewTab(AdoTable1.Fields[i].FieldName);




procedure TMainForm.NewTab(Name:String);
var
  tabSheet: TTabSheet;       
  dbrichEdit: TDBRichEdit;   
begin
  If Name = '' then Name := 
  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;
  If Name <> '' then
    begin
      dbrichEdit.DataField := Name;
      tabSheet.Name := Name;
    end else begin      
      dbrichEdit.DataField := 'Data' + IntToStr(tabSheet.tabindex+1);
      tabSheet.Name := 'TabSheet' + IntToStr(tabSheet.tabindex+1);
    end;
  tabSheet.caption := 'Page' + ' (' + IntToStr(tabSheet.tabindex+1) + ')';
  PageControl1.ActivePage := tabSheet;
end;

Open in new window

0
 
peterkiersAuthor Commented:
Thanks Ferruccio68.
500 p's are comming to you.

Greetings,

Peter Kiers
0
 
peterkiersAuthor Commented:
Incompatible types: string and tabsheet at line:
  tabSheet := TTabSheet.Create(PageControl1);
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Typo in the code.
Remove this part  
 If Name = '' then Name :=
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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