Solved

Define variables in form1 and see them in form2

Posted on 2007-11-25
14
467 Views
Last Modified: 2010-04-05
I haw 100 SppedButtons.

1.
From a SppedButton1 in Form1:

procedure TForm1.SpeedButton(VAR)Click(Sender: TObject);
begin
     //1 tell to procedure TForm2.SpeedButton2Click for which SppedButton(VAR) is sent the request for //changing font color
//2 and path to the folder for a specified machine
       PSX44.Form2.ShowModal;
   end;


  need to tell to procedure in Form2

procedure TForm2.SpeedButton2Click(Sender: TObject);
begin
     Form1.????????.Font.Color:=clGreen;
end;

2.
In the next code i have variable ('D:\PC1DelphiPregled\Montaza\PS-X44\psx44alarm.txt') of path to the folder in which I'm reading and writing the data.

And i have the similar problem like before i don't know haw define variable in
procedure TForm1.SpeedButton(VAR)Click to tell  with which file i need to communicate.


If i resume:
I Have a form with picture of factory and each machine is one sppedbotton.
I need to change a color of a font of a speedbutton on a Form1 from sppedbuttons on form2.
Because of this form2 need to know from which sppedbuton I'm sending the request.

And on form2 i have Memo  in which i'm writing and reading a data from specified file.
But again i have to change a path to the specified file which corresponds for special machine.

It works for one machine but i  cant make 100 units and connect them to work with specified machine.



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('D:\PC1DelphiPregled\Montaza\PS-X44');
end;
 
procedure TForm2.SpeedButton1Click(Sender: TObject);
   begin
      Form1.SpeedButton1.Font.Color:=clred;
end;
 
procedure TForm2.SpeedButton2Click(Sender: TObject);
begin
     Form1.SpeedButton1.Font.Color:=clGreen;
end;
 
procedure TForm2.Beri(Ime:string);
    var
         data1: textfile;
          myline: string;
  begin
     AssignFile(data1,myNome);
     Reset(data1);
     While not EOF (data1) do begin
     ReadLn(data1,myline);
     Form2.Memo1.Lines.Add (myline);
     end;
     CloseFile(data11);
  end;
 
procedure TForm2.Dodaj(MyNome:string);
    var
data1: textfile;
  begin
    AssignFile(data1,myNome);
    Append(data1);
    WriteLn(data1,Edit1.Text);
    CloseFile(data1);
  end;
 
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  addtoMemo ('D:\PC1DelphiPregled\Montaza\PS-X44\psx44alarm.txt');
     Edit1.Text:='';
     memo1.Text:='';
end;
 
procedure TForm2.FormShow(Sender: TObject);
begin
  Read('D:\PC1DelphiPregled\Montaza\PS-X44\psx44alarm.txt');  end;
 
end.

Open in new window

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
  • 7
  • 5
  • 2
14 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20346363
To carry variables from one unit (form) to another unit (form) you can use global variables.

In your form's unit, you need to add the variable to the first var section, which is before "implementation".
Then, don't forget to add that unit, to the "uses" clause of Form2,  here is an example:

unit Form1;

interface

uses
...

types
...

private
   (Private declarations)
public
   (public declarations)
end;

var
**** <--- put your global variables here, example:
MyGlobalVariable : integer;

implementation
...

======

Form 2 example:

unit Form2;

interface

uses
Form1;
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20346593
I'm not sure I understand exactly what you are asking.

What is wrong with what you have?
What is wrong with this:
procedure TForm2.SpeedButton1Click(Sender: TObject);
   begin
      Form1.SpeedButton1.Font.Color:=clred;
end;
0
 
LVL 10

Expert Comment

by:dinilud
ID: 20348358
can you explain More
0
Industry Leaders: 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!

 

Author Comment

by:pr2501
ID: 20348554
Before i'll tray to resolve my problem from the rfwolf answer i'll
tray to be more clear.

I have 100 of Spped butons: itch of them corispond to one machine in industry factory.
On the Form1 i have plane (map) of factory and the SppedButons are located where the machines stands in real situation. With clicking on the sppedbuton i get second form on which a have 3 others sppedbutons. From the first sppedbuton i can make SB (sppedbutton) on F1(form1) become red (which means that we have problem with this machine), from the SB2 i can make it become green again (which means that the problem is resolved) The SP3  opens the file folder (windows) which condends all machine folders (pdf manuals, word text files with history of folts on the machine, excel files with demaged peases wich were changed, pictures ...)
Then on F2 is a memo (which gets data from .txt file) in which i write short   notes from Edit1 about machine problems and read them next time a open the F2.

Every thing  works fine, only i can't make memo window proceed ( with text proceed ( to align  it with
down scrollbars)).

But  i need to make it work for every machine in factory.
It means that i need next variables:
1. machine(spedbutton(var)) to change a color of font text from F2
2.machine(fileForNotes(var)) for writing and reading short notes
3.machine(mainFileFolder(var)) for arhiing all informations about machine
0
 
LVL 10

Expert Comment

by:dinilud
ID: 20348634
Try this
Project1.dpr
===============
 
program Project1;
 
uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};
 
{$R *.res}
 
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
 
 
Unit1.dfm
===========
 
object Form1: TForm1
  Left = 155
  Top = 163
  Width = 191
  Height = 237
  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 SpeedButton1: TSpeedButton
    Tag = 1
    Left = 16
    Top = 8
    Width = 145
    Height = 33
    Caption = 'Machine 1  (tag 1)'
    OnClick = SpeedButtonClick
  end
  object SpeedButton2: TSpeedButton
    Tag = 2
    Left = 19
    Top = 56
    Width = 145
    Height = 33
    Caption = 'Machine 2  (tag 2)'
    OnClick = SpeedButtonClick
  end
  object SpeedButton3: TSpeedButton
    Tag = 3
    Left = 21
    Top = 107
    Width = 145
    Height = 33
    Caption = 'Machine 3  (tag 3)'
    OnClick = SpeedButtonClick
  end
  object SpeedButton4: TSpeedButton
    Tag = 4
    Left = 22
    Top = 159
    Width = 145
    Height = 33
    Caption = 'Machine 4  (tag 4)'
    OnClick = SpeedButtonClick
  end
end
 
 
Unit1.pas
==========
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons;
 
type
  TForm1 = class(TForm)
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    procedure SpeedButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
uses Unit2;
 
{$R *.dfm}
 
 
 
procedure TForm1.SpeedButtonClick(Sender: TObject);
begin
  ShowMachineProblem(TSpeedButton(Sender).Tag); 
end;
 
end.
 
 
Unit2.dfm
===========
object Form2: TForm2
  Left = 188
  Top = 193
  Width = 200
  Height = 201
  Caption = 'Form2'
  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 SpeedButton1: TSpeedButton
    Left = 16
    Top = 8
    Width = 145
    Height = 33
    Caption = 'Show Problem'
    OnClick = SpeedButton1Click
  end
  object SpeedButton2: TSpeedButton
    Left = 19
    Top = 56
    Width = 145
    Height = 33
    Caption = 'Problem Solved'
    OnClick = SpeedButton2Click
  end
  object SpeedButton3: TSpeedButton
    Left = 21
    Top = 107
    Width = 145
    Height = 33
    Caption = 'Show Note'
    OnClick = SpeedButton3Click
  end
end
 
 
 
Unit2.pas
===========
unit Unit2;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons;
 
type
  TForm2 = class(TForm)
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
  private
    { Private declarations }
    ProblemIndex:Integer;
  public
    { Public declarations }
  end;
 
  procedure ShowMachineProblem(MachineIndex:Integer);
 
implementation
 
uses Unit1;
 
{$R *.dfm}
 
procedure ShowMachineProblem(MachineIndex:Integer);
var F:TForm2;
begin
  F:=TForm2.Create(Application);
  F.ProblemIndex:=MachineIndex;
  F.Caption:='Machine '+IntToStr(MachineIndex); 
  F.Show;
end;
 
procedure TForm2.SpeedButton1Click(Sender: TObject);
var Sp:TComponent;
begin
   Sp:=Form1.FindComponent('SpeedButton'+IntToStr(ProblemIndex));
   TSpeedButton(Sp).Font.Color:=clRed;
   ShowMessage('I Have some problem');
end;
 
procedure TForm2.SpeedButton2Click(Sender: TObject);
var Sp:TComponent;
begin
   Sp:=Form1.FindComponent('SpeedButton'+IntToStr(ProblemIndex));
   TSpeedButton(Sp).Font.Color:=clGreen;
   ShowMessage('problem Solved');
end;
 
procedure TForm2.SpeedButton3Click(Sender: TObject);
begin
  ShowMessage('Show Problem'+IntToStr(ProblemIndex)+'.txt');
end;
 
end.

Open in new window

0
 

Author Comment

by:pr2501
ID: 20349424


have can i use tag of my sppedbutons (machines) in my code
to tell that i need to change color of font of exactly that SP that was clicked.

 
0
 
LVL 10

Expert Comment

by:dinilud
ID: 20349457
did you check my example?
0
 

Author Comment

by:pr2501
ID: 20349470
In my F1 from next code i'm opening second F2

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
     
       PSX44.Form2.ShowModal;
   end;

and here i need to tel  for which SB i need to change color
and which main file folder i need to  open, and whit in which folder i need to write and then read data from it into memo



0
 

Author Comment

by:pr2501
ID: 20349478

Today i have many problems  on machine . I hope i'll can do it after  1 hour.
0
 
LVL 10

Expert Comment

by:dinilud
ID: 20349487
PSX44.Form2.ShowModal;


What it means?

What is PSX44?

0
 

Author Comment

by:pr2501
ID: 20349857
 it's name of my second unit (in practice is a name of a machine)

i have made two units. One with F1 and second (PSX44) with form2.

  F1 have 100 of SB and  F2  have 3 SB, Edit1 and memo1.

  From itch  of SB of F1 i need to use 3 SB , edi1 and memo1 on F2.

  But  1 and 2  SB on F2 need to make different (that which was clicked) SB on F1 to change color.
  And to read from different txt file( which corresponds to defined machine which is collected (by meaning) to specified machine) and to write from Edit1 to specified txt file.

  The purpose of 3 SB on F2 is to open a main file folder which corresponds to defined machine.

  F1.SB1  (machine 1)  color  (green / red) has to be  change from F2.SB1(red) and F2.SB2 (green)
  txt file1    (machine1) mast be writed from  F2 Edit1 read from F2.memo1
  main folder1 (machine1) must be opened from F2.SB3.

  F1.SB2  (machine 2)  color  (green / red) has to be  change from F2.SB1(red) and F2.SB2 (green)
  txt file2    (machine2) mast be writed from  F2 Edit1 read from F2.memo1
  main folder2 (machine2) must be opened from F2.SB3.

  F1.SB3  (machine 3)  color  (green / red) has to be  change from F2.SB1(red) and F2.SB2 (green)
  txt file3    (machine3) mast be writed from  F2 Edit1 read from F2.memo1
  main folder3 (machine3) must be opened from F2.SB3.


 F1.SB4  (machine 4)  color  (green / red) has to be  change from F2.SB1(red) and F2.SB2 (green)
  txt file4    (machine4) mast be writed from  F2 Edit1 read from F2.memo1
  main folder4 (machine4) must be opened from F2.SB3.



 
0
 
LVL 10

Accepted Solution

by:
dinilud earned 125 total points
ID: 20350081
i think my eg work almost like this .
0
 

Author Comment

by:pr2501
ID: 20350423


  Thank you
   
   I hope that i'll find out what i must do  at evening. I'll let you know tomorrow.
 
0
 

Author Comment

by:pr2501
ID: 20358101


Ok

Thank you

i made it work
unit PSX44;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons,ShellAPI, DBCtrls, Mask, DB;
 
type
  TForm2 = class(TForm)
    GroupBox1: TGroupBox;
    SpeedButton1: TSpeedButton;
    GroupBox2: TGroupBox;
    Button1: TButton;
    GroupBox4: TGroupBox;
    Memo1: TMemo;
    GroupBox3: TGroupBox;
    Edit1: TEdit;
    SpeedButton2: TSpeedButton;
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
 
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
 
 
 
  private
    { Private declarations }
       mIndex:string;
 
  public
        procedure Beri(Ime:string);
        procedure Dodaj(Ime:string);
 
    { Public declarations }
  end;
 
   procedure whichMashine(MachineIndex:Integer);
var
  Form2: TForm2;
  podatki1: textfile;
  F:TForm2;
  Sp:TComponent;
   txtCaption:string;
 
implementation
 
uses odpiranjeMap;
 
{$R *.dfm}
          //stroj
procedure whichMashine(MachineIndex:Integer);
  begin
 F:=TForm2.Create(Application);
 F.mIndex:=inttostr(MachineIndex);
 Sp:=Form1.FindComponent('SpeedButton'+ intToStr(MachineIndex));
 F.Caption:=TSpeedButton(Sp).Caption ;
  // txtcaption:=TSpeedButton(Sp).Caption;
 F.Show;
 
end;
         //odpri
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('D:\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 ('D:\PC1\Montaza\'+TSpeedButton(Sp).Caption+'\'+TSpeedButton(Sp).Caption+'.txt');
       Dodaj('D:\PC1\Montaza\'+TSpeedButton(Sp).Caption+'\'+TSpeedButton(Sp).Caption+'.txt');
 
     memo1.Text:='';
end;
 
procedure TForm2.FormShow(Sender: TObject);
begin
// beri('D:\PC1\Montaza\'+TSpeedButton(Sp).Caption+'\'+TSpeedButton(Sp).Caption+'.txt');
   beri('D:\PC1\Montaza\'+TSpeedButton(Sp).Caption+'\'+TSpeedButton(Sp).Caption+'.txt');
   end;
 
end.

Open in new window

0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CheckListBox usage 3 97
Sending files from  idTcpServer Socket to idTcpClient 2 65
Multi-layered image in FireMonkey 9 93
Delphi problems to abort a printjob 9 49
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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