Question about passing fontname and size to richedit.

Hi,

On my main form (named MainForm) I have a RichtEdit (called DBRichEdit1).
with a MainMenu and just one item called Settings. When I choose this item
another forms shows up (called SettingsDlg). On the SettingsDlg the user can
Choose a fontname (combobox cbFontName) and size (spinedit called edtFontSize).
and press the OK-button, THEN the fontname and size has to be changed on the
richedit. But with my code the fontname and size changes on the dbrichedit when
I choose the fontname and size on the SettingsDLG, and I haven't even press the
OK-button yet. Who can help me to fix this problem.

unit Settings;

interface

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

type
  TSettingsDlg = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    OKBtn: TButton;
    CancelBtn: TButton;
    HelpBtn: TButton;
    GroupBox1: TGroupBox;
    lblFontName: TLabel;
    lblFontSize: TLabel;
    edtFontSize: TSpinEdit;
    cbFontName: TComboBox;
    procedure edtFontSizeChange(Sender: TObject);
    procedure cbFontNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure GetFontNames;
    function CurrText: TTextAttributes;
  public
    { Public declarations }
  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.cbFontNameChange(Sender: TObject);
begin
  CurrText.Name := cbFontName.Items[cbFontName.ItemIndex];
end;

function TSettingsDlg.CurrText: TTextAttributes;
begin
  if MainForm.DBRichEdit1.SelLength > 0 then Result := MainForm.DBRichEdit1.SelAttributes
  else Result := MainForm.DBRichEdit1.DefAttributes;
end;

procedure TSettingsDlg.edtFontSizeChange(Sender: TObject);
begin
  CurrText.Size := StrToInt(edtFontSize.Text);
end;

procedure TSettingsDlg.FormCreate(Sender: TObject);
begin
 GetFontNames;
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;

end.

Open in new window

LVL 1
Peter KiersOperatorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Emmanuel PASQUIERFreelance Project ManagerCommented:
delete cbFontNameChange and edtFontSizeChange events and do the font update in the OKBtn click event
procedure TSettingsDlg.OKBtnClick(Sender:TObject);
begin
 With CurrText do
 begin
  Name := cbFontName.Items[cbFontName.ItemIndex];
  Size := StrToInt(edtFontSize.Text);
 end;
end;

Open in new window

0
Peter KiersOperatorAuthor Commented:
Hi, espasquier,

your code works fine only now the richedit doesn't update anymore.

f.e: when i start my programm and choose Comic Sans MS + 10.
the richedit will then be adjusted to that. But then I restart my programm
and the font in the combox and the size in the spinedit are Comic Sans MS +10
(because I use a inifile to save and load the settings). But the text in het
richedit isn't Comic Sans MS+10 anymore. What do I have to do to correct this?

Peter
0
Peter KiersOperatorAuthor Commented:
anyone home?
0
Emmanuel PASQUIERFreelance Project ManagerCommented:
you miss something to set the start value in your richedit.DefAttributes to what you have read in your ini

declare this as public in your SettingsDlg :
procedure ApplyDefaultSettings;

and call it after you load your DBRichEdit


procedure TSettingsDlg.ApplyDefaultSettings;
begin
 With MainForm.DBRichEdit1.DefAttributes do
 begin
  Name := cbFontName.Items[cbFontName.ItemIndex];
  Size := StrToInt(edtFontSize.Text);
 end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KiersOperatorAuthor Commented:
Works. 500 points are comming to you...

Greetings, Peter Kiers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.