I am trying to pass the selection made by the user in the pop up menu in unit 2 back to the calling procedure in unit test
if i remove the line
ShowMessage('Calc .. retval = ' + IntToStr(Form2.retval));
the wrong value is returned - appears to be the previous value.
on the first pick the user picks option 1 then 0 is returned
second time user pics option 2 then 1 is returned etc
any suggestions on how to fix this or suggest a better way of doing this would be appreciated
Thanks
GregB
unit Test;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
Unit2;
type
{ TForm1 }
TForm1 = class(TForm)
MainMenu1: TMainMenu;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
PopupMenu1: TPopupMenu;
procedure FormCreate(Sender: TObject);
procedure MenuItem1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
x : integer;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.MenuItem1Click(Sender: TObject);
begin
x := 0;
Calc(x);
ShowMessage('Get Result .. ' + IntToStr(x));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
PopUpMenu;
end;
end.
unit Unit2;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus;
type
{ TForm2 }
TForm2 = class(TForm)
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
PopupMenu1: TPopupMenu;
//procedure FormCreate(Sender: TObject);
procedure MenuItem1Click(Sender: TObject);
procedure MenuItem2Click(Sender: TObject);
procedure PopupMenu1Popup(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
var retval : integer;
end;
var
Form2: TForm2;
//retval : integer;
Procedure Calc(var v : integer);
implementation
{$R *.lfm}
Procedure Calc(var v : integer);
begin
Form2.PopupMenu := Form2.PopUpMenu1; { links popupmenu1 to the form2 }
Form2.PopupMenu1.Items[0].Caption := 'option 1';
Form2.PopupMenu1.Items[1].Caption := 'option 2';
Form2.PopUpMenu1.PopUp; { displays the pop up menu }
[color=red][b] ShowMessage('Calc .. retval = ' + IntToStr(Form2.retval)); [/b][/color]{ if i remove this it does not return the correct value}
v := Form2.retval;
end;
{ TForm2 }
//procedure TForm2.FormCreate(Sender: TObject);
//begin
// //Form2.Show;
//end;
procedure TForm2.MenuItem1Click(Sender: TObject);
begin
//ShowMessage('MenuItem1Click .. retval = ' + IntToStr(retval));
retval := 1;
//ShowMessage('MenuItem1Click .. retval = ' + IntToStr(retval));
end;
procedure TForm2.MenuItem2Click(Sender: TObject);
begin
//ShowMessage('MenuItem2Click .. retval = ' + IntToStr(retval));
retval := 2;
//ShowMessage('MenuItem2Click .. retval = ' + IntToStr(retval));
end;
procedure TForm2.PopupMenu1Popup(Sender: TObject);
begin
//ShowMessage('PopupMenu1Popup');
PopupMenu;
end;
end.