Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 12-09-2014, 07:48 AM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default Passing Integers to a dynamically created Object's event

Hi All

There is probably a very simple solution to my problem - apart from giving up programing - but I just can't work it out!

I want to create an array of combo boxes and associated edits that increase and decrease with the spinbox. That part's easy and the code below does this very well. ( I elected to scrub and recreate all the combos each time the spinbox changes - before someone points out it is more efficient to just trim the relevant combo, I'm reworking on the code for this .

My problem arose when I tried to make the associated edit appear when 'Other' is chosen on one of the combos and disappear when it's not. I need to pass the combos index to the event handler so the relevant edit visibility can be changed.

Trying to pass the For Loop value x gave me the error "[dcc32 Error] CommitteeStructure.pas(82): E2010 Incompatible types: 'TObject' and 'Integer'"

Leaving out the x gives [dcc32 Error] CommitteeStructure.pas(82): E2009 Incompatible types: 'Parameter lists differ' so it's obviously looking for something.

Does anyone have any ideas????

Badger

Code:
 
unit CommitteeStructure;
interface
uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  FMX.Edit, FMX.ListBox;
type
  TCommitteeStructureForm = class(TForm)
    Label1: TLabel;
    NumberofMembers: TSpinBox;
    Label2: TLabel;
    title: TComboBox;
    OtherTitle: TEdit;
    ComboBox1: TComboBox;
    procedure NumberofMembersChange(Sender: TObject);
//    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    Procedure OnComboChange(Sender: TObject;Num:Integer);
  public
    { Public declarations }
  end;
var
  CommitteeStructureForm: TCommitteeStructureForm;
implementation
{$R *.fmx}
var
  ComboArray:Array of TComboBox;
  EditArray:Array of TEdit;
  CommitteeNumber:Integer;
 
procedure TCommitteeStructureForm.NumberofMembersChange(Sender: TObject);
var
  x,count:integer;
begin
   //get rid of old objects from screen
   if CommitteeNumber>0 then
     for x:=0 to CommitteeNumber-1 do
     begin
       ComboArray[x].DisposeOf;
       EditArray[x].DisposeOf;
     end;
   SetLength(ComboArray,0);
   SetLength(EditArray,0);
 //Change CommitteeNumberumber
  CommitteeNumber:=StrToInt(NumberofMembers.Text);
  //Layout the combo Boxes etc
  SetLength(ComboArray,CommitteeNumber);
  SetLength(EditArray,CommitteeNumber);
  for x:=0 to CommitteeNumber-1 do
  begin
    //Create ComboBox
    ComboArray[x]:=TCombobox.Create(Self);
    ComboArray[x].Parent:=Self;
    //Create Edit
    EditArray[x]:=TEdit.Create(Self);
    EditArray[x].Parent:=Self;
    //Add properties
    With ComboArray[x] do
    begin
      Position.x:=50;
      position.y:=100+x*(height+5);
      //add items
       items.Add(title.Items[title.ItemIndex]);
       items.Add('Vice '+title.Items[title.ItemIndex]);
       items.Add('vice '+title.Items[title.ItemIndex]+' 1');
       items.Add('Vice '+title.Items[title.ItemIndex]+' 2');
       items.Add('Vice '+title.Items[title.ItemIndex]+' 3');
       ...............other positions.......
       items.Add('Other');
      //Add events
      OnChange:= OnComboChange{(x)};
    end;
    With EditArray[x] do
    begin
      Position.x:=50+ComboArray[x].Width+10;
      position.y:=100+x*(height+5);
 
    end;
  end;
end;
procedure TCommitteeStructureForm.OnComboChange(Sender:TObject;Num:Integer);
begin
  If ComboArray[Num].Items[ItemIndex]='Other' then
  begin
    EditArray[Num].Visible:=true;
  end
  else
  begin
    EditArray[Num].Visible:=False;
  end;
end;
end.
  #2  
Old 12-09-2014, 11:03 AM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

Try something like this(see bold, red):
Code:
unit CommitteeStructure;
interface
uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  FMX.Edit, FMX.ListBox;
type
  TCommitteeStructureForm = class(TForm)
    Label1: TLabel;
    NumberofMembers: TSpinBox;
    Label2: TLabel;
    title: TComboBox;
    OtherTitle: TEdit;
    ComboBox1: TComboBox;
    procedure NumberofMembersChange(Sender: TObject);
//    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    //Procedure OnComboChange(Sender: TObject;Num:Integer);
     Procedure OnComboChange(Sender: TObject);
  public
    { Public declarations }
  end;
var
  CommitteeStructureForm: TCommitteeStructureForm;
implementation
{$R *.fmx}
var
  ComboArray:Array of TComboBox;
  EditArray:Array of TEdit;
  CommitteeNumber:Integer;

procedure TCommitteeStructureForm.NumberofMembersChange(Sender: TObject);
var
  x,count:integer;
begin
   //get rid of old objects from screen
   if CommitteeNumber>0 then
     for x:=0 to CommitteeNumber-1 do
     begin
       ComboArray[x].DisposeOf;
       EditArray[x].DisposeOf;
     end;
   SetLength(ComboArray,0);
   SetLength(EditArray,0);
 //Change CommitteeNumberumber
  CommitteeNumber:=StrToInt(NumberofMembers.Text);
  //Layout the combo Boxes etc
  SetLength(ComboArray,CommitteeNumber);
  SetLength(EditArray,CommitteeNumber);
  for x:=0 to CommitteeNumber-1 do
  begin
    //Create ComboBox
    ComboArray[x]:=TCombobox.Create(Self);
    ComboArray[x].Parent:=Self;
    ComboArray[x].Tag = x;
    //Create Edit
    EditArray[x]:=TEdit.Create(Self);
    EditArray[x].Parent:=Self;
    //Add properties
    With ComboArray[x] do
    begin
      Position.x:=50;
      position.y:=100+x*(height+5);
      //add items
       items.Add(title.Items[title.ItemIndex]);
       items.Add('Vice '+title.Items[title.ItemIndex]);
       items.Add('vice '+title.Items[title.ItemIndex]+' 1');
       items.Add('Vice '+title.Items[title.ItemIndex]+' 2');
       items.Add('Vice '+title.Items[title.ItemIndex]+' 3');
       ...............other positions.......
       items.Add('Other');
      //Add events
      OnChange:= OnComboChange;
    end;
    With EditArray[x] do
    begin
      Position.x:=50+ComboArray[x].Width+10;
      position.y:=100+x*(height+5);

    end;
  end;
end;

//procedure TCommitteeStructureForm.OnComboChange(Sender:TObject;Num:Integer);
procedure TCommitteeStructureForm.OnComboChange(Sender:TObject);
var
  Num: Integer;
begin
  Num := (Sender as TComboBox).Tag;
  If ComboArray[Num].Items[ItemIndex]='Other' then
  begin
    EditArray[Num].Visible:=true;
  end
  else
  begin
    EditArray[Num].Visible:=False;
  end;
end;
end.
  #3  
Old 12-09-2014, 11:59 AM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default

Thanks kolbasz

Worked perfectly. Quite logical when it's written out in front of you. I'd never have got it by myself in a million years!

Badger
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 01:31 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.