BeforeOpen and AfterOpen for AdoStoredProc.ExecProc - delphi

Is there an analog of the BeforeOpen and AfterOpen events for ExecProc method in Delphi?

Related

TCard class does not have public OnShow and OnHide events

In a Delphi 10.4 VCL Application, TCard (as container-item of TCardPanel) does not have public OnShow and OnHide events (like TTabSheet has).
Therefore, the TCard.OnEnter event-handler is NOT triggered when a specific TCard is ACTIVATED. The TCard.OnEnter event-handler is triggered ONLY when e.g. clicking on a control on the TCard.
Example code:
CardPanel1.ActiveCard := Card2;
In this case, the TCard.OnEnter event is NOT triggered!
Is it possible to upgrade the TCard class with public OnShow and OnHide events? Or is it possible to simulate those events?
Use the OnCardChange event of TCardPanel and compare PrevCard and/or NextCard with your actual card instances.

Delphi - Assign Form OnClose on an existent one

I have many Forms on my project that uses the Form OnClose event.
However, I need to add another "generic" OnClose on all forms in runtime.
There is any way to just add the new event method, instead of replace it? So, the form will trigger both OnClose events.
Only one handler can be assigned to an event at a time.
What you could do is assign the "generic" handler to each Form's OnClose event, and then have each Form override its virtual DoClose() event to do their local work. It can call the inherited DoClose() method when ready to call the generic handler. For example:
type
TMyForm = class(TForm)
protected
procedure DoClose(var Action: TCloseAction); override;
end;
procedure TMyForm.DoClose(var Action: TCloseAction);
begin
// do something here...
inherited; // <-- call OnClose handler
end;
The alternative is to implement a multicast delegate for the actual event handler, and then the delegate can call other handlers as needed. Here are a few blogs on that topic:
Multicast events using generics
MultiCast Events - Part 1
MultiCast Events - Part 2
MultiCast Events - Conclusion
Alternatively, you can ignore the OnClose event altogether and implement an Observer Pattern instead (using DoClose() to call the observers). Here are a few blogs on that topic:
Observer Design Pattern in Delphi
Delphi and the Observer Pattern
The Observer Pattern

Form OnDeactivate - need to determine which is the new Activated control

I have the code which shows a search form for a specific DBGrid which is placed in another form (the caller Form of TSearchGridForm):
procedure TSearchGridForm.FormDeactivate(Sender: TObject);
begin
// Pseudo
if NewActiveControl <> CallerForm.DBGrid then
Close;
end;
The TSearchGridForm is activated by the caller form with .Show (not Modal) and when it is deactivated I want to close/hide it only if the new active control <> CallerForm.DBGrid.
Only if the user clicked on DBGrid on the caller form the search form should remain visible, otherwise I need to close it.
How can I do this?
Delphi's TScreen object has events OnActiveControlChange and OnActiveFormChange. You can set up event handlers for these to monitor changes and react to them.
See the D7 Online Help for more info. There are Delphi VCL code examples of using both events.

How can I abort a SMTP send operation with Synapse for Delphi?

At the moment I am testing Ararat Synapse to send e-mails in Delphi.
A local function creates a TSMTPSend and sends the e-mail.
How can I abort this operation?
I have set a callback function assigned to SMTP.Sock.OnStatus to perform some status output.
When I want to abort the send progress, I thought I could use the TTCPBlockSocket of the TSMTPSend within the callback function because in this function I have no access to the TSMTPSend directly.
What I wanted to do looks basically like
MyCallBack(Sender: TObject; Reason: THookSocketReason; const Value: string);
begin
if FCancelWasClicked then
begin
if Sender is TTCPBlockSocket then
TTCPBlockSocket(Sender).StopFlag := True;
// or TTCPBlockSocket(Sender).AbortSocket or CloseSocket
end;
end;
But StopFlag shows no effect and AbortSocket/ CloseSocket lead to a StackOverFlow because the socket will then be pumping HR_CloseSocket messages endlessly.
Am I doing it wrong? Are there other options?
Synapse provides a heartbeat function, which allows implementation of Cancel behaviour.
http://www.ararat.cz/synapse/doku.php/public:howto:heartbeat
Handle the OnHeartbeat event, set the HeartbeatRate property to the interval between heartbeats, and set the StopFlag to cancel the operation.

Delphi: Convert keys to letters, ignoring shiftsate

I'm trying to make a search feature where the user can hold in the control key and type some text to search.
I'm using the OnKeyDown/OnKeyUp to trap the control key.
Is there a easy way to check if the key parameter given to the onKeyUp/Down event is a litteral?
Or is it possible to convert the char given to OnKeyPressed while holding down the control key to the char it would have been if the control key where not pressed?
Edit:
I need a solution that can handle letters beyond the simple a..z range, like æ, ø å.
It looks like Delphi 2009 got a couple of useful methods in the TCharachter-class; e.g. function IsLetterOrDigit(C: Char): Boolean;
I'm stuck with delphi 2007, though...
The OnKeyDown & OnKeyPress Events have a number of Limitations.
Rather use a TApplicationEvents Component.
In it's OnShortCut Event hander use the following code
procedure TFormA.ApplicationEvents1ShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
if (Msg.CharCode = Ord('B')) and (HiWord(Msg.KeyData) and KF_ALTDOWN <> 0) then
begin
Handled := True;
// Do what needs to be done;
end;
end;
This will trap ALT-B
have a look at Delphi - Using the TApplicationEvents OnShortCut event to detect Alt+C key presses for more info
You can convert the Key(Char) parameter from OnKeyPress event to it's ordinal value using Ord(Key) however, in the OnKeyDown event you can do the following
if CharInSet(Char(Key), ['A'..'Z']) then
do something
you might also want to play with ShiftState to check if ALT, CTRL and/or SHIFT key is down...
Here you have all the info:
Virtual Key Codes
Understanding and Processing Keyboard events in Delphi

Resources