Recent

Author Topic: Showing a second form using a Menu  (Read 11815 times)

ThomasK1201

  • New Member
  • *
  • Posts: 31
Showing a second form using a Menu
« on: March 02, 2015, 01:49:35 pm »
hi guys!

As first I want to apologize for my not-so-perfect English. Im a student from Holland and Im not that good at English :S

For a instruction on school I need to make students able to make excercises for math and basic calculation and stuff in Lazarus. I made a menu and from that menu, you should be able to get to another form to make excercises. But how can I make that happen? I tried Name.Show, but that doesnt work. And Im sure I didnt make a basic mistake like forgetting ; ..

Thomas.

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Showing a second form using a Menu
« Reply #1 on: March 02, 2015, 02:15:40 pm »
Have you added the corresponding unit to the 'uses' clause of your main form?

For instance, for a basic project with 2 forms (Form1/Unit1 and Form2/Unit2), in Unit1 (when menu is clicked):
Code: [Select]
uses Unit2;

procedure TForm1.MenuItem1Click(Sender: TObject);
begin
  Form2.Show;   // eventually, ShowModal
end;

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: Showing a second form using a Menu
« Reply #2 on: March 03, 2015, 11:03:29 am »
That might be the problem, as soon as I'm home I'll look if that is the mistake I made :)

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: Showing a second form using a Menu
« Reply #3 on: March 04, 2015, 01:22:25 pm »
It still doesn't work but I did exactly what you said me to.. Or I haven't understand you  :(
So I just paste my code in here :D

I called the units and forms like this:
Main unit = SommenOefeningUnit
Second unit = PlusMakkelijkUnit
Main form = FormMenu
Second Form = PlusMakkelijk
I hope the Dutch names I gave them don't distract you guys :S

This is my main form:
Code: [Select]
unit SommenOefeningUnit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus;

type

  { TFormMenu }

  TFormMenu = class(TForm)
    Handleiding: TMenuItem;
    PlusMakkelijk: TMenuItem;
    SommenMenu: TMainMenu;
    Delen: TMenuItem;
    MenuItem1: TMenuItem;
    Inhoudsmaten: TMenuItem;
    AllesDoorElkaar: TMenuItem;
    Oppervlakte: TMenuItem;
    Vermenigvuldigen: TMenuItem;
    Minsommen: TMenuItem;
    Sommen: TMenuItem;
    Plussommen: TMenuItem;
    procedure PlusMakkelijkClick(Sender: TObject);
    procedure PlussommenClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  FormMenu: TFormMenu;

implementation

{$R *.lfm}

{ TFormMenu }

uses PlusMakkelijkUnit;

procedure TFormMenu.PlusMakkelijkClick(Sender: TObject);
begin
   PlusMakkelijk.Show;
end;

end.


For now there is no code in my second form because I wanted to link them first, but Im not sure if I have to put some code in my second form as well to link them..
« Last Edit: March 04, 2015, 01:25:25 pm by ThomasK1201 »

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Showing a second form using a Menu
« Reply #4 on: March 04, 2015, 02:50:49 pm »
You are not saying what you mean by 'It still doesn't work':
- does it compile correctly ?
- if yes, do you mean that the second form is just not shown ?

Anyway, as far as I can see, there is problem with your component names. Theoretically, you cant' have a second form called 'PlusMakkelijk' and also a menuitem called with the same name 'PlusMakkelijk' in FormMenu.

So, I guess your second form has another name. Or it's not included in the project. Or ...

Attached, a basic sample (YourTest.zip) trying to use your own component/unit names (with only a unique sub menuitem). Please note that I've had to rename the sub menuitem differently: PlusMakkelijkShow.

Source code for the main form:
Code: [Select]
unit SommenOefeningUnit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus;

type

  { TFormMenu }

  TFormMenu = class(TForm)
    Handleiding: TMenuItem;
    PlusMakkelijkShow: TMenuItem;
    SommenMenu: TMainMenu;
    procedure PlusMakkelijkShowClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  FormMenu: TFormMenu;

implementation

{$R *.lfm}

{ TFormMenu }

uses PlusMakkelijkUnit;

procedure TFormMenu.PlusMakkelijkShowClick(Sender: TObject);
begin
  PlusMakkelijk.Show;
end;

end.
« Last Edit: March 04, 2015, 03:23:50 pm by ChrisF »

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: Showing a second form using a Menu
« Reply #5 on: March 04, 2015, 07:42:23 pm »
Sorry :S it doesn't compile correctly; it gives the following error: Identifier not found ''PlusMakkelijk''

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Showing a second form using a Menu
« Reply #6 on: March 04, 2015, 07:45:24 pm »
Are you talking of your program, or of my sample project ? Because I'm sure mine is compiling correctly.

BTW, which OS are you using (just in case) ?

And if it's yours it's surprising, because PlusMakkelijk is also the name of your menuitem (I can see it in your source code) ?

So, even if your form is not called PlusMakkelijk, or if you've not added the PlusMakkelijkUnit unit in your SommenOefeningUnit unit, you should get instead an error complaining about the unknown property 'show' for the menuitem PlusMakkelijk ?
« Last Edit: March 04, 2015, 08:09:30 pm by ChrisF »

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: Showing a second form using a Menu
« Reply #7 on: March 04, 2015, 08:58:20 pm »
I just made a new project with a few new names. The only error I get now is Identifier idents no member "ShowModal"

Here's my new code:

Code: [Select]
unit MainForm;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
  PlusMakkelijkUnit;

type

  { TFormMenu }

  TFormMenu = class(TForm)
    HoofdMenu: TMainMenu;
    LeerlingenMenu: TMenuItem;
    PlusMenu: TMenuItem;
    PlusMakkelijkShow: TMenuItem;
    MinMenu: TMenuItem;
    VermenigvuldigenMenu: TMenuItem;
    DelenMenu: TMenuItem;
    MachtsheffenMenu: TMenuItem;
    InhoudsmatenMenu: TMenuItem;
    OppervlakteMenu: TMenuItem;
    AllesDoorElkaarMenu: TMenuItem;
    HandleidingMenu: TMenuItem;
    Docenten: TMenuItem;
    HandleidingMenuDocenten: TMenuItem;
    procedure PlusMakkelijkShowClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  FormMenu: TFormMenu;

implementation

{$R *.lfm}

{ TFormMenu }

procedure TFormMenu.PlusMakkelijkShowClick(Sender: TObject);
begin
   PlusMakkelijkShow.ShowModal;
end;

end.

And Im feeling really dumb right now, but what is a OS?  :(
« Last Edit: March 04, 2015, 09:01:15 pm by ThomasK1201 »

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Showing a second form using a Menu
« Reply #8 on: March 04, 2015, 09:01:10 pm »
This is normal.

PlusMakkelijkShow is a menuitem: it doesn't have any Show/ShowModal property.

Put instead your second form name (is it still PlusMakkelijk ?), and not your menuitem name: you've now inversed both of them.

**Edit **

OS ? Sorry, my fault. OS usually means Operating System (http://en.wikipedia.org/wiki/Operating_system)

For instance, in our case : Windows, MacOS, Linux, ...
« Last Edit: March 04, 2015, 09:22:15 pm by ChrisF »

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: Showing a second form using a Menu
« Reply #9 on: March 04, 2015, 09:04:53 pm »
I did it but now I get this error:
Project MainUnit raised exception class 'EReadError' with message: Invalid value for property. At adress 446CE7.

And the previous error was in my program.

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Showing a second form using a Menu
« Reply #10 on: March 04, 2015, 09:07:01 pm »
Could you attached here your whole project sample, please (if I've understood correctly, it's not very big, isn't it)?

I'll have a look at it.

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: Showing a second form using a Menu
« Reply #11 on: March 04, 2015, 09:08:37 pm »
Nope, it isnt very big :P

Code: [Select]
unit MainForm;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
  PlusMakkelijkUnit;

type

  { TFormMenu }

  TFormMenu = class(TForm)
    HoofdMenu: TMainMenu;
    LeerlingenMenu: TMenuItem;
    PlusMenu: TMenuItem;
    PlusMakkelijkShow: TMenuItem;
    MinMenu: TMenuItem;
    VermenigvuldigenMenu: TMenuItem;
    DelenMenu: TMenuItem;
    MachtsheffenMenu: TMenuItem;
    InhoudsmatenMenu: TMenuItem;
    OppervlakteMenu: TMenuItem;
    AllesDoorElkaarMenu: TMenuItem;
    HandleidingMenu: TMenuItem;
    Docenten: TMenuItem;
    HandleidingMenuDocenten: TMenuItem;
    procedure PlusMakkelijkShowClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  FormMenu: TFormMenu;

implementation

{$R *.lfm}

{ TFormMenu }

procedure TFormMenu.PlusMakkelijkShowClick(Sender: TObject);
begin
   FormPlusMakkelijk.ShowModal;
end;

end.

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Showing a second form using a Menu
« Reply #12 on: March 04, 2015, 09:19:05 pm »
By "full project", l meant all the files of your project, compressed in one zip/rar file, and attached to one of your posts in this topic.

Like I've done it with my project sample (i.e. the "YourTest.zip" file).

Only the sources, so only the following file types: *.pas, *.lfm, *.lpi, *.lpr and *.res.

(Please let me know if you don't know exactly how to do it).

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: Showing a second form using a Menu
« Reply #13 on: March 04, 2015, 09:26:20 pm »
Ooh sorry
I know how to zip, but not how to put it in a message on this forum

Im not sure what OS I use but at school I heard something like Delphi, I dont know if that is what you mean?
« Last Edit: March 04, 2015, 09:28:42 pm by ThomasK1201 »

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Showing a second form using a Menu
« Reply #14 on: March 04, 2015, 09:44:30 pm »
See picture hereafter, to attach a file to one of your answer in this forum (your browser will most probably translate differently the concerned controls).

(Forget my question about OS; it doesn't matter in fact).

 

TinyPortal © 2005-2018