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
291 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
Comment Utility
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
Comment Utility
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
Comment Utility


  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
Comment Utility
you lost me...
0
 

Author Comment

by:pr2501
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:pr2501
Comment Utility


   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
Comment Utility
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
Comment Utility
How exactly do i

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

Expert Comment

by:TheRealLoki
Comment Utility
:-/
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
Comment Utility

  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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

15 Experts available now in Live!

Get 1:1 Help Now