Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Set the RichEdit1 with the chosen fontname and size.

Posted on 2009-06-29
2
Medium Priority
?
425 Views
Last Modified: 2012-05-07
Dear Experts,

I have a MainForm with a Richtedit on it and a dialog form called Settings
with a Combox (to choose the fontname) and an Edit with an UpDown component
(for choosing the fontsize). The chosen fontname and size will be save and
loaded from an INI-file. Now I have to tell the richedit to load the saved
fontname and size at startup of my application.  How can I do that. Who
can help me...

Greetings,

Peter Kiers



SETTINGS FORM: 
============
 
 private
    { Private declarations }
    fSettingsFile: string; 
    procedure LoadSettings;
    procedure SaveSettings;
  public
    { Public declarations }
  end;
 
var
  SettingsDlg: TSettingsDlg;
  fFontName: string;      
  fFontSize: Integer;    
 
implementation
 
uses Main;
 
{$R *.dfm}
 
procedure TSettingsDlg.FontNameChange(Sender: TObject);
begin
  fFontName := FontName.Items[FontName.ItemIndex];   
end;
(*-----------------------------------------*)
procedure TSettingsDlg.FontSizeChange(Sender: TObject);
begin
  fFontSize := StrToInt(FontSize.Text);       
end;
(*-----------------------------------------*)
procedure TSettingsDlg.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveSettings;
end;
(*-----------------------------------------*)
procedure TSettingsDlg.FormCreate(Sender: TObject);
begin
  fSettingsFile := ChangeFileExt(Application.ExeName, '.ini'); 
  FontName.Items := Screen.Fonts;    
end;
(*-----------------------------------------*)
procedure TSettingsDlg.FormShow(Sender: TObject);
begin   
  LoadSettings;  
  FontName.ItemIndex := FontName.Items.IndexOf(fFontName);
  FontSize.Text := IntToStr(fFontSize);
end;
(*-----------------------------------------*)
procedure TSettingsDlg.LoadSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    fFontName := ini.ReadString('FONT', 'FaceName', fFontName);     
    fFontSize := ini.ReadInteger('FONT', 'PointSize', fFontSize);   
  finally
    FreeAndNil(ini);
  end;
end;
(*-----------------------------------------*)
procedure TSettingsDlg.SaveSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    ini.WriteString('FONT', 'FaceName', fFontName);    
    ini.WriteInteger('FONT', 'PointSize', fFontSize);    
  finally
    FreeAndNil(ini);
  end;
end;
(*-----------------------------------------*)
end.
 
MAINFORM: 
============
procedure TMainForm.Settings1Click(Sender: TObject);
var
  dlgSettingsDialog: TSettingsDlg;
  SaveSelStart, SaveSelLength: Integer;
begin
  dlgSettingsDialog := TSettingsDlg.Create(Self);
  try
    dlgSettingsDialog.ShowModal;
  finally
   if RichEdit1.Text <> '' then
    begin
    SaveSelStart := RichEdit1.SelStart;
    SaveSelLength := RichEdit1.SelLength;
    RichEdit1.SelectAll;
    RichEdit1.SelAttributes.Name := dlgSettingsDialog.FontName.Text;
    RichEdit1.SelAttributes.Size := StrToInt(dlgSettingsDialog.FontSize.Text);
    RichEdit1.SelStart := SaveSelStart;
    RichEdit1.SelLength := SaveSelLength;
  end else
  begin
    RichEdit1.Font.Name := dlgSettingsDialog.FontName.Text;
    RichEdit1.Font.Size := StrToInt(dlgSettingsDialog.FontSize.Text);
  end;
  end;
  FreeAndNil(dlgSettingsDialog);
end;

Open in new window

0
Comment
Question by:peterkiers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 2000 total points
ID: 24735154
i guess you like circular unit referencing ?

try it like this (can be used from any form containing a richedit)
try and prevent global variables, using implementation variables instead !

if the user presses cancel (or close) your code applies the settings anyway ...

it's a little problem to set one way of loading/saving settings.
This time i used the form, most of the time i use a separate object (not a form) for loading/saving the settings

your try finally for dlgSettingsDialog is wrong too ...
SETTINGS FORM: 
============
 
 private
    { Private declarations }
    fSettingsFile: string; 
    fFontName: string;      
    fFontSize: Integer; 
    procedure LoadSettings;
    procedure SaveSettings;
  public
    { Public declarations }
  end;
 
var
  SettingsDlg: TSettingsDlg;
 
procedure RichEditSettings(ARichEdit: TRichEdit; LoadFromIni: Boolean = False);  
 
implementation
 
{$R *.dfm}
 
procedure RichEditSettings(ARichEdit: TRichEdit; LoadFromIni: Boolean = False);  
var
  dlgSettingsDialog: TSettingsDlg;
  SaveSelStart, SaveSelLength: Integer;
  Apply: Boolean;
begin
  if Assigned(ARichEdit) then 
  begin
    dlgSettingsDialog := TSettingsDlg.Create(ARichEdit.Owner);
    try
      Apply := False;
      with dlgSettingsDialog do 
      begin 
        if LoadFromIni then 
        begin
          LoadSettings;
          Apply := True;
        end else if dlgSettingsDialog.ShowModal = mrOk then 
          Apply := True;
        if Apply then 
        begin
          if ARichEdit.Text <> '' then
          begin
            SaveSelStart := ARichEdit.SelStart;
            SaveSelLength := ARichEdit.SelLength;
            ARichEdit.SelectAll;
            ARichEdit.SelAttributes.Name := fFontName;
            ARichEdit.SelAttributes.Size := fFontSize;
            ARichEdit.SelStart := SaveSelStart;
            ARichEdit.SelLength := SaveSelLength;
          end else
          begin
            ARichEdit.Font.Name := fFontName;
            ARichEdit.Font.Size := fFontSize;
          end;
        end;
      end;
    finally
      FreeAndNil(dlgSettingsDialog);
    end;
  end else 
    Raise Exception.Create('Richedit not assigned !');
end;
  
procedure TSettingsDlg.FontNameChange(Sender: TObject);
begin
  fFontName := FontName.Items[FontName.ItemIndex];   
end;
(*-----------------------------------------*)
procedure TSettingsDlg.FontSizeChange(Sender: TObject);
begin
  fFontSize := StrToInt(FontSize.Text);       
end;
(*-----------------------------------------*)
procedure TSettingsDlg.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveSettings;
end;
(*-----------------------------------------*)
procedure TSettingsDlg.FormCreate(Sender: TObject);
begin
  fSettingsFile := ChangeFileExt(Application.ExeName, '.ini'); 
  FontName.Items := Screen.Fonts;    
end;
(*-----------------------------------------*)
procedure TSettingsDlg.FormShow(Sender: TObject);
begin   
  LoadSettings;  
  FontName.ItemIndex := FontName.Items.IndexOf(fFontName);
  FontSize.Text := IntToStr(fFontSize);
end;
(*-----------------------------------------*)
procedure TSettingsDlg.LoadSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    fFontName := ini.ReadString('FONT', 'FaceName', fFontName);     
    fFontSize := ini.ReadInteger('FONT', 'PointSize', fFontSize);   
  finally
    FreeAndNil(ini);
  end;
end;
(*-----------------------------------------*)
procedure TSettingsDlg.SaveSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    ini.WriteString('FONT', 'FaceName', fFontName);    
    ini.WriteInteger('FONT', 'PointSize', fFontSize);    
  finally
    FreeAndNil(ini);
  end;
end;
(*-----------------------------------------*)
end.
 
MAINFORM: 
============
procedure TMainForm.Settings1Click(Sender: TObject);
begin
  RichEditSettings(RichEdit1);
end;
 
procedure TMainForm.FormCreate(Sender: TObject);
begin
  RichEditSettings(RichEdit1, True);
end;

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 24735402
Wauw, i am impressed. 500 p's comming your way...

Greetings, Peter Kiers
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question