User Tools

Site Tools


checkparametersbeforecreateform

Differences

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

Link to this comparison view

Next revision
Previous revision
checkparametersbeforecreateform [2025/04/18 03:14] – created jwancheckparametersbeforecreateform [2025/10/10 10:15] (current) – external edit 127.0.0.1
Line 69: Line 69:
  
 How It Works: How It Works:
-Parameter Check in Main Block:- Before calling Application.CreateForm, the program checks if any parameters are provided using ParamCount. +  * Parameter Check in Main Block:- Before calling Application.CreateForm, the program checks if any parameters are provided using ParamCount. 
-If /scan is passed as a parameter (case-insensitive), it shows a message and terminates the application using Halt. +  If /scan is passed as a parameter (case-insensitive), it shows a message and terminates the application using Halt. 
-If no parameters or a different parameter is passed, it proceeds to create and display the form. +  If no parameters or a different parameter is passed, it proceeds to create and display the form. 
- +  Form Creation:- The form's constructor (Create) is overridden, but in this case, it doesn’t perform parameter checking (since it’s already handled in the main program block). 
-Form Creation:- The form's constructor (Create) is overridden, but in this case, it doesn’t perform parameter checking (since it’s already handled in the main program block). +  Graceful Exit with Message:- When /scan is detected, a message box informs the user, and the program exits without showing the form.
- +
-Graceful Exit with Message:- When /scan is detected, a message box informs the user, and the program exits without showing the form.+
  
  
Line 83: 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.1744946069.txt.gz · Last modified: (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki