Solved

Can't make properties of font color of a spped button  to be set to color of last runing of aplication

Posted on 2007-11-27
11
300 Views
Last Modified: 2008-02-26
  When i reopen my application the color of a font of a sppedButton  is equal to color set in properties window.

 It's not set to:
 TSpeedButton(Sp).Font.Color:=clRed;

like i programed it in  previous running of program.

What must i do to conserve the setting of previews running?
0
Comment
Question by:pr2501
  • 5
  • 5
11 Comments
 
LVL 18

Expert Comment

by:Johnjces
ID: 20360958
Certainly not sure why it would change if you set the font color at design time using the Object Inspector > Font > Color > clRed.

Somewhere it is being changed in your code?

One thing you can do is in your Forms OnCreate event, i.e., TForm1.FormCreate add:

TSpeedButton.Font.Color:=clRed;

Would look like:

procedure TForm1.FormCreate(Sender: TObject);
begin
 TSpeedButton.Font.Color:=clRed;
end;
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 20362764
this is how you can save and load the button's font color from an ini file (eg. porject1.ini in the same folder as your exe - this is creted automatically)
uses inifiles; // needed
 

procedure TForm1.SpeedButton1Click(Sender: TObject);

begin

  SpeedButton1.Font.Color := clRed;

end;
 

procedure TForm1.FormCreate(Sender: TObject);

begin

// load settings

  with TINIFile.Create(ChangeFileExt(Paramstr(0), '.ini')) do

  try

    SpeedButton1.Font.Color := TColor(ReadInteger('Form1', 'SpeedButton1 Font Color', integer(clBlack))); // black is the default

  finally

    free;

  end;

end;
 

procedure TForm1.FormDestroy(Sender: TObject);

begin

// save settings

  with TINIFile.Create(ChangeFileExt(Paramstr(0), '.ini')) do

  try

    WriteInteger('Form1', 'SpeedButton1 Font Color', integer(SpeedButton1.Font.Color));

  finally

    free;

  end;

end;

Open in new window

0
 

Author Comment

by:pr2501
ID: 20373819


  Yes, i can make it work in one form with there buttons .

 I have larger code with two units-forms;..
 i can't integrate it..
  i need help with variables declarations,...





 
 

unit Map;
 
 

    procedure TForm1.SpeedButtonClick(Sender: TObject);

begin
 

  whichMashine(TSpeedButton(Sender).Tag);

end;
 

   end.
 
 

unit PSX44;
 

 
 

var
 

  Form2: TForm2;

  podatki1: textfile;

  F:TForm2;

  Sp:TComponent;
 

procedure whichMashine(MachineIndex:Integer);

  begin

 F:=TForm2.Create(Application);

 F.mIndex:=inttostr(MachineIndex);

 Sp:=Form1.FindComponent('SpeedButton'+ intToStr(MachineIndex));

 F.Caption:=TSpeedButton(Sp).Caption ;

 F.Show;
 

end;
 

 procedure OpenFolder(FolderName: string);

   begin

 Screen.Cursor := crHourGlass;
 

 try

  ShellExecute(0, 'open', PChar(FolderName), '', '', SW_SHOWNORMAL);

 finally

  Screen.Cursor := crDefault;

 end;

end;
 

procedure TForm2.Button1Click(Sender: TObject);

begin

             OpenFolder('C:\PC1\Montaza\'+TSpeedButton(Sp).Caption);

end;
 

procedure TForm2.SpeedButton1Click(Sender: TObject);

     begin

          TSpeedButton(Sp).Font.Color:=clRed;

      end;
 

procedure TForm2.SpeedButton2Click(Sender: TObject);

     begin

         TSpeedButton(Sp).Font.Color:=clGreen;

         end;
 

procedure TForm2.Beri(Ime:string);

    var

          podatki1: textfile;

          vrstica: string;

  begin

     AssignFile(podatki1,Ime);

     Reset(podatki1);

     While not EOF (podatki1) do begin

     ReadLn(podatki1,vrstica);

  F.Memo1.Lines.Add (vrstica);

     end;

     CloseFile(podatki1);

  end;
 

procedure TForm2.Dodaj(Ime:string);

    var

    podatki1: textfile;

  begin

    AssignFile(podatki1,Ime);

    Append(podatki1);

    WriteLn(podatki1,Edit1.Text);

    CloseFile(podatki1);

  end;
 

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);

begin

             Dodaj('C:\PC1\Montaza\'+TSpeedButton(Sp).Caption+'\'+TSpeedButton(Sp).Caption+'.txt');

             memo1.Text:='';

end;

  

procedure TForm2.FormShow(Sender: TObject);

   begin

          beri('C:\PC1\Montaza\'+TSpeedButton(Sp).Caption+'\'+TSpeedButton(Sp).Caption+'.txt');

   end;
 
 
 

end.

Open in new window

0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 20378187
you lost me...
0
 

Author Comment

by:pr2501
ID: 20380385
thank you for trying to understand me.

1.
My SppedButon (SB) in  Form1 (F1) in realty is a machine in factory

I don't have only one SB in  F1:
SpeedButton1.Font.Color := TColor(ReadInteger('Form1', 'SpeedButton1 Font Color', integer(clBlack)));

But many:
SpeedButton(Index).Font.Color := TColor(ReadInteger('Form1', 'SpeedButton(Index) Font Color', integer(clBlack)));

2.
For itch SB on F1 i create a from from buttonclick  event using a  tag of SB for SB index:

procedure whichMashine(MachineIndex:Integer);
  begin
 F:=TForm2.Create(Application);
 F.mIndex:=inttostr(MachineIndex);
 Sp:=Form1.FindComponent('SpeedButton'+ intToStr(MachineIndex));
 F.Caption:=TSpeedButton(Sp).Caption ;
 F.Show;
 end;

Then i'm changing SB font color from F2 SB buttonclick event:

          TSpeedButton(Sp).Font.Color:=clRed;
or:
           TSpeedButton(Sp).Font.Color:=clGreen;

I have understood that i have to create a ini file and to write in it a string.
And to  read from it when F1 is created.

problem:
  I do need:
SppedButon (INDEX)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:pr2501
ID: 20380425


   to be more clear i can make next  example:

 In Form1 i have:
 Sppedbutton1:    machine1
 Sppedbutton2:   machine2

 in Form2 (which is created  from sbClick event on first form) i have:
Sppedbutton3:   make F1.SB (index) font color red
Sppedbutton4:   make F1.SB (index) font color green

 SpeedButton(index).Font.Color := TColor(ReadInteger('Form1', 'SpeedButton(index) Font Color', integer(clBlack)));
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 20381036
if you name all the speed buttons you want saved like this
"mySpeedButton1" "mySpeedButton2" "mySpeedButton3"
you can do something like this :-

TSpeedButton(Form1.FindComponent('MySpeedButton' + IntToStr(index))).Font.Color := ...

and for saving, you can do a loop like this :-

var
  cmp: tcomponent;
  i: integer;
begin
...open ini file... then
  i := 1;
  cmp := FindComponent('MySpeedButton' + IntToStr(i));
  while (cmp <> nil) do
  begin
    WriteInteger('Form1', cmp.Name + ' Font Color', integer(TSpeedButton(cmp).Font.Color));
    i := i + 1;
    cmp := FindComponent('MySpeedButton' + IntToStr(i));
  end;

I'm away from Delphi atm, but if casting  TSpeedButton(cmp) doesn't work, try it like
(cmp as TSpeedButton).Font.Color
instead. I think 1 way works, the other complains. can't remember
0
 

Author Comment

by:pr2501
ID: 20381286
How exactly do i

...open ini file...
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 20382444
:-/
as I posted before

  with TINIFile.Create(ChangeFileExt(Paramstr(0), '.ini')) do
  try
    i := 1;
    cmp := FindComponent('MySpeedButton' + IntToStr(i));
    while (cmp <> nil) do
    begin
      WriteInteger('Form1', cmp.Name + ' Font Color', integer(TSpeedButton(cmp).Font.Color));
      i := i + 1;
      cmp := FindComponent('MySpeedButton' + IntToStr(i));
    end;
  finally
    free;
  end;
0
 

Author Comment

by:pr2501
ID: 20387987

  Now i have problem with this line:

TSpeedButton(Form1.FindComponent('MySpeedButton' + IntToStr(index))).Font.Color :=
 
thank you
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 50 total points
ID: 20392134
what error are you getting?

here is a complete example I quickly wrote up...
I hope it helps you to understand..
unit Unit1;
 

interface
 

uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

  Buttons, StdCtrls;
 

type

  TForm1 = class(TForm)

    bbLoadColors: TBitBtn;

    bbSaveColors: TBitBtn;

    MySpeedButton1: TSpeedButton;

    MySpeedButton2: TSpeedButton;

    MySpeedButton3: TSpeedButton;

    ColorDialog1: TColorDialog;

    procedure bbLoadColorsClick(Sender: TObject);

    procedure bbSaveColorsClick(Sender: TObject);

    procedure MySpeedButtons_Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;
 

var

  Form1: TForm1;
 

implementation
 

{$R *.DFM}

uses inifiles;
 

procedure TForm1.bbLoadColorsClick(Sender: TObject);

var

  cmp: tcomponent;

  i: integer;

begin

  with TINIFile.Create(ChangeFileExt(Paramstr(0), '.ini')) do

  try

    i := 1;

    cmp := FindComponent('MySpeedButton' + IntToStr(i));

    while (cmp <> nil) do

    begin

      WriteInteger('Form1', cmp.Name + ' Font Color', integer(TSpeedButton(cmp).Font.Color));

      i := i + 1;

      cmp := FindComponent('MySpeedButton' + IntToStr(i));

    end;

  finally

    free;

  end;
 

end;
 

procedure TForm1.bbSaveColorsClick(Sender: TObject);

var

  cmp: tcomponent;

  i: integer;

begin

  with TINIFile.Create(ChangeFileExt(Paramstr(0), '.ini')) do

  try

    i := 1;

    cmp := FindComponent('MySpeedButton' + IntToStr(i));

    while (cmp <> nil) do

    begin

      TSpeedButton(cmp).Font.Color := TColor(ReadInteger('Form1', cmp.Name + ' Font Color', integer(clBlack)));

      i := i + 1;

      cmp := FindComponent('MySpeedButton' + IntToStr(i));

    end;

  finally

    free;

  end;
 

end;
 

procedure TForm1.MySpeedButtons_Click(Sender: TObject);

begin

  ColorDialog1.Color := TSpeedButton(Sender).Font.Color;

  if ColorDialog1.Execute then

    TSpeedButton(Sender).Font.Color := ColorDialog1.Color;

end;
 

end.
 
 

******************************

******* FORM FOLLOWS *********

******************************
 

object Form1: TForm1

  Left = 249

  Top = 107

  Width = 221

  Height = 169

  Caption = 'Form1'

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'MS Sans Serif'

  Font.Style = []

  OldCreateOrder = False

  PixelsPerInch = 96

  TextHeight = 13

  object MySpeedButton1: TSpeedButton

    Left = 16

    Top = 8

    Width = 23

    Height = 22

    Caption = 'A'

    OnClick = MySpeedButtons_Click

  end

  object MySpeedButton2: TSpeedButton

    Left = 16

    Top = 36

    Width = 23

    Height = 22

    Caption = 'B'

    OnClick = MySpeedButtons_Click

  end

  object MySpeedButton3: TSpeedButton

    Left = 16

    Top = 64

    Width = 23

    Height = 22

    Caption = 'C'

    OnClick = MySpeedButtons_Click

  end

  object bbLoadColors: TBitBtn

    Left = 16

    Top = 92

    Width = 75

    Height = 25

    Caption = 'bbLoadColors'

    TabOrder = 0

    OnClick = bbLoadColorsClick

  end

  object bbSaveColors: TBitBtn

    Left = 100

    Top = 92

    Width = 75

    Height = 25

    Caption = 'bbSaveColors'

    TabOrder = 1

    OnClick = bbSaveColorsClick

  end

  object ColorDialog1: TColorDialog

    Ctl3D = True

    Left = 128

    Top = 12

  end

end

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now