Inno Setup: Access to custom control from OnClick event of another control - event-handling

I have next code for Inno Setup:
procedure CheckBoxClick(Sender: TObject);
begin
{ How to make BrowseButton visible from here? }
end;
procedure CreateTheWizardPage;
var
Page: TWizardPage;
BrowseButton, FormButton: TNewButton;
CheckBox: TNewCheckBox;
Memo: TNewMemo;
begin
Page := PageFromID(wpReady);
BrowseButton := TNewButton.Create(Page);
CheckBox := TNewCheckBox.Create(Page);
CheckBox.OnClick := #CheckBoxClick;
end;
I'm wondering how can I access custom controllers on the wizard page from handler procedure for one of them?

You have to make the BrowseButton variable global and define it before the event handler:
var
BrowseButton: TButton;
procedure CheckBoxClick(Sender: TObject);
begin
{ Now you can use the BrowseButton here }
end;
procedure CreateTheWizardPage;
var
Page: TWizardPage;
FormButton: TNewButton;
CheckBox: TNewCheckBox;
Memo: TNewMemo;
begin
Page := PageFromID(wpReady);
BrowseButton := TNewButton.Create(Page);
CheckBox := TNewCheckBox.Create(Page);
CheckBox.OnClick := #CheckBoxClick;
end;
Related question: Reading values from custom Inno Setup wizard pages without using global variables

Related

Inno Setup get TObject type/class in event handler

I have an event procedure that checks the OnKeyUp key press for two objects/controls (TNewEdit and TNewComboBox). Both objects need to be completed before a TNewButton gets enabled.
However, I cannot find a way to know how to get the type of the Sender: TObject, if that is TNewEdit or TNewComboBox.
Anyone can help?
You should not need to know the type/class for anything.
Such a need is a sign of a bad design.
If the handling of the event is different for each type/class, create a separate handler for each.
If part of the handling is common, call the common handler from the specific handlers.
var
Edit: TNewEdit;
ComboBox: TNewComboBox;
procedure CommonKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
Log('Common handling');
end;
procedure EditKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
Log('Edit key up');
CommonKeyUp(Sender, Key, Shift);
end;
procedure ComboBoxKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
Log('Combo box key up');
CommonKeyUp(Sender, Key, Shift);
end;
procedure InitializeWizard();
begin
{ ... }
Edit.OnKeyUp := #EditKeyUp;
Combobox.OnKeyUp := #ComboBoxKeyUp;
end;
Though as you actually have two controls, you probably want to distinguish, what control raised the event.
That's, what the Sender argument is for. The following code shows how to use it. But again, in general, this is not the right way to go.
var
Edit: TNewEdit;
ComboBox: TNewComboBox;
procedure ControlKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Sender = Edit then
begin
Log('Edit key up');
end
else
if Sender = ComboBox then
begin
Log('Combo box key up');
end
else
begin
Log('Other key up');
end;
end;
procedure InitializeWizard();
begin
{ ... }
Edit.OnKeyUp := #ControlKeyUp;
Combobox.OnKeyUp := #ControlKeyUp;
end;
Though still I do not understand, what you need this for.
You have to check both controls every time, so why do you need to know, what control, was the one that changed?
Also, to detect a change, do not use OnKeyUp, use OnChange. That way you capture all changes (key press, drag&drop, copy&paste, anything).
var
Edit: TNewEdit;
ComboBox: TNewComboBox;
Button: TNewButton;
procedure ControlChange(Sender: TObject);
begin
Button.Enabled := (Edit.Text <> '') and (ComboBox.Text <> '');
end;
procedure InitializeWizard();
begin
{ ... }
Edit.OnChange := #ControlChange;
Combobox.OnChange := #ControlChange;
end;

In Delphi, how can I modify cells in a TStringGrid using a DLL procedure?

In Embarcadero Delphi v10.1 I have both a DLL library with a record, and a VCL application containing a TStringGrid and a TEdit. The idea is to take the shortstring entered into the TEdit; save it to the record in the DLL and then use the data stored in the record to fill in one of the cells in the TStringGrid.
My problem is that after saving the shortstring to the record I can't seem to find a way to access the TStringGrid while inside the DLL procedure. So far I have tried using both classes and pointers to access the TStringGrid in the DLL but neither has worked:
type
pstringgrid = ^TStringGrid;
//or
type
pstringgrid = ^stringgrid1;
//or
type
istringgrid = class(TStringGrid);
I have even tried to import the TStringGrid into the procedure which is supposed to enter the shortstring from the record into the TStringGrid:
procedure AddElement (var grid : stringgrid1); stdcall;
//or
type
pstringgrid = ^TStringGrid;
procedure AddElement (var grid : ^pstringgrid); stdcall;
So far nothing has worked and all I am getting is the "undecleared identifier" error message from the debugger; please help! How can I access and edit a TStringGrid while in a DLL procedure?
Edit:
Here is the relevant code, sorry for the foreign variable names.
The DLL:
library BibliotekaDLL;
uses
System.SysUtils,
System.Classes;
type
StringGrid1 = class(TStringGrid);
plist = ^game;
game = record
nazwa: shortstring;
wydawca: shortstring;
rokwyd: integer;
gatunek1: shortstring;
gatunek2: shortstring;
pointer: plist;
end;
var
BazaDanych : file of game;
first, current: plist;
[...]
procedure WyswietlListe; stdcall;
var
row : integer;
begin
AssignFile(BazaDanych, 'c:\Baza_Danych_Gier.dat');
if not FileExists('c:\Baza_Danych_Gier.dat') then
ShowMessage ('Baza Danych Nie Instnieje' +E.Message)
else
begin
Reset(BazaDanych);
Read(BazaDanych, first);
Close(BazaDanych);
current := first;
row := 1;
while current^.pointer <> nil do
begin
current := first;
StringGrid1.Cells[0,row] := current^.nazwa;
StringGrid1.Cells[1,row] := current^.wydawca;
StringGrid1.Cells[2,row] := current^.rokwyd;
StringGrid1.Cells[3,row] := current^.gatunek1;
StringGrid1.Cells[4,row] := current^.gatunek2;
current := current^.pointer;
row = row +1;
StringGrid1.RowCount := row;
end;
if current^.pointer = nil do
begin
StringGrid1.Cells[0,row] := current^.nazwa;
StringGrid1.Cells[1,row] := current^.wydawca;
StringGrid1.Cells[2,row] := current^.rokwyd;
StringGrid1.Cells[3,row] := current^.gatunek1;
StringGrid1.Cells[4,row] := current^.gatunek2;
end;
end;
end;
[...]
And the VCL application code:
[...]
type
TForm1 = class(TForm)
Button2: TButton;
StringGrid1: TStringGrid;
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
[...]
procedure TForm1.Button2Click(Sender: TObject);
var
Handle : THandle;
WyswietlListe : procedure;
begin
Handle := LoadLibrary('BibliotekaDLL.dll');
try
#WyswietlListe:= GetProcAddress(Handle, 'WyswietlListe');
if #WyswietlListe = nil then raise Exception.Create('Nie Można Znaleźć Procedury w Bibliotece!');
WyswietlListe;
finally
FreeLibrary(Handle);
end;
end;
[...]
My problem is that after saving the shortstring to the record I can't seem to find a way to access the TStringGrid while inside the DLL procedure.
Don't do that. It is bad design.
For one thing, it is not safe to access objects across the DLL boundary unless both app and DLL are compiled with Runtime Packages enabled so they share a single instance of the RTL and memory manager.
It is best if the DLL has no knowledge of your UI at all. If the DLL needs to communicate info to the app, the DLL should define a callback event that the app can assign a handler for, and then the DLL can call that event when needed. Let the app decide how to manage its own UI.
Also, your game record has a pointer member, but pointers cannot be persisted in files. You need to remove that member.
Try something more like this:
library BibliotekaDLL;
uses
System.SysUtils,
System.Classes,
Vcl.Dialogs;
type
game = packed record
nazwa: shortstring;
wydawca: shortstring;
rokwyd: integer;
gatunek1: shortstring;
gatunek2: shortstring;
end;
gameCallback = procedure(var g: game; userData: Pointer); stdcall;
procedure WyswietlListe(callback: gameCallback; userData: Pointer); stdcall;
var
BazaDanych : File of game;
current: game;
begin
AssignFile(BazaDanych, 'c:\Baza_Danych_Gier.dat');
Reset(BazaDanych);
if IOResult <> 0 then
ShowMessage ('Baza Danych Nie Instnieje')
else
try
repeat
Read(BazaDanych, current);
if IOResult <> 0 then Break;
if Assigned(callback) then callback(current, userData);
until False;
finally
Close(BazaDanych);
end;
end;
exports
WyswietlListe;
end.
interface
type
TForm1 = class(TForm)
Button2: TButton;
StringGrid1: TStringGrid;
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
type
game = packed record
nazwa: shortstring;
wydawca: shortstring;
rokwyd: integer;
gatunek1: shortstring;
gatunek2: shortstring;
end;
gameCallback = procedure(var g: game; userData: Pointer); stdcall;
pmyCallbackInfo = ^myCallbackInfo;
myCallbackInfo = record
Grid: TStringGrid;
FirstTime: Boolean;
end;
procedure myCallback(var g: game; userData: Pointer); stdcall;
var
row: Integer;
begin
Grid := pmyCallbackInfo(userData).Grid;
// add a new row only if the initial non-fixed row is already filled...
if pmyCallbackInfo(userData).FirstTime then
pmyCallbackInfo(userData).FirstTime := False
else
Grid.RowCount := Grid.RowCount + 1;
row := Grid.RowCount - 1;
Grid.Cells[0, row] := g.nazwa;
Grid.Cells[1, row] := g.wydawca;
Grid.Cells[2, row] := IntToStr(g.rokwyd);
Grid.Cells[3, row] := g.gatunek1;
Grid.Cells[4, row] := g.gatunek2;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
DLLHandle : THandle;
WyswietlListe : procedure(callback: gameCallback; userData: Pointer); stdcall;
info: myCallbackInfo;
begin
// clear the TStringGrid. However, it has an odd quirk
// that it requires at least 1 non-fixed row at all times...
//
StringGrid1.RowCount := StringGrid1.FixedRows + 1;
StringGrid1.Rows[StringGrid1.RowCount - 1].Clear;
DLLHandle := LoadLibrary('BibliotekaDLL.dll');
if DLLHandle = 0 then raise Exception.Create(...);
try
#WyswietlListe := GetProcAddress(DLLHandle, 'WyswietlListe');
if not Assigned(WyswietlListe) then raise Exception.Create('Nie Można Znaleźć Procedury w Bibliotece!');
info.Grid := StringGrid1;
info.FirstTime := True;
WyswietlListe(#myCallback, #info);
finally
FreeLibrary(DLLHandle);
end;
end;

I am trying to open a form and close the form behind it in Delphi 10 [duplicate]

Application has a Login form and a Main form.
Applications DPR file has code to load Login form first, and when Login form returns successful login, then Main form is created and loaded.
When user logs out via a menu command in Main form, it should close the Main form and load the Login form.
Application exits only when user selects Exit in the Main form (or when user Cancels out of the Login form).
Using code in the application's DPR file, is it possible to code this?
Here is the code that presently exists:
program H;
uses
Forms,
SysUtils,
Registry,
MidasLib,
Dialogs,
Controls,
uDatamod in 'uDatamod.pas' {datamod: TDataModule} ,
uMain in 'uMain.pas' {fMain} ,
uMtlUpd in 'uMtlUpd.pas' {fMtlUpd} ,
uReportPrv in 'uReportPrv.pas' {fReportPrv} ,
uCamera in 'uCamera.pas' {fCamera} ,
uConfig in 'uConfig.pas' {fConfig} ,
uFuncs in 'uFuncs.pas',
uLogin in 'uLogin.pas' {fLogin} ,
uAdmin in 'uAdmin.pas' {fAdmin};
// MidasLib is required.
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.Title := 'HTech';
if ((ParamCount = 1) and (UpperCase(ParamStr(1)) = '/CONFIG')) or
(getHServerHostName = EmptyStr) then
begin
Application.CreateForm(TfConfig, fConfig);
Application.Run;
end
else
begin
if not testHServerConnection then
begin
ShowMessage('Error: Could not connect to HServer');
Exit;
end;
Application.CreateForm(Tdatamod, Datamod);
while not TerminateApplicationFlag do
begin
fLogin := TfLogin.Create(Application);
try
if fLogin.ShowModal = mrOk then
begin
LoggedInEmployeeID := fLogin.FEmployeeID;
LoggedInEmployeeNm := fLogin.edtFirstName.Text + ' ' +
fLogin.edtLastName.Text;
AdminLogin := fLogin.FAdminUser;
FinanceLogin := fLogin.FFinanceUser;
end
else
begin
FreeAndNil(fLogin);
FreeAndNil(Datamod);
Exit;
end;
finally
// FreeAndNil(fLogin);
end;
if AdminLogin then
Application.CreateForm(TfAdmin, fAdmin)
else
begin
FreeAndNil(fLogin);
if not Assigned(fMain) then
Application.CreateForm(TfMain, fMain);
fMain.FHServerHost := getHServerHostName;
end;
Application.Run;
end;
end;
end.
The problem with the above code is that after one iteration (after user performs Logout in Main form), the application exits (control is returned to the operating system) because " fLogin.ShowModal " exits without showing the Login form.
Here is the code from the Main form:
Procedure LogoutProcedure;
begin
TerminateApplicationFlag := False;
Close;
end;
Procedure ExitProcedure;
begin
TerminateApplicationFlag := True;
Close;
end;
I'm stuck with this and would appreciate any advice or corrections in getting it to work.
Thank you in advance.
Regards,
Steve Faleiro
Maybe this very simple solution is sufficient:
The project file:
program Project1;
uses
Forms,
FMain in 'FMain.pas' {MainForm},
FLogin in 'FLogin.pas' {LoginForm};
{$R *.res}
var
MainForm: TMainForm;
begin
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Login;
Application.Run;
end.
The main form:
unit FMain;
interface
uses
Classes, Controls, Forms, StdCtrls, FLogin;
type
TMainForm = class(TForm)
LogoutButton: TButton;
procedure LogoutButtonClick(Sender: TObject);
end;
implementation
{$R *.dfm}
procedure TMainForm.LogoutButtonClick(Sender: TObject);
begin
Login;
end;
end.
And the login form:
unit FLogin;
interface
uses
Classes, Controls, Forms, StdCtrls;
type
TLoginForm = class(TForm)
LoginButton: TButton;
CancelButton: TButton;
procedure FormCreate(Sender: TObject);
end;
procedure Login;
implementation
{$R *.dfm}
procedure Login;
begin
with TLoginForm.Create(nil) do
try
Application.MainForm.Hide;
if ShowModal = mrOK then
Application.MainForm.Show
else
Application.Terminate;
finally
Free;
end;
end;
procedure TLoginForm.FormCreate(Sender: TObject);
begin
LoginButton.ModalResult := mrOK;
CancelButton.ModalResult := mrCancel;
end;
end.
Now, this answer works here, quite well with Delphi 7, but I suspect problems with more recent versions were Application.MainFormOnTaskbar and Application.ShowMainForm are True by default. When so, try to set them to False.

delphi Activex and parented forms error

i try to fix up my activex project and i had errors , i have 2 forms in my activex project first form hold tmemo and button to call second form as parented form every thing works fine till now but i cannot set any record from second form to first form control always get access violation so i decided to show result before set tmemo.text control in the first form and actually result is showing but but cannot be set into the first form here is my project code
unit main1;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ActiveX, AxCtrls, embed_TLB, StdVcl, Vcl.StdCtrls;
type
Tform1 = class(TForm, Iform1)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
procedure showEmo(L,T:Integer);
end;
var
Form1 : Tform1;
implementation
uses ComObj, ComServ, main2;
{$R *.DFM}
{ Tform1 }
procedure Tform1.Button1Click(Sender: TObject);
var
Rect: TRect;
begin
GetWindowRect(Self.button1.Handle, Rect);
showEmo(Rect.Left + 70,(Rect.Top - 290));
end;
procedure Tform1.FormCreate(Sender: TObject);
begin
Form2 := TForm2.Createparented(0);
end;
procedure TForm1.showEmo(L,T:Integer);
var
Rect: TRect;
begin
try
GetWindowRect(button1.Handle, Rect);
begin
Form2.FormStyle := fsStayOnTop;
end;
Form2.Left := L;//Rect.Left;
Form2.top := T;//Rect.Top - emo.Height;
finally
Form2.Visible := not (Form2.visible);
end;
end;
initialization
TActiveFormFactory.Create(
ComServer,
TActiveFormControl,
Tform1,
Class_form1,
0,
'',
OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
tmApartment);
end.
Form 2
unit main2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, SHDocVw_EWB, EwbCore,
EmbeddedWB, MSHTML_EWB, Vcl.StdCtrls;
type
TForm2 = class(TForm)
ewbpage: TEmbeddedWB;
load: TMemo;
procedure FormCreate(Sender: TObject);
procedure ewbpageBeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
var URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
var Cancel: WordBool);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses main1;
{$R *.dfm}
procedure TForm2.ewbpageBeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
var URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
var Cancel: WordBool);
var
MousePos: TPoint;
HtmlElement: IHTMLElement;
iHTMLDoc: IHtmlDocument2;
begin
if Pos('#sm',URL)>0 then
begin
if Supports(ewbpage.Document, IHtmlDocument2, iHTMLDoc) then
begin
if GetCursorPos(MousePos) then
begin
MousePos := ewbpage.ScreenToClient(MousePos);
HtmlElement := iHTMLDoc.ElementFromPoint(MousePos.X, MousePos.Y);
if Assigned(HtmlElement) then
showmessage(HtmlElement.getAttribute('id', 0));
form1.Memo1.Text := HtmlElement.getAttribute('id', 0);
Cancel := True;
Self.Close;
end;
end;
end;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
ewbpage.LoadFromStrings(load.Lines);
end;
end.
and the question is why i get this error
Access violation at address 07C734FC in module 'EMBEDA~1.OCX'. Read of
address 000003B4.
at this line
form1.Memo1.Text := HtmlElement.getAttribute('id', 0);
why i cannot set result from second form to first form ? what i did wrong here is the full project for better understand
http://www.mediafire.com/download/zn7hzoxze2390a3/embeddedactivex.zip
You will see this issue once you start to format your code properly
procedure TForm2.ewbpageBeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
var URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
var Cancel: WordBool);
var
MousePos: TPoint;
HtmlElement: IHTMLElement;
iHTMLDoc: IHtmlDocument2;
begin
if Pos('#sm',URL)>0 then
begin
if Supports(ewbpage.Document, IHtmlDocument2, iHTMLDoc) then
begin
if GetCursorPos(MousePos) then
begin
MousePos := ewbpage.ScreenToClient(MousePos);
HtmlElement := iHTMLDoc.ElementFromPoint(MousePos.X, MousePos.Y);
// if we have a valid HtmlElement ...
if Assigned(HtmlElement)
then // show a message
showmessage(HtmlElement.getAttribute('id', 0));
// now we do not care about if HtmlElement is valid or not
form1.Memo1.Text := HtmlElement.getAttribute('id', 0);
Cancel := True;
Self.Close;
end;
end;
end;
end;
To only solve your current access violation you simply put a begin end block around all the lines that will use HtmlElement.
HtmlElement := iHTMLDoc.ElementFromPoint( MousePos.X, MousePos.Y );
if Assigned( HtmlElement )
then
begin
showmessage( HtmlElement.getAttribute( 'id', 0 ) );
form1.Memo1.Text := HtmlElement.getAttribute( 'id', 0 );
end;
But there are some more issues in your code. You should not use the global variables form1 and form2. Instead pass the form instance to the created TForm2 instance or even better a callback method.

How do I save the contents of TWebBrowser, including user-entered form values?

is it possible to save entire document loaded in Webbrowser (in Delphi) as a ordinary HTML file with new values (I mean values entered by user in html's forms this document)?
I need this for reading this HTML document with all values next time when application will be used.
Sure this is possible!
Small demo App, make a new vcl forms application, drop a TWebBrowser, a TButton and a TMemo on your form and use this code (don't forget to bind OnCreate for the Form and OnClick for the Button)
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls,mshtml, ActiveX;
type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//code snagged from about.com
procedure WBLoadHTML(WebBrowser: TWebBrowser; HTMLCode: string) ;
var
sl: TStringList;
ms: TMemoryStream;
begin
WebBrowser.Navigate('about:blank') ;
while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
Application.ProcessMessages;
if Assigned(WebBrowser.Document) then
begin
sl := TStringList.Create;
try
ms := TMemoryStream.Create;
try
sl.Text := HTMLCode;
sl.SaveToStream(ms) ;
ms.Seek(0, 0) ;
(WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ;
finally
ms.Free;
end;
finally
sl.Free;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Doc : IHtmlDocument2;
begin
Doc := WebBrowser1.Document as IHtmlDocument2;
Memo1.Lines.Text := Doc.body.innerHTML;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Html : String;
begin
Html := 'change value of input and press Button1 to changed DOM<br/><input id="myinput" type="text" value="orgval"></input>';
WBLoadHTML(WebBrowser1, Html);
end;
end.
Output:
EDIT
As mjn pointed out, the values of password type inputs will not be shown.
You can still can get their value though:
add these 2 lines to Button1.Click and change html
OnCreate:
Html := 'change value of input and press Button1 to changed DOM<br/><input id="myinput" type="password" value="orgval"></input>';
OnClick:
El := (Doc as IHtmlDocument3).getElementById('myinput') as IHtmlInputElement;
Memo1.Lines.Add(Format('value of password field = %s', [El.value]))