I found a code here that I needed. To only allow write numbers in a text box. But I still wanted more, which does not offer up the "Next" button without write the number in this text box.
Can help me?
procedure NumbersOnly(Sender: TObject; var Key: Char);
var
S: string;
begin
S := ('1234567890'#8);
if Pos(Key, S) = 0 then
Key := #0;
end;
You can setup the next button to be enabled or disabled in the CurPageChanged event when the user reaches the page where resides your edit box. Except that you need to monitor changes of that edit box to enable or disable the next button according to whether there's something entered in that edit box. For this you need to write a handler for the OnChange event. Here is an example:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
var
MyEdit: TNewEdit;
MyPage: TWizardPage;
procedure MyEditChange(Sender: TObject);
begin
{ enable the next button if the edit box is not empty; disable otherwise }
WizardForm.NextButton.Enabled := MyEdit.Text <> '';
end;
procedure MyEditKeyPress(Sender: TObject; var Key: Char);
var
KeyCode: Integer;
begin
{ allow only numbers }
KeyCode := Ord(Key);
if not ((KeyCode = 8) or ((KeyCode >= 48) and (KeyCode <= 57))) then
Key := #0;
end;
procedure InitializeWizard;
begin
MyPage := CreateCustomPage(wpWelcome, 'Caption', 'Description');
MyEdit := TNewEdit.Create(WizardForm);
MyEdit.Parent := MyPage.Surface;
MyEdit.Left := 0;
MyEdit.Top := 0;
MyEdit.Width := 150;
MyEdit.OnChange := #MyEditChange;
MyEdit.OnKeyPress := #MyEditKeyPress;
end;
procedure CurPageChanged(CurPageID: Integer);
begin
{ if the currently turned wizard page is the one with the edit box, enable }
{ the next button if the edit box is not empty; disable otherwise }
if CurPageID = MyPage.ID then
WizardForm.NextButton.Enabled := MyEdit.Text <> '';
end;
There are two flaws in the #TLama's answer:
A user can bypass the check by using Paste command from edit box context menu (and possibly by other kinds of input methods).
To fix this, you can introduce a last resort check in NextButtonClick.
I would also suggest adding the check for an empty input there, instead of MyEditChange, as it allows providing feedback to the user, explaining what is wrong.
On the other hand, Ctrl+C, Ctrl+V and Ctrl+X keys are not working. Particularly a lack of Ctrl+V is not comfortable.
To fix this, allow these control characters in the MyEditKeyPress.
[Code]
var
MyEdit: TNewEdit;
MyPage: TWizardPage;
procedure ValidateMyEdit;
begin
{ enable the next button if the edit box is not empty; disable otherwise }
WizardForm.NextButton.Enabled := (MyEdit.Text <> '');
end;
procedure MyEditChange(Sender: TObject);
begin
ValidateMyEdit;
end;
function IsDigit(C: Char): Boolean;
begin
Result := (C >= '0') and (C <= '9')
end;
procedure MyEditKeyPress(Sender: TObject; var Key: Char);
begin
if not ((Key = #8) or { Tab key }
(Key = #3) or (Key = #22) or (Key = #24) or { Ctrl+C, Ctrl+V, Ctrl+X }
IsDigit(Key)) then
begin
Key := #0;
end;
end;
procedure InitializeWizard;
begin
MyPage := CreateCustomPage(wpWelcome, 'Caption', 'Description');
MyEdit := TNewEdit.Create(WizardForm);
MyEdit.Parent := MyPage.Surface;
MyEdit.Left := 0;
MyEdit.Top := 0;
MyEdit.Width := 150;
MyEdit.OnChange := #MyEditChange;
MyEdit.OnKeyPress := #MyEditKeyPress;
end;
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = MyPage.ID then
begin
ValidateMyEdit;
end;
end;
function NextButtonClick(CurPageID: Integer): Boolean;
var
I: Integer;
begin
Result := True;
if CurPageID = MyPage.ID then
begin
for I := 1 to Length(MyEdit.Text) do
begin
if not IsDigit(MyEdit.Text[I]) then
begin
MsgBox('Only numbers are allowed', mbError, MB_OK);
Result := False;
Break;
end;
end;
end;
end;
This is the way I use based on keys from ASCII code:
procedure justNumbers(Sender: TObject; var Key: Char);
begin
if not ((Key = #8) or (Key = #43) or ((Key >= #48) and (Key <= #57)))
then
begin
Key := #0;
end;
end;
Could work for validate others characters too.
Related
I have created some custom checkboxes in the finished page of Inno Setup.
For example launching an app, opening a text file etc.
I need when the user clicks on the finish button I check those checkboxes and do whatever that is needed. How can I do such a thing in Inno Setup?
Here is the code:
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = wpFinished then
begin
Launch := TNewCheckBox.Create(WizardForm);
Launch.Parent := WizardForm;
Launch.Left := WizardForm.ClientWidth - 350;
Launch.Top := WizardForm.CancelButton.Top;
Launch.Width := 120;
Launch.Height := WizardForm.CancelButton.Height;
Launch.Caption := 'Launch';
end;
end;
In NextButtonClick event handler, test if your checkbox is checked and act accordingly.
function NextButtonClick(CurPageID: Integer): Boolean;
var
ResultCode: Integer;
Path: string;
Message: string;
begin
if CurPageID = wpFinished then
begin
if Launch.Checked then
begin
Path := ExpandConstant('{app}\MyProg.exe');
if ExecAsOriginalUser(Path, '', '', SW_SHOW, ewNoWait, ResultCode) then
begin
Log('Executed MyProg');
end
else
begin
Message := 'Error executing MyProg: ' + SysErrorMessage(ResultCode);
MsgBox(Message, mbError, MB_OK);
end;
end;
end;
Result := True;
end;
Simply check the checkbox state:
if (Launch.Checked = True) then
begin
// checkbox is checked
end
else
begin
// Checkbox is unchecked
end;
The best place is to use function NextButtonClick(CurPageID: Integer): Boolean;
however in that case you need to make your checkbox a global variable (so it is accessible).
I have created a custom welcome page with an image on it but the main panel on the top remains to be displayed. For what I want to achieve see image below:
Here is the code:
[Code]
procedure InitializeWizard;
var
BitmapFileName: string;
BitmapImage: TBitmapImage;
WelcomePage: TWizardPage;
begin
WelcomePage := CreateCustomPage(wpWelcome, '', '');
BitmapFileName := ExpandConstant('{tmp}\DataNova_Logo.bmp');
ExtractTemporaryFile(ExtractFileName(BitmapFileName));
BitmapImage := TBitmapImage.Create(WelcomePage);
BitmapImage.AutoSize := True;
BitmapImage.Bitmap.LoadFromFile(BitmapFileName);
BitmapImage.Cursor := crHand;
BitmapImage.Left := 10;
BitmapImage.Top := 10;
BitmapImage.Parent := WelcomePage.Surface;
end;
How to show the image over the whole page with the main panel hidden ?
You need to hide the Bevel1, MainPanel and the InnerNotebook components when you switch to your welcome page and show them again when you leave it. As the opposite, the image you need to show only when you're showing your welcome page since it covers the whole page area. So the following code will do the trick:
[Code]
var
WelcomePageID: Integer;
BitmapImage: TBitmapImage;
procedure InitializeWizard;
var
WelcomePage: TWizardPage;
begin
WelcomePage := CreateCustomPage(wpWelcome, '', '');
WelcomePageID := WelcomePage.ID;
BitmapImage := TBitmapImage.Create(WizardForm);
BitmapImage.Bitmap.LoadFromFile('C:\Image.bmp');
BitmapImage.Top := 0;
BitmapImage.Left := 0;
BitmapImage.AutoSize := True;
BitmapImage.Cursor := crHand;
BitmapImage.Visible := False;
BitmapImage.Parent := WizardForm.InnerPage;
end;
procedure CurPageChanged(CurPageID: Integer);
begin
BitmapImage.Visible := CurPageID = WelcomePageID;
WizardForm.Bevel1.Visible := CurPageID <> WelcomePageID;
WizardForm.MainPanel.Visible := CurPageID <> WelcomePageID;
WizardForm.InnerNotebook.Visible := CurPageID <> WelcomePageID;
end;
How to fix it? FormStyle:fsStayOnTop. I call Input Query in a thread, but it appears behind a main form or is not visible!
I dynamically create ZipForge in a thread.
procedure StartUpdating.DoPassword;
var
S: String;
begin
if PassSkip then
FSkipFile := True
else if InputQuery('Pas', FFileName, S) then
FPassword := AnsiString(S)
else
begin
PassSkip := True;
FSkipFile := True;
Terminate;
end;
end;
procedure StartUpdating.ZipForgePassword(Sender: TObject; FileName: String;
var NewPassword: AnsiString; var SkipFile: Boolean);
begin
FFileName := FileName;
FPassword := NewPassword;
FSkipFile := SkipFile;
Synchronize(DoPassword);
FileName := FFileName;
NewPassword := FPassword;
SkipFile := FSkipFile;
end;
Even if I call this from a thread it does not help me:
Function TForm1.InQuery(cap1: string; cap2: string):bool;
var s:string;
begin
if InputQuery(cap1,cap2,s) then
begin
ThreadUpdating.MainPas:=s;
result:=true;
end else result:=false;
end;
Edit (2)
The form shown by InputQuery() is a modal form (shown using .ShowModal, and Delphi's VCL is smart enough to make sure no modal form is shown behind a top-most window. Essentially calls are made to DisableTaskWindows and NormalizeAllTopMosts, the interesting one being NormalizeAllTopMosts: this makes sure there are no top-most windows while the modal form is shown. Those calls aren't directly made, the VCL uses a number of tricks to make it happen. The details can be found by reading the code for TCustomForm.ShowModal, Forms.DisableTaskWindows, TApplication.WndProc - specifically the handling of the WM_ENABLE message.
What needs to be known is that there appears to a bug allowing a modal form to be shown behind the Main form of the application, if the main form is top-most. This is happening on both Delphi 2010 and Delphi XE, didn't test with older versions. The test is very simple: Create a new Delphi VCL application, on Form1 set FormStyle=fsStayOnTop, drop a button and in the button's OnClick do this:
procedure TForm2.Button1Click(Sender: TObject);
var s:string;
begin
s := '';
InputQuery('a', 'b', s);
end;
The fix
This should be considered a temporary fix, because clearly the design calls for top-most windows to be "degraded" before showing modal forms. If you know your main form is top-most, you should do something like this before calling ShowModal for anything:
MainForm.FormStyle := fsNormal;
try
YourDialog.ShowModal;
finally MainForm.FormStyle := fsStayOnTop;
end;
In the particular case of InputQuery one can use a function similar to the following one. Please note I included the ability to turn the text editor into a password-editor, based on what the OP requested in an other question:
function CustomInputQuery(const Caption, Prompt: string; const Password:Boolean; var Value:string): Boolean;
var F: TForm;
Ed: TEdit;
Lb: TLabel;
Bt: TButton;
WasStayOnTop:Boolean;
begin
F := TForm.Create(nil);
try
F.Caption := Caption;
Lb := TLabel.Create(F);
Lb.Parent := F;
Lb.Caption := Prompt;
Lb.Left := 8;
Lb.Top := 8;
Ed := TEdit.Create(F);
Ed.Parent := F;
Ed.Left := Lb.Left + Lb.Width + 8;
Ed.Top := 8;
Ed.Width := 150;
Ed.Text := Value;
if Password then
Ed.PasswordChar := '*';
Bt := TButton.Create(F);
Bt.Caption := 'Ok';
Bt.Default := True;
Bt.ModalResult := mrOk;
Bt.Left := 8;
Bt.Top := Ed.Top + Ed.Height + 8;
Bt.Parent := F;
Bt := TButton.Create(F);
Bt.Caption := 'Cancel';
Bt.Cancel := True;
Bt.ModalResult := mrCancel;
Bt.Left := 8 + Bt.Width + 8;
Bt.Top := Ed.Top + Ed.Height + 8;
Bt.Parent := F;
F.Width := F.Width - F.ClientWidth + Ed.Left + Ed.Width + 8;
F.Height := F.Height - F.ClientHeight + Bt.Top + Bt.Height + 8;
F.Position := poDesktopCenter;
WasStayOnTop := Assigned(Application.MainForm) and (Application.MainForm.FormStyle = fsStayOnTop);
if WasStayOnTop then Application.MainForm.FormStyle := fsNormal;
try
if F.ShowModal = mrOk then
begin
Value := Ed.Text;
Result := True;
end
else
Result := False;
finally if WasStayOnTop then Application.MainForm.FormStyle := fsStayOnTop;
end;
finally F.Free;
end;
end;
I am making use of Inno Setup (it's amazing!). I was hoping to customise the installer so that I can accept a string from the user in the form of an input field and maybe add a message to it.
How can I do this? I had a look through the docs, google search and not much came up!
Thanks all for any help
You can use Pascal scripting in InnoSetup to create new pages for the installer. These pages can be integrated into the normal installation flow. This is well documented within the InnoSetup documentation (Google search should also come up with samples). Also the Samples folder within your Program Files\InnoSetup has some code examples.
Some time ago, there was a software called InnoSetup Form designer, which allowed you to visually design the page. The link is still there, but on the page I could not find the download. Maybe if you look around a bit you can find it?
EDIT
This is a sample for a page I made once. This is the code section of the ISS file.[Code]
var
EnableFolderPage: Boolean;
lblBlobFileFolder: TLabel;
lblBlobFileWarning1: TLabel;
lblBlobFileWarning2: TLabel;
tbBlobFileFolder: TEdit;
btnBlobFileFolder: TButton;
function GetBlobFolder(param: String): String;
begin
Result := Trim(tbBlobFileFolder.Text);
end;
{ BlobFileForm_Activate }
procedure BlobFileForm_Activate(Page: TWizardPage);
var
s: string;
begin
s := Trim(tbBlobFileFolder.Text);
if (s = '') then
begin
tbBlobFileFolder.Text := ExpandConstant('{sys}');
end;
end;
{ BlobFileForm_NextButtonClick }
function BlobFileForm_NextButtonClick(Page: TWizardPage): Boolean;
var
s: string;
begin
s := Trim(tbBlobFileFolder.Text);
if (s = '') then
begin
MsgBox(ExpandConstant('{cm:BlobFileForm_NoFolder}'), mbError, MB_OK);
Result := false;
end else
begin
if not DirExists(s) then
begin
MsgBox(ExpandConstant('{cm:BlobFileForm_DirDoesntExist}'), mbError, MB_OK);
Result := false;
end else
begin
Result := True;
end;
end;
end;
procedure btnBlobFileFolder_Click(sender: TObject);
var
directory: string;
begin
if BrowseForFolder('', directory, true) then
begin
tbBlobFileFolder.Text := directory;
end;
end;
{ BlobFileForm_CreatePage }
function BlobFileForm_CreatePage(PreviousPageId: Integer): Integer;
var
Page: TWizardPage;
begin
Page := CreateCustomPage(
PreviousPageId,
ExpandConstant('{cm:BlobFileForm_Caption}'),
ExpandConstant('{cm:BlobFileForm_Description}')
);
{ lblBlobFileFolder }
lblBlobFileFolder := TLabel.Create(Page);
with lblBlobFileFolder do
begin
Parent := Page.Surface;
Caption := ExpandConstant('{cm:BlobFileForm_lblBlobFileFolder_Caption0}');
Left := ScaleX(8);
Top := ScaleY(8);
Width := ScaleX(167);
Height := ScaleY(13);
end;
{ lblBlobFileWarning1 }
lblBlobFileWarning1 := TLabel.Create(Page);
with lblBlobFileWarning1 do
begin
Parent := Page.Surface;
Caption := ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning1_Caption0}');
Left := ScaleX(8);
Top := ScaleY(80);
Width := ScaleX(50);
Height := ScaleY(13);
Font.Color := -16777208;
Font.Height := ScaleY(-11);
Font.Name := 'Tahoma';
Font.Style := [fsBold];
end;
{ lblBlobFileWarning2 }
lblBlobFileWarning2 := TLabel.Create(Page);
with lblBlobFileWarning2 do
begin
Parent := Page.Surface;
Caption :=
ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning2_Caption0}') + #13 +
ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning2_Caption1}') + #13 +
ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning2_Caption2}') + #13 +
ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning2_Caption3}') + #13 +
ExpandConstant('{cm:BlobFileForm_lblBlobFileWarning2_Caption4}');
Left := ScaleX(8);
Top := ScaleY(96);
Width := ScaleX(399);
Height := ScaleY(133);
AutoSize := False;
WordWrap := True;
end;
{ tbBlobFileFolder }
tbBlobFileFolder := TEdit.Create(Page);
with tbBlobFileFolder do
begin
Parent := Page.Surface;
Left := ScaleX(8);
Top := ScaleY(24);
Width := ScaleX(401);
Height := ScaleY(21);
TabOrder := 0;
end;
{ btnBlobFileFolder }
btnBlobFileFolder := TButton.Create(Page);
with btnBlobFileFolder do
begin
Parent := Page.Surface;
Caption := ExpandConstant('{cm:BlobFileForm_btnBlobFileFolder_Caption0}');
Left := ScaleX(320);
Top := ScaleY(48);
Width := ScaleX(91);
Height := ScaleY(23);
TabOrder := 1;
end;
with Page do
begin
OnActivate := #BlobFileForm_Activate;
OnNextButtonClick := #BlobFileForm_NextButtonClick;
end;
with btnBlobFileFolder do
begin
OnClick := #btnBlobFileFolder_Click;
end;
Result := Page.ID;
end;
procedure InitializeWizard();
begin
BlobFileForm_CreatePage(wpSelectDir);
end;
EDIT 2
To write the value the user entered to a registry key, create a new function:
function GetUserEnteredText(param: String): String;
begin
Result := Trim(tbTextBox.Text);
end;
This function simply returns what was entered in the text box. Please note that the function must take a string parameter - even though you ignore it!
In the [Registry] section of your script, declare the key that should be written like that:
Root: HKLM; Subkey: SOFTWARE\MyCompany\MyTool; ValueType: string; ValueName: MyValue; ValueData: {code:GetUserEnteredText}; Flags: createvalueifdoesntexist uninsdeletekeyifempty uninsdeletevalue
This creates a registry value named "MyValue" in HKLM\SOFTWARE\MyCompany\MyTool that contains what the user entered in the text box.
Here is shorter code to add a custom page to Inno Setup installer with an Input Field:
var
CustomQueryPage: TInputQueryWizardPage;
procedure AddCustomQueryPage();
begin
CustomQueryPage := CreateInputQueryPage(
wpWelcome,
'Custom message',
'Custom description',
'Custom instructions');
{ Add items (False means it's not a password edit) }
CustomQueryPage.Add('Custom Field:', False);
end;
procedure InitializeWizard();
begin
AddCustomQueryPage();
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
begin
{ Read custom value }
MsgBox('Custom Value = ' + CustomQueryPage.Values[0], mbInformation, MB_OK);
end;
end;
This is the code section from inno setup.My intention is to make two Checkbox where at a time one is being selected.
But this code return error when first checkbox is clicked.
[code]
procedure CheckBoxOnClick(Sender: TObject);
var
Box2,CheckBox: TNewCheckBox;
begin
if CheckBox.Checked then ///error:"Could not call proc" [sud it be global if then how to or what to change?]
BEGIN
CheckBox.State := cbUnchecked;
Box2.State := cbChecked;
END else
BEGIN
CheckBox.State := cbChecked;
Box2.State := cbUnchecked;
END;
end;
procedure Box2OnClick(Sender: TObject);
var
Box2,CheckBox: TNewCheckBox;
begin
if Box2.Checked then ///error:same
BEGIN
CheckBox.State := cbChecked;
Box2.State := cbUnchecked;
END else
BEGIN
CheckBox.State := cbUnchecked;
Box2.State := cbChecked;
END;
end;
procedure CreateTheWizardPages;
var
Page: TWizardPage;
Box2,CheckBox: TNewCheckBox;
begin
{ TButton and others }
Page := CreateCustomPage(wpWelcome, '', '');
CheckBox := TNewCheckBox.Create(Page);
CheckBox.Top :=ScaleY(8)+ScaleX(50);
CheckBox.Width := Page.SurfaceWidth;
CheckBox.Height := ScaleY(17);
CheckBox.Caption := 'Do this';
CheckBox.Checked := True;
CheckBox.OnClick := #CheckBoxOnClick;
CheckBox.Parent := Page.Surface;
Box2 := TNewCheckBox.Create(Page);
Box2.Top :=ScaleY(8)+ScaleX(70);
Box2.Width := Page.SurfaceWidth;
Box2.Height := ScaleY(17);
Box2.Caption := 'No,Thanks.';
Box2.Checked := False;
Box2.OnClick := #Box2OnClick;
Box2.Parent := Page.Surface;
end;
procedure InitializeWizard();
//var
begin
{ Custom wizard pages }
CreateTheWizardPages;
end;
Please tell me where to change..
The cause of your error is that your are referencing CheckBox which is defined locally in that method. At this point CheckBox has not been created. Which means your calling CheckBox.Checked when CheckBox is undefined (most likely NIL)
Each declaration of CheckBox represents a new variable and does not reference the other variables with the same name. This is due to scoping rules.
One option to solve your problem is to remove the local declarations of Box2, and CheckBox in each method and declare them globally.