• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

writing a simple procedure in delphi?

Hi again,

i know this is simple, but i can't do it. :D


when i write a procedure and do not include any other object invoke, there is no problem like this

procedure dothis(message:string);
begin
showMessage(message);
end;

but here i want to change a number of objects states. but i have this error:
Pascal Error] Unit1.pas(1): Unable to invoke Code Completion due to errors in source code

would u help me do this correctly please ?

procedure enableAll(ok:boolean);
begin
if ok then
begin
id.readonly:= true;
ip.readonly:= true;
name.readonly:= true;
address.readonly:= true;
end
else
begin
id.readonly:= false;
ip.readonly:= false;
name.readonly:= false;
address.readonly:= false;
end;

where id,ip,name and address are of TDBEdit.
0
moayyad
Asked:
moayyad
3 Solutions
 
Scay7Commented:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
  procedure enableAll(ok:boolean);  //add this line in to register the procedure
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure Tform1.enableAll(ok:boolean); //add the Tform1. or what ever the name of your form is called
begin
  if ok = true then //ok is boolean so you have to say = true/false in this case well use false
  begin
  id.readonly:= true;
  ip.readonly:= true;
  name.readonly:= true;
  address.readonly:= true;
  end else
        begin
        id.readonly:= false;
        ip.readonly:= false;
        name.readonly:= false;
        address.readonly:= false;
        end; //with any begin that is made there must always be an end; i call it the "BE" rule
end;

end.
0
 
LMuadDIbCommented:
if the procedure is not apart of object then you have to reference it

like Scay7 did, he added your procedure to TForm1 object so you can refernce the TDBEdits from the MainForm otherwsie you will have to pass the TDBEdits thru the procedure or the form. But best way would do it like Scay7 says and add your procedure to TForm1 instead of creating a global procedure.

procedure enableAll(ok:boolean);
begin
      with Form1 do begin
      if ok then
      begin
        id.readonly:= true;
        ip.readonly:= true;
        name.readonly:= true;
        address.readonly:= true;
      end
      else
      begin
        id.readonly:= false;
        ip.readonly:= false;
        name.readonly:= false;
        address.readonly:= false;
      end;
      end;
end;

//--
you can use the boolean as just "ok", or "not ok" to equal true or false, no need for "ok=true" or "ok = false"
0
 
TheRealLokiSenior DeveloperCommented:
just as an aside...
if you type
procedure enableAll(ok:boolean);

in the form's public/private block, like here :-
type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure enableAll(ok:boolean);   // <----- placed here.
  end;

then you place the cursor on the "procedure enableAll(ok:boolean);" line and press CTRL-SHIFT-C
it will make the procedure for you. Just a shortcut in case you don't know it

Also, (as a suggestion), a simpler procedure for doing what you want would be

procedure TForm1.EnableAll(ok:boolean);
begin
  id.readonly:= ok;  // this simplifies things. if "ok" is true, readonly becomes true, if "ok" is false, readonly becomes false
  ip.readonly:= ok;
  name.readonly:= ok;
  address.readonly:= ok;
end;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now