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
314 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
[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
  • 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
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!

 
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
 

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

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

738 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