As I typed this question I realize that it probably should.
Docking a form to a TPageControl calls FormShow when form.Create() is called and when form.ManualDock(pagecontrol,pagecontrol.alClient) is called.
Un-docking the form also calls show and I assume this is because the form is actually 'reset' when you dock/undock?
If this is as designed I'll just refactor the code I dont want to fire there to onCreate (unless that is bad design).
If should or not is more philosophical than technical question. The TForm.OnShow event is fired by performing control message CM_DOCKCLIENT which is used also by the ManualDock function. Internally this message calls the CM_SHOWINGCHANGED what fires the event itself.
In the following example I will use two forms, Form1 (with a page control and a button) and Form2 (empty and dockable). I presume that both are auto created.
The following code is a proof that the OnShow event is fired by the CM_DOCKCLIENT control message. Clicking on the button, the CM_DOCKCLIENT message is performed and Form2's OnShow event is fired.
Code for Form1
procedure TForm1.FormShow(Sender: TObject);
begin
Form2.Show;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
DockObject: TDragDockObject;
begin
DockObject := TDragDockObject.Create(Form2);
try
// sending the CM_DOCKCLIENT message internally performs also the
// CM_SHOWINGCHANGED message which triggers the TForm.OnShow event
PageControl1.Perform(CM_DOCKCLIENT, WPARAM(DockObject), LPARAM(SmallPoint(0, 0)));
finally
DockObject.Free;
end;
end;
And Form2 has only the OnShow event handler
procedure TForm2.FormShow(Sender: TObject);
begin
ShowMessage('FormShow');
end;
An easy workaround is not to dock the Form2 manually by its own (in the OnShow event) but dock it by the creator or let's say by the form which displays it. In my previous example I've displayed the Form2 in the Form1.OnShow event, so I can easily dock it manually there.
procedure TForm1.FormShow(Sender: TObject);
begin
Form2.Show;
Form2.ManualDock(PageControl1);
end;
procedure TForm2.FormShow(Sender: TObject);
begin
// no manual docking of this form by itself
end;
Related
I need that "x" button on any form would not close the form but instead open another 3 random forms on delphi, i have no idea how to do that, please help
Just use the form's OnCloseQuery event to detect the user's trying to close your form (by clicking the close button in the top-right corner, by double-clicking the form's title bar icon, by selecting the Close system menu item, by pressing Alt+F4, etc.).
Then set CanClose to False and instead open your three new forms:
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := False;
Form2.Show;
Form3.Show;
Form4.Show;
end;
As suggested by #AndreasRejbrand's answer, you could use the Form's OnCloseQuery event. But, the problem with that approach is that the event is also triggered during system reboot/shutdown, and you don't want to block that. If OnCloseQuery returns CanClose=False during a system shutdown, the shutdown is canceled.
Another option is to use the Form's OnClose event instead, setting its Action parameter to caNone, eg:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone;
Form2.Show;
Form3.Show;
Form4.Show;
end;
However, the best option would to be to handle only user-initiated closures (the X button, ALT-F4, etc) by having the Form handle the WM_SYSCOMMAND message looking for SC_CLOSE notifications, eg:
procedure TForm1.WndProc(var Message: TMessage);
begin
if (Message.Msg = WM_SYSCOMMAND) and (Message.WParam and $FFF0 = SC_CLOSE) then
begin
Message.Result := 0;
Form2.Show;
Form3.Show;
Form4.Show;
end
else
inherited;
end;
This way, system-initiated closures are unhindered.
I have multiple TTabSheet on TPageControl and on certain action, user click on a button, I would like run OnResize event on a selected TTabSheet. The problem is that not all TTabSheet controls have OnResize event created.
I have this code on button:
procedure TForm1.Button1Click(Sender: TObject);
begin
TTabSheet(PageControl1.ActivePage).OnResize(PageControl1.ActivePage);
end;
procedure TForm1.TabSheet1Resize(Sender: TObject);
begin
// actions on Resize
end;
It works when TabSheet1 is active. But when TabSheet2 is active and it doesn't have OnResize event I get error:
Project Project1.exe raised exception class $C0000005 with message
'access vialotion at 0x00000000: read of address 0x00000000'.
I tried to check for nil, like this:
If TTabSheet(PageControl1.ActivePage).OnResize(PageControl1.ActivePage) <> nil then...
But it doesn't compile:
E2008 Incompatible type.
The workaround I discovered is for each TTabSheet control to have empty OnResize event, with just a comment, no code.
Is there any better check than <> nil, that doesn't work, if TTabSheet has OnResize event?
Thank you
You are probably going about this the wrong way. You should not invoke event handlers in your code. Leave that to the framework. Instead write it like this:
procedure TForm1.DoTabsheet1Resize;
begin
// actions on Resize
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
DoTabsheet1Resize;
end;
procedure TForm1.TabSheet1Resize(Sender: TObject);
begin
DoTabsheet1Resize;
end;
Here DoTabsheet1Resize is a private method that you define.
procedure TForm1.Button1Click(Sender: TObject);
begin
If Assigned(PageControl1.ActivePage.OnResize)
then PageControl1.ActivePage.OnResize(Sender);
end;
Judging by this comment of yours
I set OnResize events, but nothing for TabSheet2. So TabSheet2 has OnResize in Object Inspector but not in code in unit1.
I think you may be confused about exactly what goes on the Form Designer, it terms of defining event-handling code.
Try this:
On a newly-created blank form, drop a TPageControl, PageControl1, right-click it and create two TabSheets in it, TabSheet1 and TabSheet2.
In the OI, click TabSheet1, click its Events tab in the OI and click the drop-down button in the OnResize event property. You will find the drop down list empty.
Now double-click in Tabsheet1's OnResize event. Doing this causes the OI to create an empty event handling procedure TabSheet1Resize in the form's code. It also assigns the OnResize property of TabSheet1 to this procedure, TabSheet1Resize. Don't save the form at this point, or the IDE will detect that this procedure contains no code (or comments) and will delete it.
In TabSheet1Resize add this code
Caption := 'TabSheet1.Resize';
and save the form's unit.
Now select TabSheet2's event tab in the OI and single-click in its OnResize event. At this point TabSheet2's OnResize property is not assigned to any procedure so attempting to call TabSheet2.OnResize() will result in an AV, because the value of its OnResize property is Nil.
Now, In TabSheet2's OnResize, click the drop-down button and you will see
TabSheet1Resize as the single entry in the list. Select it from the list and now the value of TabSheet2's OnResize property is the same as TabSheet1'1, i.e. the procedure TabSheet1Resize.
Steps 3 and 6 are the ones which are vital for TabSheet1 and TabSheet2 to have the same OnResize event-handler code.
In other words, your comment seems to overlook the difference between the event-handler property (e.g. OnResize) which the object, in this case a TTabSheet always has (which is why the OI shows you the event-handler property) and the code in the event-handler procedure, if any, to which the event-handler property is assigned.
Sorry if all the above is painfully obvious, if it is then I've misunderstood several of your comments.
If you want all the other tabsheets to have the same OnResize handler as TabSheet1, you can set this up in, e.g., your FormCreate event handler, like so:
procedure TForm1.FormCreate(Sender: TObject);
var
i : integer;
begin
for i := 1 to PageControl1.PageCount - 1 do
PageControl1.Pages[i].OnResize := PageControl1.Pages[0].OnResize;
// PageControl1.Pages[0] will be equal to TabSheet1, if that's the first Tabsheet
// you created
end;
I would like to know where is the formshow in delphi 2010 as when I can only see a formcreate in my project.
The reason I am asking is because I need to add Randomize in the FormShow event handler, as shown below:
procedure TfrmWinnaSpree.FormShow(Sender: TObject);
begin
Randomize;
end;
You create the event handler the same way you create almost every event handler in Delphi, by double-clicking the method in the Events tab of the Object Inspector.
Click on the form itself (not any control on the form), then switch to the Object Inspector. Click on the Events tab, and then scroll down to the OnShow event. Double-click in the right half next to the event name, and the IDE will create a new, empty event handler and put the cursor in the right place to start writing code.
procedure TForm3.FormShow(Sender: TObject);
begin
|
end;
However, FormShow is the wrong place to call Randomize, because FormShow executes every time your form is shown, and that can happen more than once. Here's an example (it assumes two forms, Form1 and Form2, autocreated as usual in the .dpr file with the default variable names, which of course is a bad idea - this is to demonstrate a problem with your question's purpose):
procedure TForm2.FormShow(Sender: TObject);
begin
ShowMessage('In FormShow');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Visible := not Form2.Visible;
end;
Run the program and click TForm1.Button1 multiple times; you'll see the In FormShow message every other time you do so.
The proper places for a call to Randomize are:
in your main form's FormCreate
in an initialization section of your main form's unit
unit uMainForm;
interface
...
implementation
...
initialization
Randomize;
end.
in your project source (.dpr) file
program MyGreatApp;
uses
Math,
Vcl.Forms,
uMainForm in 'uMainForm.pas' {Form1};
{$R *.RES}
begin
Randomize;
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.Title := 'My Super App';
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Alternatively you can also override the protected method TForm.DoShow:
type
TForm = class(Forms.TForm)
protected
procedure DoShow; override;
end;
implementation
procedure TForm.DoShow;
begin.
// custom show code
inherited;
// custom show code
end;
The advantage over the event-based approach is that you can put your custom code before or after the inherited call.
I'm stuck on a problem in Delphi 7 about event propagation (due to my ignorance).
I am asked to dynamically attach an OnMouseUp event handler on some controls on a form (and I'm fine with that thing), but if the OnMouseUp is present, the OnClick event on that control must not be processed.
Background
If you are asking the reason behind this, well, I'm in charge to modify an old production monitoring application (sigh) that from now on must accommodate a conditional behaviour for some controls, in direct response to a former click on a special function button.
Some of those controls have an OnClick event handler already; the first solution the team came up with was to punctually intervene on each OnClick handler and manage out the contextual actions in relation to the special function button status.
I suggested to take advantage of the Object-Oriented design already in place for the application forms: they all inherit from the same custom ancestor object, so I planned to insert an initialization method there to dynamically attach OnMouseUp events to the controls that are declared to support it in subclasses.
The need
I'm not hereby asking a validation or questioning on the (possible lack of) design goodness about all this (by the way, after a lot of thinking and reasoning it seemed to be the path we can walk with less pain); my problem is that for such design to take place I'd have to let dynamically-attached OnMouseUp event handlers stop event propagation to the pre-existent OnClick events on those controls.
Is it possible with Delphi 7?
Please note, the following does not explicitly answer the question here. It's more a proposal to the concept re-design (redirect OnClick events instead of adding extra OnMouseUp). It's about how to redirect OnClick event handler (if assigned some) of all components (might be filtered, if needed) to another (common) OnClick event handler. It includes also a method for restoring them to the original state.
In the following example I'll try to show you how to replace and then optionally restore the OnClick event handlers (if the component has written some) by the specific one. This is done to all components having the OnClick event published, so you don't need to know in advance if the component class has OnClick event available or not (but it can very simply be modified to use only a specific class).
The code consists from the following:
OnSpecialClick - it is the event handler to what all OnClick events will be binded when you call the ReplaceOnClickEvents procedure, notice that it must be published to be visible for RTTI !!!
Button1Click - represents here the old event handler which should be replaced, it is binded to the Button1.OnClick event at design time
ReplaceOnClickEvents - method, which iterates through all components on the form and checks if the currently iterated one has the OnClick event handler assigned; if so, it stores it into a backup collection and replace this event handler by the OnSpecialClick
RestoreOnClickEvents - method, which restores the original OnClick event handlers; it iterates through the backup collection and assign the event methods to its stored component instances
CheckBox1Click - this check box click event is meant to be the switch between the common and a special mode (CheckBox1 checked state means to be the special mode), only this OnClick event is not replaced by the ReplaceOnClickEvents call (because you wouldn't be able to restore the mode back to normal)
And here it is:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, TypInfo, StdCtrls, Contnrs;
type
TEventBackup = class
Component: TComponent;
OnClickMethod: TMethod;
end;
type
TForm1 = class(TForm)
Button1: TButton;
CheckBox1: TCheckBox;
procedure Button1Click(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
private
procedure ReplaceOnClickEvents;
procedure RestoreOnClickEvents;
published
procedure OnSpecialClick(Sender: TObject);
end;
var
Form1: TForm1;
EventBackupList: TObjectList;
implementation
{$R *.dfm}
procedure TForm1.OnSpecialClick(Sender: TObject);
begin
ShowMessage('Hi, I''m an OnSpecialClick event message!');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Hi, I''m just that boring original OnClick event message!');
end;
procedure TForm1.ReplaceOnClickEvents;
var
I: Integer;
Component: TComponent;
EventMethod: TMethod;
EventBackup: TEventBackup;
begin
for I := 0 to ComponentCount - 1 do
begin
Component := Components[I];
if Component = CheckBox1 then
Continue;
if IsPublishedProp(Component, 'OnClick') then
begin
EventMethod := GetMethodProp(Component, 'OnClick');
if Assigned(EventMethod.Code) and Assigned(EventMethod.Data) then
begin
EventBackup := TEventBackup.Create;
EventBackup.Component := Component;
EventBackup.OnClickMethod := EventMethod;
EventBackupList.Add(EventBackup);
EventMethod.Code := MethodAddress('OnSpecialClick');
EventMethod.Data := Pointer(Self);
SetMethodProp(Component, 'OnClick', EventMethod);
end;
end;
end;
end;
procedure TForm1.RestoreOnClickEvents;
var
I: Integer;
EventBackup: TEventBackup;
begin
for I := 0 to EventBackupList.Count - 1 do
begin
EventBackup := TEventBackup(EventBackupList[I]);
SetMethodProp(EventBackup.Component, 'OnClick', EventBackup.OnClickMethod);
end;
EventBackupList.Clear;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked then
ReplaceOnClickEvents
else
RestoreOnClickEvents;
end;
initialization
EventBackupList := TObjectList.Create;
EventBackupList.OwnsObjects := True;
finalization
EventBackupList.Free;
end.
As both TLama and TOndrej have said, there are a few ways to accomplish what you're attempting:
To do if Assigned(Control.OnMouseUp) then Exit; on your OnClick event handler
To "unassign" the OnClick event when assigning OnMouseUp (and vice-versa)
Both approaches will accomplish what you've detailed, though "unassigning" the OnClick event will be best for performance (to an infintismally small extent) since you won't be performing the if statement repeatedly.
I have added hints to components on my form. When the components receive the focus, I'd like to set the caption of a label component to display the hint.
I have added a TApplicationEvents object and set the OnShowHint event to
procedure TImportFrm.ApplicationEvents1ShowHint(var HintStr: string;
var CanShow: Boolean; var HintInfo: THintInfo);
begin
HelpLbl.Caption := HintStr;
end;
However it seems that the ShowHint event only fires with mouse movements. Is there a way to fire the hint code when components receive focus, without having to implement the OnEnter event for every single component on the form?
Add a handler for TScreen.OnActiveControlChange in your main form's creation, and handle the hints in that event:
type
TForm2=class(TForm)
...
private
procedure ScreenFocusControlChange(Sender: TObject);
end;
implementation
procedure TForm2.FormCreate(Sender: TObject);
begin
Screen.OnActiveControlChange := ScreenFocusControlChange;
end;
procedure TForm2.ScreenFocusControlChange(Sender: TObject);
begin
Label1.Caption := ActiveControl.Hint;
Label1.Update;
end;
Note that Sender won't do you much good; it's always Screen. You can filter (for instance, to only change the Label.Caption for edit controls) by testing the ActiveControl:
if (ActiveControl is TEdit) then
// Update caption of label with ActiveControl.Hint
Note that if you'll need to reassign the event when you show child forms (to an event on that child form), or you'll always be updating the original form's label with the hints. The easiest way to do the reassignment is to give every form an OnActiveControlChange handler, and assign it in the form's OnActivate event and unassign it in the OnDeactivate event:
procedure TForm1.FormActivate(Sender: TObject);
begin
Screen.OnActiveControlChange := Self.ScreenActiveControlChange;
end;
procedure TForm1.FormDeactivate(Sender: TObject);
begin
Screen.OnActiveControlChange := nil;
end;
This will allow you to update controls other than Label1 on each form, and only use the hint changes on forms you want to do so.
A simple solution is to use OnIdle event:
procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
begin
if Assigned(ActiveControl) then
Label1.Caption:= ActiveControl.Hint;
end;
A more advanced solution is to override protected ActiveChanged method of TForm:
type
TForm1 = class(TForm)
...
protected
procedure ActiveChanged; override;
end;
...
procedure TForm1.ActiveChanged;
begin
inherited;
if Assigned(ActiveControl) then
Label1.Caption:= ActiveControl.Hint;
end;
Receiving focus and OnShowHint are quite different events; OnShowHint can be triggered for non-focused control as well.
Why would you need to implement the OnEnter event for every single component? You can create one generic method / event handler like:
procedure TForm1.AnyControlEnter(Sender: TObject);
begin
lbl1.Caption := TControl(Sender).Hint;
end;
and assign it to every component you placed on the form.
You said:
it seems that the ShowHint event only fires with mouse movements
This is a normal behaviour. The problem you have ( it's a guess) is that hints are not fired directly. Don't try to make a workaround, what you try to do with MouseEnter is exactly what is already happening...the only difference is that you'be forget something...
Keep the event ApplicationEvents1ShowHint() as you've initially done but add this in the form constructor event:
Application.HintPause := 1;
And then hints will be displayed (almost) without delay.