Recent

Author Topic: Accessing form controls in my procedure  (Read 4885 times)

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Accessing form controls in my procedure
« on: July 14, 2011, 09:28:07 pm »
Probably a silly question but:

I have a simple form 'Form1' with an Edit box 'Edit1'. I've written a procedure to manipulate the contents of the box but I have to refer to Form1.Edit1.Text to access the text in my procedure.

I get the feeling that if I define my procedure in the correct place I'll be able to just refer to Edit1.Text rather than use Form1.Edit1.Text or 'With Form1 do...'.

It's been a long time since I used Delphi (v2.0 when it was new!), can someone put me right on this please?

TIA

mrguzgog


typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Accessing form controls in my procedure
« Reply #1 on: July 14, 2011, 09:37:54 pm »
Are you sure your procedure is part of TForm1 class?

Dick, from the internet

  • Full Member
  • ***
  • Posts: 198
Re: Accessing form controls in my procedure
« Reply #2 on: July 14, 2011, 09:42:25 pm »
Yes, you need to declare your procedure within the form class declaration.  You can make your procedure private, public or published depending on how you need to access it - there are some excellent resources within this site for more information.

Code: [Select]
type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
    procedure myProcedure;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

procedure TForm1.Button1Click(Sender: TObject);
begin
  myProcedure;
end;

procedure TForm1.myProcedure;
begin
  Edit1.Text := 'Hello World';
end;

{$R *.lfm}

end.


geno.

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: Accessing form controls in my procedure
« Reply #3 on: July 14, 2011, 09:49:13 pm »
Thanks both.

Well I did try putting the declaration beforehand but if I do this:

  private
    { private declarations }
    procedure ShowStack;
  public
    { public declarations }

I get :
unit1.pas(41,15) Error: Forward declaration not solved "TForm1.ShowStack;"

Thanks again

mrguzgog

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Accessing form controls in my procedure
« Reply #4 on: July 14, 2011, 09:51:33 pm »
The compiler expects code on format:

Code: [Select]
procedure TForm1.ShowStack;
begin
{...}
end;

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: Accessing form controls in my procedure
« Reply #5 on: July 14, 2011, 10:07:03 pm »
Ah, ok thank you  :-[ ... seems logical now I look at it!

mrguzgog

 

TinyPortal © 2005-2018