Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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

  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
pr2501
Asked:
pr2501
  • 5
  • 5
1 Solution
 
JohnjcesCommented:
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
 
TheRealLokiSenior DeveloperCommented:
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
 
pr2501Author Commented:


  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
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!

 
TheRealLokiSenior DeveloperCommented:
you lost me...
0
 
pr2501Author Commented:
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
 
pr2501Author Commented:


   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
 
TheRealLokiSenior DeveloperCommented:
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
 
pr2501Author Commented:
How exactly do i

...open ini file...
0
 
TheRealLokiSenior DeveloperCommented:
:-/
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
 
pr2501Author Commented:

  Now i have problem with this line:

TSpeedButton(Form1.FindComponent('MySpeedButton' + IntToStr(index))).Font.Color :=
 
thank you
0
 
TheRealLokiSenior DeveloperCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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