User Tools

Site Tools


checkparametersbeforecreateform

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
checkparametersbeforecreateform [2025/04/18 03:15] jwancheckparametersbeforecreateform [2025/10/10 10:15] (current) – external edit 127.0.0.1
Line 81: Line 81:
  
  
 +Additional option is what if no form will be created and just display a message in console, just add the $APPTYPE like this:
  
 +<code>
 +{$APPTYPE CONSOLE}
 +program NetworkDownAlarm;
 +
 +uses
 +  Forms,
 +  mainnetdownalarm in 'mainnetdownalarm.pas' {formmain_networkdown};
 +
 +{$R *.res}
 +
 +begin
 +  Application.Initialize;
 +
 +  if ParamCount > 0 then begin
 +    if ParamStr(1)='/scan' then
 +    begin
 +      // If the parameter is "/scan", do not create the form and exit the program
 +      writeln('Scan mode executed. No form will be created.');
 +      Halt; // Terminate the program
 +    end;
 +  end;
 +
 +  Application.CreateForm(Tformmain_networkdown, formmain_networkdown);
 +  Application.Run;
 +end.
 +</code>
 +
 +
 +Another is to create different form base on the parameter:
 +
 +<code>
 +program Project1;
 +
 +uses
 +  Forms, Dialogs, SysUtils, Unit1 in 'Unit1.pas', Unit2 in 'Unit2.pas';
 +
 +{$R *.RES}
 +
 +const
 +  ProgramVersion = '1.0.0'; // Define the program version
 +
 +begin
 +  Application.Initialize;
 +
 +  // Check for command-line parameters
 +  if ParamCount > 0 then
 +  begin
 +    if SameText(ParamStr(1), '/scan') then
 +    begin
 +      // If the parameter is "/scan", create and display TScanForm (Unit2)
 +      Application.CreateForm(TScanForm, ScanForm);
 +      Application.Run;
 +      Exit;
 +    end
 +    else if SameText(ParamStr(1), '/help') then
 +    begin
 +      // If the parameter is "/help", show help text and exit
 +      MessageBox(0, PChar('Program Version: ' + ProgramVersion + sLineBreak +
 +                          'Usage:' + sLineBreak +
 +                          '/scan - Run scan mode (no default form).' + sLineBreak +
 +                          '/help - Display this help message.'), 
 +                          'Help', MB_ICONINFORMATION or MB_OK);
 +      Halt; // Terminate the program
 +    end;
 +  end;
 +
 +  // If no parameters or unrecognized parameter, create and display TForm1
 +  Application.CreateForm(TForm1, Form1);
 +  Application.Run;
 +end.
 +</code>
 +
 +NOTE: just make sure each of the form will have:
 +
 +<code>
 +
 +public
 +    { Public declarations }
 +    constructor Create(AOwner: TComponent); override;
 +    
 +constructor Tformmain.Create(AOwner: TComponent);
 +begin
 +   inherited Create(AOwner); // Call the inherited constructor
 +end;
 +</code>
checkparametersbeforecreateform.1744946118.txt.gz · Last modified: (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki