Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Second Form and Thread (https://www.delphipraxis.net/113225-second-form-thread.html)

mohfa 4. Mai 2008 21:48


Second Form and Thread
 
Hi , in my application i use a Second Form which is created on RunTime
Delphi-Quellcode:
var
MyNewForm:TSecondForm { this is the Second Form }
.
.
On the Main Form Creat Event
begin
if Not Assigned(MyNewForm) then
MyNewForm:=TSecondForm.create(Nil);
end;

// and release it on main Form Destroy event

MyNewForm.release;
in this Second Form i have 3 Buttons and 2 Labels

Delphi-Quellcode:
on main form button1 click event
MyNewForm.ShowModal;
when i launch this Second Form for the First Time every thing goes well , but when i launch it for the Second time it Freezes the Form is Shown but all other Controls are Hidden ( 3 BUTTONS + 2 LABELS ).
i thought it's because the Form is not safe Threaded.
but how could i thread it , the Values of the 2 labels are from the Main Form
Delphi-Quellcode:
.
.
MyNewForm.label1.caption:={mainform.}label1.caption;
MyNewForm.label2.caption:={mainform.}label2.caption;
.
.
so how could i thread my SecondForm so that whenever i launch it it will not freeze and all their controls are still Visible .


Regards.

mohfa 5. Mai 2008 02:36

Re: Second Form and Thread
 
please is there any solution or suggestion on this Issue .

is it possible to create the Second Form threaded .

christian_r 5. Mai 2008 03:02

Re: Second Form and Thread
 
Good morning.

A modal form has not to be closed with "TForm.Close".

Zitat:

Zitat von Delphi Help
To close a modal form, set its ModalResult property to a nonzero value.

As next here is the example of the same help topic. It explains, how to close the modal form in the right way. ;)

Delphi-Quellcode:
procedure TMyDialogBox.OKButtonClick(Sender: TObject);

begin
  ModalResult := mrOK; // mrOK ...
end;

procedure TMyDialogBox.CancelButtonClick(Sender: TObject);
begin
  ModalResult := mrCancel; // ... and mrCancel are nonzero values
end;

// This code brings up the modal dialog from Form1 when a button is clicked. It causes a Beep if the OK button is clicked.

procedure TForm1.Button1Click(Sender: TObject);

begin
  if MyDialogBox1.ShowModal = mrOK then
    Beep;
end;
So you don't need to thread this modal form for that simple problem.

mohfa 5. Mai 2008 03:21

Re: Second Form and Thread
 
[quote="christian_r"]Good morning.

A modal form has not to be closed with "TForm.Close".

Zitat:

Zitat von Delphi Help
To close a modal form, set its ModalResult property to a nonzero value.

As next here is the example of the same help topic. It explains, how to close the modal form in the right way. ;)

Hi christian_r that's not the problem , my problem is why when i Click for the Seconde Time on the Button to show the Second Form all Second Form controls are disappeared

christian_r 5. Mai 2008 08:26

Re: Second Form and Thread
 
Zitat:

Zitat von mohfa
but when i launch it for the Second time it Freezes the Form is Shown but all other Controls are Hidden ( 3 BUTTONS + 2 LABELS ).

What does it means - "it freezes"? Can't you close it?

I think you should post more relevant code. The code you posted above is not the cause of the error in the manner described. (But this fact doesn't prevent you from fixing your kind of closing your modal form. ;) )

mohfa 6. Mai 2008 21:27

Re: Second Form and Thread
 
Hi christian_r , my problem is not in Closing the Second Form , my prolem is when i click 4 the Second Time to show the 2nd Form .
Here the 2nd Form is Shown but without Buttons or Labels . Only an empty Form .
Here is a Pseudo Code .
// the Second Form contains : 2 Buttons , 2 Labels .
Delphi-Quellcode:
On MainForm Creat Event
begin
if Not Assigned(SecondForm) then
SecondForm:=TSecondForm.Create(Nil); // here i Create the 2nd Form .
end;

On MainForm Destroy
begin
SecondForm.Release;// the 2nd Form is released
end;
(* 
Here when i click on this Button1 the SecondForm is Shown
 so 1st Click the Form is Shown Correctly with all its Controls.
 the 2nd Click the Form is Shown but without any Controls on It .
 Only an Empty Form
*)
On TMainForm.Button1.Click
begin
SecondForm.Label1.caption:={MainForm.}Label1.Caption;
SecondForm.Label2.caption:={MainForm}.Label2.Caption;
if SecondForm.ShowModal=MrOk then
// i have a Function to Execute
end;
Regards

christian_r 7. Mai 2008 07:22

Re: Second Form and Thread
 
Zitat:

Zitat von mohfa
Here is a Pseudo Code.

I think you should post the relevant code you really used.

Delphi-Quellcode:
On MainForm Creat Event
On MainForm Destroy
On TMainForm.Button1.Click
What is that? :gruebel: You should post pseudo code that could be used and doesn't have to be corrected.

Here is a code that works. It's tested! It's the code, you posted as a pseudo code. And it doesn't cause any compiler errors and it doesn't cause your problem. It works fine!

unit1
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Classes, Controls, StdCtrls, Forms, Dialogs;

type
  TForm1 = class( TForm )
    Button1 : TButton;
    Label1  : TLabel;
    Label2  : TLabel;
    procedure FormCreate
              ( Sender : TObject );
    procedure Button1Click
              ( Sender : TObject );
  end;

var
  Form1 : TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.FormCreate
          ( Sender : TObject );
begin
  if not Assigned( Form2 ) then
    Form2 := TForm2.Create( Nil );
end;

procedure TForm1.Button1Click
          ( Sender : TObject );
begin
  Form2.Label1.Caption := 'Caption Nr. 1';
  Form2.Label2.Caption := 'Caption Nr. 2';
  if Form2.ShowModal = mrOk then
    ShowMessage( 'Ok.' );
end;

end.
unit2
Delphi-Quellcode:
unit Unit2;

interface

uses
  Windows, Classes, Controls, StdCtrls, Forms, Dialogs;

type
  TForm2 = class( TForm )
    Label1  : TLabel;
    Label2  : TLabel;
    Button1 : TButton;
    Button2 : TButton;
    procedure Button1Click
              ( Sender : TObject );
  end;

var
  Form2 : TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click
          ( Sender : TObject );
begin
  Self.ModalResult := mrOk;
end;

end.
And again ...
Check your code!
Stop to discuss!
Post your relevant code!


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:06 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz