Recent

Author Topic: How to call Form3 by OnClick  (Read 4425 times)

zkdjeksa

  • New Member
  • *
  • Posts: 10
How to call Form3 by OnClick
« on: September 09, 2018, 03:16:29 pm »
I want to call Form3 out and let Form1 invisible by using the procedure OnClick, but how to write the procedure?

This is how it looks like now.

unit unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, unit3;

type

  { TForm1 }

  TForm1 = class(TForm)
    Instrc: TLabel;
    Name1: TLabel;
    Startg: TLabel;
    procedure StartgClick(Sender: TObject);
    procedure StartgMouseEnter(Sender: TObject);
    procedure StartgMouseLeave(Sender: TObject);
  private

  public

  end;
var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.StartgMouseEnter(Sender: TObject);
begin
  Startg.Font.Style:=[fsBold];
end;

procedure TForm1.StartgMouseLeave(Sender: TObject);
begin
  Startg.Font.Style:= [];
end;

procedure TForm1.StartgClick(Sender: TObject);
begin
 
end;
end.       

Besides this,  I try to add Form3.show to the procedure. But the error "Project S1 raised exception class 'external: SIGSEGV'. In file '.\include\customeform.inc' at line 2313" always appear when I try to click the button after I run the program.

Bart

  • Hero Member
  • *****
  • Posts: 5663
    • Bart en Mariska's Webstek
Re: How to call Form3 by OnClick
« Reply #1 on: September 09, 2018, 03:39:37 pm »
Is Form3 autocreated?
Menu->Project->Forms. If it is autocreated then it will be listed there.

If not, you must create it first.

Bart

zkdjeksa

  • New Member
  • *
  • Posts: 10
Re: How to call Form3 by OnClick
« Reply #2 on: September 09, 2018, 04:01:30 pm »
Form 3 has been created already.

Bart

  • Hero Member
  • *****
  • Posts: 5663
    • Bart en Mariska's Webstek
Re: How to call Form3 by OnClick
« Reply #3 on: September 09, 2018, 04:06:59 pm »
Please attach the project (zipped) here.
Don't zip the executable: source files only please.

Without full source code (including lfm's) it's impossible to tell.

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to call Form3 by OnClick
« Reply #4 on: September 09, 2018, 08:50:12 pm »
Form 3 has been created already.

Just in case it is not (and because Murphy!) try this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StartgClick(Sender: TObject);
  2. begin
  3.   if not Assigned(Form3) then
  4.     Application.CreateForm(TForm3, Form3);
  5.   Form3.Show;
  6.   Self.Hide;
  7.   {WARNING!
  8.     The following is *NOT* recommended!!!.
  9.     The proper thing to do is have Form3 show Form1
  10.     when it has ended v.g. in an OnCloseQuery handler.}
  11.   while Form3.Showing do
  12.     Application.ProcessMessages;
  13.   Self.Show;
  14.   {/WARNING}
  15. end;

BTW,
[...] the error "Project S1 raised exception class 'external: SIGSEGV'. In file '.\include\customeform.inc' at line 2313" always appear when I try to click the button after I run the program.

Which button do you click? TForm1, apparently , only has three TLabels. Have you shown us your full code?
« Last Edit: September 09, 2018, 09:45:20 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

nouzi

  • Sr. Member
  • ****
  • Posts: 322
Re: How to call Form3 by OnClick
« Reply #5 on: September 09, 2018, 09:55:51 pm »
//form1
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StartgClick(Sender: TObject);
  2. begin
  3.  hide ;
  4. Form3.Show;
  5.  
  6. end;
  7.  

// form3
Code: Pascal  [Select][+][-]
  1. procedure TForm3.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3. Form1.Show;
  4.  
  5. end;
  6.  

My English is  bad
Lazarus last version free pascal last version
Lazarus trunk  free pascal trunk 
System : Linux mint  64bit  Windows 7 64bit

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to call Form3 by OnClick
« Reply #6 on: September 09, 2018, 10:05:40 pm »
@nouzi:

Yes, that would work, although I prefer to do such things in the OnCloseQuery handler rather than in the OnClose one. That way you can simply set CanClose := False if something goes wrong and your appplication is not left in limbo if, for some reason, CloseAction is set to caHide (or worse: caFree) in FormXClose().
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

zkdjeksa

  • New Member
  • *
  • Posts: 10
Re: How to call Form3 by OnClick
« Reply #7 on: September 10, 2018, 10:24:21 am »
@lucamar
After i run the program, i try to click the "start game" button to test whether Form3.show works or not, but the error message popup.

This will be my program now after applied your suggestion.

unit unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, unit2, unit3;

type

  { TForm1 }

  TForm1 = class(TForm)
    Instrc: TLabel;
    Name1: TLabel;
    Startg: TLabel;
    procedure InstrcClick(Sender: TObject);
    procedure InstrcMouseEnter(Sender: TObject);
    procedure InstrcMouseLeave(Sender: TObject);
    procedure StartgClick(Sender: TObject);
    procedure StartgMouseEnter(Sender: TObject);
    procedure StartgMouseLeave(Sender: TObject);
  private

  public

  end;
var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.StartgMouseEnter(Sender: TObject);
begin
  Startg.Font.Style:=[fsBold];
end;

procedure TForm1.StartgMouseLeave(Sender: TObject);
begin
  Startg.Font.Style:= [];
end;

procedure TForm1.StartgClick(Sender: TObject);
begin
  if not Assigned(Form3) then
     Application.CreateForm(TForm3, Form3);
   Form3.Show;
   Self.Hide;
   {WARNING!
     The following is *NOT* recommended!!!.
     The proper thing to do is have Form3 show Form1
     when it has ended v.g. in an OnCloseQuery handler.}
   while Form3.Showing do
     Application.ProcessMessages;
   Self.Show;
   {/WARNING}
end;

procedure TForm1.InstrcClick(Sender: TObject);
begin
 Form2.show;
end;

procedure TForm1.InstrcMouseEnter(Sender: TObject);
begin
 Instrc.Font.Style:= [fsBold];
end;

procedure TForm1.InstrcMouseLeave(Sender: TObject);
begin
 Instrc.Font.Style:= [];
end;

end.           

zkdjeksa

  • New Member
  • *
  • Posts: 10
Re: How to call Form3 by OnClick
« Reply #8 on: September 10, 2018, 10:29:22 am »
@lucamar
I tried again and now it does work. Thank you very much.

Handoko

  • Hero Member
  • *****
  • Posts: 5513
  • My goal: build my own game engine using Lazarus
Re: How to call Form3 by OnClick
« Reply #9 on: September 10, 2018, 10:31:26 am »
When posting source to this forum, you should use code tag. Read more:
http://wiki.lazarus.freepascal.org/Forum

Also you should consider to provide the whole project as what @Bart said. To do it:
Create a new folder. Copy all the necessary files to that folder including data, database and images, except: *.bak, lib and backup folder. Compress the folder and send the zip file to the forum.

zkdjeksa

  • New Member
  • *
  • Posts: 10
Re: How to call Form3 by OnClick
« Reply #10 on: September 10, 2018, 10:50:27 am »
@Handoko
Thank you for your advice. :D

nouzi

  • Sr. Member
  • ****
  • Posts: 322
Re: How to call Form3 by OnClick
« Reply #11 on: September 13, 2018, 12:19:17 am »
@lucamar
thank
My English is  bad
Lazarus last version free pascal last version
Lazarus trunk  free pascal trunk 
System : Linux mint  64bit  Windows 7 64bit

 

TinyPortal © 2005-2018