evitar que se ejecute el evento OnActivate del formulario principal

I have a simple program (in delphi 7) on its main form is a button. When I click the button on the main form, I will open the second form. When I close the second form how can I prevent running main form OnActivate event? (except this code: MainForm.OnActivate = nil)

gracias

preguntado el 09 de enero de 11 a las 09:01

What's in the OnActivate event handler, why shouldn't it run when Form2 closes, and should it run at other occasions (for example when the user switched away from your app and returns to it). The answers to these question greatly dictate the solution you need. -

i suspect wrong OnActivate usage, fix it and problem will go away. -

Indeed it sounds a little like what is happening in MainForm.OnActivate isn't appropriate and is in fact the real problem. -

The code is probably better located in Application.OnActivate? -

2 Respuestas

A nice shorthand for temporarily disabling an event using GpStuff (Licencia BSD):

uses
  GpStuff;

with DisableHandler(@@MainForm.OnActivate) do
  Form2.ShowModal;

Respondido el 09 de enero de 11 a las 13:01

Despite the double pointer reference, which looks a little bit ugly, I find this an absolutely cool solution. +1 - Uwe Raabe

I think this only works when the event property is in fact stored directly in a field. This is true for most VCL events, but not necessarily for events in general. BTW: +1; I like the function returning an interface for automatic cleanup, I've been using that myself a lot, but see few people do. - Jeroen Wiert Pluimers

Trinary operator, ORLY? Anyways, overkill. - Consultoría gratuita

@Uwe: I couldn't find a nicer looking solution :( - Gabriel

@gabr Couldn't it be done with RTTI since these will always be published properties? - David Heffernan

easiest possibility

MainForm.OnActivate := NIL;
Form2.ShowModal;
MainForm.OnActivate := MainFormOnActivate;

using something else can break future compatibility in Delphi...

Respondido el 10 de enero de 11 a las 10:01

If Form2.Show is used then this doesn't have any effect. - David Heffernan

@David Heffernan I'm not sure I understand your point, can you please be a bit more specific? I'm a little slow the past few days :-P - user497849

Re[David's comment]: OnActivate is changed to 'nil', form2 is displayed, OnActivate is changed back (while the form2 is still displayed); some time later, user closes form2 and OnActivate of form1 is triggered. - Gabriel

aha, there's my flaw in logic... I must be losing my mind :-P anyhu the OnActive should not be triggered unless the form receives focus, but still there's a big proability of that event to happen, better use ShowModal to be on safe side or gabr's "DisableHandler" - user497849

My DisableHandler approach would also work only with ShowModal. - Gabriel

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.