Passing Tobject to another form? - delphi

I'm writing a touchscreen enabled application in Delphi XE2.
I have a form with TEdits. When I click on them, I call the procedure I've written to show another maximized always on top form with a TTouchkeyboard with a label (for caption) and a TEdit for keyboard input.
My procedure (vkeyboard is my form name with the TTouchkeyboard):
procedure TLogin.showkeyboard(numeric,password: Boolean;
caption,value:string;Sender:TObject);
begin
if numeric then
vkeyboard.TouchKeyboard1.Layout := 'NumPad' // make the TTouchkeyboard on the form numeric or alpha
else
vkeyboard.TouchKeyboard1.Layout := 'Standard';
if password then
vkeyboard.input.PasswordChar := '*' //make the TEdit show * or normal characters
else
vkeyboard.input.PasswordChar := #0;
vkeyboard.title.Caption := caption;
vkeyboard.input.Text := value;
vkeyboard.Show;
end;
I'm trying to send Form1.Edit1 object to the form vkeyboard but i don't know how to do it properly!
Why? Because i want to be able to click Done on the input form (vkeyboard) then trace back who was the sender then update the text in the main form edit!
procedure Tvkeyboard.sButton1Click(Sender: TObject);
begin
(temp as TEdit).Text := input.Text; // send back the text to the right object
vkeyboard.Hide;
end;
This little part of course didn't work... I think i need to specified that the temp object belong the X form ?
To be clear, i want to trace back who called the procedure or at least be able to specified it in the procedure and then return back the text (from the 2nd form to the main one) to the right TEdit!

You're welcome to pass whatever arguments you want to whatever function you want. If you need to use the passed value in yet another function, you'll need to save it somewhere so the later function can still access it.
Using your example, you appear to have provided a Sender parameter for your showkeyboard function. I assume that's where you're passing a reference to the TEdit control that triggered the keyboard to show. The Tvkeyboard object stored in vkeyboard will need to use that value later, so give a copy of that value to the Tvkeyboard object. Declare a TEdit field:
type
Tvkeyboard = class(...)
...
public
EditSender: TEdit;
Then, in showkeyboard, set that field:
vkeyboard.EditSender := Sender;
Finally, use that field when you set the text:
procedure Tvkeyboard.sButton1Click(Sender: TObject);
begin
EditSender.Text := input.Text; // send back the text to the right object
Self.Hide;
end;
Since you know it will always be a TEdit control, you can change the type of the Sender parameter in showkeyboard to reflect that specific type:
procedure TLogin.showkeyboard(..., Sender: TEdit);

Related

Delphi - Getting property values with GetPropValue()

I'm using Delphi's GetPropValue() function to get values ​​of certain properties of some objects of type TControl. Everything works correctly when I get simple property values ​​such as Value, Opacity, etc, but as I'm using firemonkey there are some extended properties, such as RotationCenter, it has RotationCenter.X and RotationCenter.Y, or even properties of text within TextSettings, in these properties with sub-types I can not get the values.
In this example I get the values ​​correctly:
If IsPublishedProp (Component_cc, 'Value') then
EditValue.Text: = GetPropValue (Component_cc, 'Value', true);
Where Component_cc:TControl; And is created dynamically, it can also be any type of Firemonkey component (so far everything is okay, everything works).
When I need to use the form below, it does not work.
If IsPublishedProp (Component_cc, 'RotationCenter.X') then
EditRotationCenterX.Text: = GetPropValue (CC_component, 'RotationCenter.X', true);
Does anyone know a way to get these properties extended by this function?
First, the CC_component's RotationCenter property is actually an instance of TPosition class which decends from TPersistent.
Second, you cannot use dotted notation when calling IsPublishedProp.
You can use GetObjectProp to first retrieve the internal TPosition instance and then access the X property from there:
(Assume a simple FMX application with one form that contains a TButton called Button1 and a TEdit called EditRotationCenterX.)
procedure TForm1.Button1Click(Sender: TObject);
var
CC_component : TComponent;
CC_component_RotationCenter : TPosition;
begin
CC_component := Button1;
if IsPublishedProp(CC_component, 'RotationCenter') then
begin
CC_component_RotationCenter := TPosition(GetObjectProp(CC_component, 'RotationCenter'));
EditRotationCenterX.Text := CC_component_RotationCenter.X.ToString;
end
end;
Update, for a property of type Set:
For a Set type property, you will need to retrieve its ordinal value using GetOrdProp. This will be the array of bits that represent which elements are included in the current value. Then, you simply test if the appropriate bit is set. This is the method I would prefer.
Alternatively you can use GetSetProp which will return a text representation of the elements in the Set's current value. For example, if the Set's value is [TCorner.BottonLeft, TCorner.TopRight] the you will get back the string value "TopRight,BottonLeft". You then check to see if the name of your target element appears anywhere in the returned string. This method is susceptible to failure if the Delphi RTL or FMX libraries are ever changed in the future.
(This example adds a TRectangle shape called Rectangle1 and a TCheckBox called cbCornerBottonRight to the simple FMX App from above:)
procedure TForm1.Button1Click(Sender: TObject);
var
CC_component : TComponent;
CC_component_Corners : nativeint;
CC_component_CornersAsString : string;
begin
CC_component := Rectangle1;
if IsPublishedProp(CC_component, 'Corners') then
begin
// Using this method will make your code less sensitive to
// changes in the ordinal values of the Set's members or
// changes to names of the enumeration elements.
//
CC_component_Corners := GetOrdProp(CC_component,'Corners');
cbCornerBottonRight.IsChecked := ((1 shl ord(TCorner.BottomRight)) and CC_component_Corners) <> 0;
// This approach may break if the names of the elements of
// the TCorner enumeration are ever changed. (BTW, they have
// been in the past: "cvTopLeft", "cvTopRight", "cvBottomLeft",
// and "cvBottomRight" are now deprecated)
//
CC_component_CornersAsString := GetSetProp(CC_component,'Corners');
cbCornerBottonRight.IsChecked := CC_component_CornersAsString.IndexOf('BottomRight') >= 0;
end;
end;
When speaking about the old RTTI, you can do this. You need to go deeper in the structure. Ask for the X property the TPosition object:
var
O: TObject;
X: Integer;
begin
if PropIsType(Component_cc, 'RotationCenter', tkClass) then
begin
O := GetObjectProp(Component_cc, 'RotationCenter');
if Assigned(O) and PropIsType(O, 'X', tkInteger) then
X := GetOrdProp(O, 'X');
end;
end;

TUpDown and TEdit with custom captions

I associated a TEdit to a TUpDown. It updates the TEdit text fine automatically with the TUpDown.position value. But I want to display custom captions depending on the TUpDown.position value.
For this I unassociated the TEdit from the TUpDown and wrote custom onClick/onChanging handlers. But both of the cases the TUpDown.position contains the previous value (not the incremented/decremented one). What event should I use to update the TEdit.text depending on the right TUpDown.position value?
I use Delphi XE4.
Use the OnChangingEx event. It has a NewValue parameter, that holds the new value to which the control is changing.
procedure TForm19.UpDown1ChangingEx(Sender: TObject; var AllowChange: Boolean;
NewValue: Integer; Direction: TUpDownDirection);
begin
Edit2.Text := IntToStr(NewValue);
end;

How to know when the USER changed the text in a TMemo/TEdit?

I was always bugged by the fact that TMemo (and other similar controls) only have the OnChange event. I would like to know when the USER changed the text, not when the text was changed programmatically.
I know two methods to discriminate between the user changed text and programmatically changed text:
Put OnChange:= NIL before you change the text programmatically. Then restore the OnChange. This is error prone as you need to remember to do it every time you change text from the code (and to which memos/edits needs this special treatment to be applied). Now we know that every time the OnChange is called, the control was edited by user.
Capture the OnKeyPress, MouseDown, etc events. Decide if the text was actually changed and manually call the code that needs to be called when user edited the ext. This could add a big amount of procedures to an already large file.
There is a more elegant way to do it?
You can write a helper procedure to do your option 1, and use it in your framework whenever you want to ensure no OnChange event is triggered when you set the text. e.g.:
type
TCustomEditAccess = class(TCustomEdit);
procedure SetEditTextNoEvent(Edit: TCustomEdit; const AText: string);
var
OldOnChange: TNotifyEvent;
begin
with TCustomEditAccess(Edit) do
begin
OldOnChange := OnChange;
try
OnChange := nil;
Text := AText;
finally
OnChange := OldOnChange;
end;
end;
end;
TMemo has also the Lines property which also triggers OnChange, so you can make another similar procedure that accepts Lines: TStrings argument.
How about using the Modified property?
procedure TForm1.MyEditChange(Sender: TObject);
begin
if MyEdit.Modified then
begin
// The user changed the text since it was last reset (i.e. set programmatically)
// If you want/need to indicate you've "taken care" of the
// current modification, you can reset Modified to false manually here.
// Otherwise it will be reset the next time you assign something to the
// Text property programmatically.
MyEdit.Modified := false;
end;
end;

When iterating through controls on a form, how can I identify particular buttons?

I need to make some changes to a TaskDialog before it is shown to the user. It's fairly simple to use Windows API calls to work with each of the controls on the dialog box. I need to be more sure which button I have found. I would have expected to find a place where I could read the result the button would give if pressed.
in other words, if I pressed a button that would cause a return value (in Delphi, it's called a modal result) of 100, I would have expected there to be an API call I could call to find out what the button's "return value" would be. I haven't yet found any such call.
I don't want to rely on the button text..
Here's what I have so far.
function EnumWindowsProcToFindDlgControls(hWindow: HWND; _param:LPARAM): BOOL; stdcall;
var
sClassName:string;
hBMP:THandle;
i:integer;
begin
SetLength(sClassName, MAX_PATH);
GetClassName(hWindow, PChar(sClassName), MAX_PATH);
SetLength(sClassName, StrLen(PChar(sClassName)));
if sClassName='Button' then
begin
// always 0...
i:=GetDlgCtrlID(hWindow);
if (i=100) or (i=102) then
begin
hBmp := LoadImage(HInstance, 'DISA', IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE or LR_LOADTRANSPARENT );
SendMessage(hWindow, BM_SETIMAGE, WPARAM(IMAGE_BITMAP), LPARAM(hBmp));
end;
end;
// keep looking
Result:=true;
end;
procedure TForm2.TaskDialog1DialogConstructed(Sender: TObject);
begin
EnumChildWindows(TaskDialog1.Handle, #EnumWindowsProcToFindDlgControls, 0);
end;
I suspect it's not entirely "respectable" to do things like this with a dialog.
This is a Delphi 10 Win32 application using Delphi's VCL TTaskDialog component which is a wrapper around Windows task dialog feature. before it's shown, the OnConstructed event fires, executing this code.
Thank you for your help!
Win32 buttons do not have "return values", which is why there is no API to retrieve such a value from them. What you are thinking of is strictly a VCL feature.
In Win32 API terms, a button can have a control ID, and in the case of MessageBox(), for example, standard ID values like IDOK, IDCANCEL, etc are assigned to the dialog buttons. When a button is clicked and the dialog is closed, the button's control ID is used as the function return value.
But task dialogs do not use control IDs, which is why you do not see any assigned to the dialog buttons.
To identify a particular task dialog button, I can think of two ways:
during child enumeration, retrieve each button's caption text (GetWindowText()), and compare that to captions you are interested in. Just know that the standard buttons (from the TTaskDialog.CommonButtons property) use localized text, which does not make this a well-suited option for locating standard buttons unless you have control over the app's locale settings.
send the dialog a TDM_ENABLE_BUTTON message to temporarily disable the desired button that has a given ID, then enumerate the dialog's controls until you find a disabled child window (using IsWindowEnabled()), and then re-enable the control. You can then manipulate the found window as needed.
For Task Dialog messages and Task Dialog Notifications that operate on buttons (like TDN_BUTTON_CLICKED, which triggers the TTaskDialog.OnButtonClicked event), the standard buttons use IDs like IDOK, IDCANCEL, etc while custom buttons (from the TTaskDialog.Buttons property) use their ModalResult property as their ID.
You can send TDM_ENABLE_BUTTON directly via SendMessage() for standard buttons, or via the TTaskDialogBaseButtonItem.Enabled property for custom buttons.
For #2, this works when I try it:
uses
Winapi.CommCtrl;
function FindDisabledDlgControl(hWindow: HWND; _param: LPARAM): BOOL; stdcall;
type
PHWND = ^HWND;
begin
if not IsWindowEnabled(hWindow) then
begin
PHWND(_param)^ := hWindow;
Result := False;
end else
Result := True;
end;
procedure TForm2.TaskDialog1DialogConstructed(Sender: TObject);
var
hButton: HWND;
begin
// common tcbOk button
SendMessage(TaskDialog1.Handle, TDM_ENABLE_BUTTON, IDOK, 0);
hButton := 0;
EnumChildWindows(TaskDialog1.Handle, #FindDisabledDlgControl, LPARAM(#hButton));
SendMessage(TaskDialog1.Handle, TDM_ENABLE_BUTTON, IDOK, 1);
if hButton <> 0 then
begin
// use hButton as needed...
end;
// custom button
TaskDialog1.Buttons[0].Enabled := False;
hButton := 0;
EnumChildWindows(TaskDialog1.Handle, #FindDisabledDlgControl, LPARAM(#hButton));
TaskDialog1.Buttons[0].Enabled := True;
if hButton <> 0 then
begin
// use hButton as needed...
end;
end;

Getting data from Modal forms before it closes

I have a Modal form, and in the Ok button it processes some information, that I need in the form that called the modal form.
How can I get it out before it closes?
Or delay the close till I say it can close.
I expect that your OK button has ModalResult set to mrOK. If you want to add error checking to the OK button then change that to mrNone. Add an OnClick handler to the button which does whatever checking or processing you need. If it determines that the form can close, set Self.ModalResult := mrOK in the OnClick handler.
Do you really need to access the information before the form is closed? Delaying the closing of a form will affect the users experience of the app (unless it's fast enough that they don't notice - in which case why delay it at all?)
A closed form is still available in memory for the caller (unless the close action is caFreeOnClose). So you should be able to add public properties to the form which you can then access within the caller.
e.g
Type Form2 = Class(TForm)
public
//Add a public property here
end;
From the caller:
if Form2.ShowModal = mrOk then
begin
InformationIWant = Form2.PublicProperty;
end;
Just a combination of what others are saying.
It is a good idea to formalize how to validate and get data out from a modal dialog.
Using the same technique over and over again makes everything easier to maintain and read.
An example :
Type TFormModal = Class(TForm)
procedure OnOkClick( Sender : TObject);
function ValidateInterface : boolean;
public
procedure SetInterface( input data here);
procedure GetInterface( output data here);
end;
procedure TFormModal.OnOkClick( Sender : TObject);
begin
if ValidateInterface
then modalResult := mrOk
else modalResult := mrNone;
end;
from your main form :
procedure MainForm.OnShowMyModalFormClick( sender : TObject);
var
myModal : TFormModal;
begin
...
myModal := TFormModal.Create( nil);
try
myModal.SetInterface( ...);
if (myModal.ShowModal = mrOk) then myModal.GetInterface(...);
finally
myModal.Free;
end;
...
end;
An alternative to David’s answer is to use either OnClose or OnCloseQuery events. With OnCloseQuery you can prevent the form from closing by setting CanClose := false;
As addition to JamesB's answer.
You must call Form2.Free, áfter you take the information you want.
I generally add a new function to the second form's unit, something like:
type
TForm2 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
InformationIWant : SomeType;
end;
var
Form2: TForm2;
function ReturnValue : SomeType
implementation
function ReturnValue : Sometype;
begin
try
if Form2 = nil then
Form2 := TForm2.Create(nil);
Form2.Windowstate := wsNormal;
Form2.BringToFront;
SetForegroundWindow(Application.Handle);
if Form2.ShowModal then
Result := InformationIWant
finally
FreeAndNil(Form2);
end;
end;
Let's say you want to do more than to simply know if the user pressed the OK or Cancel button in your modal from.
Let's suppose you need to set some parameters for the MainFrom in FromSettings.
Create and show the FormSettings modal
When the user presses the "OK" (or "Apply") button in FormSettings to close the form, you transfer all your data from FormSettings to MainForm
Finally call FormSettings.Close or better FormSettings.Release (not FormSettings.Free) to close the form.
Of course the MainFrom has to have some exposed (public) fields in which you receive the data from FormSettings, like:
FormMain.AlphaBlendValue := FormSettings.spnTransparency;
Hint 1:
You don't necessary have to transfer data from FormSettings into MainForm. If it better suits you, you can also save the data in a global variable, or record.
Hint 2:
I personally don't use the method described above, which is designed to save RAM when the FormSettings is freed.
I actually never destroy the SettingsForm. Some people would say that this is "horror" but computers today have 4GB or RAM (at least) and a form with some controls on it will "waste" a very very little amount of that RAM. So, the FormSettings in is memory all the time. When I need some values, I just let the MainForm to read them from FormSettings.
Again... this is definitively the recommended way to do it. It is the convenient way :) You have been warned!

Resources