troubleshooting Question

Font doesn't change after creating a dynamic tab

Avatar of Peter Kiers
Peter KiersFlag for Netherlands asked on
Delphi
2 Comments1 Solution231 ViewsLast Modified:
Dear Experts,

In my example I have a procedure called NewTab that creates
dynamicly Pagecontrol Pages with a richedit (align = alClient) on it.
This procedure will be executed at creation (so the application starts
with at least 1 page) and with the menu-item called New Page
(to give the user the ability to create more pages).

I have made another form called SettingsDlg with a combox and
a spinedit on it for choosing a fontname and size that will change the
text of all the richedits that are created on the main form after pressing the
OK-button.

All that works fine, accept for one thing:

f.e: when there are 3 pages created with their richedit's, and in all 3 of them
is some text. Then I open de SettingsDlg to choose another fontname and size.
for example Comic Sans MS+12. After pressing the OK-button the text of all 3 of
them will be changed, but then I create a new page and when I put some text in
it, its not the textname and size that I have choosen. In other words: the procedure
NewTab have to call for the procedure "SettingsDlg.ApplyDefaultSettings" also. But
When I do that I get an Acces-violation.

Who knows the answer and is willing to help me?
I have put the code in the code-section.

Peter

unit Main;

interface

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

type
  TMainForm = class(TForm)
    PageControl1: TPageControl;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Exit1: TMenuItem;
    Settings1: TMenuItem;
    Edit1: TMenuItem;
    Insert1: TMenuItem;
    NewPage1: TMenuItem;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure NewPage1Click(Sender: TObject);
    procedure Settings1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure NewTab;
  end;

var
  MainForm: TMainForm;

implementation

uses Settings;

{$R *.dfm}

procedure TMainForm.FormCreate(Sender: TObject);
begin
  NewTab;
end;
(*---------------------------------------------------*)
procedure TMainForm.FormShow(Sender: TObject);
begin
  SettingsDlg.ApplyDefaultSettings;
end;
(*---------------------------------------------------*)
procedure TMainForm.NewPage1Click(Sender: TObject);
begin
  NewTab;
end;
(*---------------------------------------------------*)
procedure TMainForm.NewTab;
var
  aRE: TRichEdit;
  tabSheet: TTabSheet;
begin
  tabSheet := TTabSheet.Create(PageControl1);
  tabSheet.PageControl := PageControl1;
  tabSheet.Name := 'TabSheet' + IntToStr(tabSheet.tabindex+1);
  tabSheet.caption := 'Page' + '(' + IntToStr(tabSheet.tabindex+1) + ')';
  aRE := TRichEdit.Create(tabSheet);
  aRE.Parent := tabSheet;
  aRe.Name := 'RichEdit' + IntToStr(tabSheet.tabindex+1);    
  aRE.Align := alClient;
  aRE.ParentFont := False;
  aRE.HideSelection := False;
  aRE.ScrollBars := ssVertical;
  aRE.Visible := True;
  PageControl1.ActivePage := tabSheet;
end;
(*---------------------------------------------------*)
procedure TMainForm.Settings1Click(Sender: TObject);
begin
  SettingsDlg.ShowModal;
end;
(*---------------------------------------------------*)
end.

unit Settings;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
  Buttons, ComCtrls, ExtCtrls, Spin, IniFiles, DBCtrls;


type
  TSettingsDlg = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    OKBtn: TButton;
    CancelBtn: TButton;
    HelpBtn: TButton;
    GroupBox1: TGroupBox;
    lblFontName: TLabel;
    lblFontSize: TLabel;
    cbFontName: TComboBox;
    edtFontSize: TSpinEdit;
    procedure OKBtnClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    SettingsFile: string; 
    procedure GetFontNames;
    procedure LoadSettings;
    procedure SaveSettings;
  public
    { Public declarations }
    procedure ApplyDefaultSettings;
  end;

var
  SettingsDlg: TSettingsDlg;

implementation

uses Main;

{$R *.dfm}

function EnumFontsProc(var LogFont: TLogFont; var TextMetric: TTextMetric;
  FontType: Integer; Data: Pointer): Integer; stdcall;
begin
  TStrings(Data).Add(LogFont.lfFaceName);
  Result := 1;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.ApplyDefaultSettings;
var
 z, i: Integer;
begin
 with MainForm.PageControl1 do
  for z := 0 to PageCount -1 do
   for i := 0 to Pages[z].ControlCount - 1 do
    if Pages[z].Controls[i] is TRichEdit Then
     with (Pages[z].Controls[i] As TRichEdit).DefAttributes do
      begin
       Name := cbFontName.Items[cbFontName.ItemIndex];
       Size := StrToInt(edtFontSize.Text);
      end;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.FormCreate(Sender: TObject);
begin
 GetFontNames;
 SettingsFile := ChangeFileExt(Application.ExeName, '.ini');
 LoadSettings;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.FormDestroy(Sender: TObject);
begin
 SaveSettings;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.GetFontNames;
var
  DC: HDC;
begin
  DC := GetDC(0);
  EnumFonts(DC, nil, @EnumFontsProc, Pointer(cbFontName.Items));
  ReleaseDC(0, DC);
  cbFontName.Sorted := True;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.LoadSettings;
var ini: TIniFile; 
begin 
  ini := TIniFile.Create(SettingsFile); 
  try 
   cbFontName.ItemIndex  := cbFontName.Items.IndexOf(ini.ReadString('Font', 'FaceName', Font.Name)); 
   edtFontSize.Value := ini.ReadInteger('Font', 'PointSize', Font.Size); 
  finally 
    FreeAndNil(ini) 
  end; 
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.OKBtnClick(Sender: TObject);
begin
 ApplyDefaultSettings;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.SaveSettings;
var ini: TIniFile; 
begin 
  ini := TIniFile.Create(SettingsFile); 
  try 
    ini.WriteString('Font', 'FaceName', cbFontName.Items[cbFontName.ItemIndex]); 
    ini.WriteInteger('Font', 'PointSize', edtFontSize.Value); 
  finally 
    FreeAndNil(ini) 
  end; 
end;
(*---------------------------------------------------*)
end.

Open in new window

ASKER CERTIFIED SOLUTION
Emmanuel PASQUIER
Freelance Project Manager

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2010

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros