Re: MessageDlg jams screen update



ellenjay1@xxxxxxxxxxx wrote:
I am having great difficulties with Message box stopping screen update

e.g

.procedure TForm1.FormActivate(Sender: TObject);
begin
Edit1.Visible:=True;
Edit1.SetFocus;
end;

procedure TForm1.Edit1DblClick(Sender: TObject);
begin
Edit1.Visible:=False;
End;

procedure TForm1.Edit1Exit(Sender: TObject);
begin
Label1.Visible:=True;
Label1.Caption:=Edit1.Text;
Application.ProcessMessages;
end;

works as expected.

BUT

Change to

procedure TForm1.Edit1Exit(Sender: TObject);
begin
Label1.Visible:=True;
Label1.Caption:=Edit1.Text;
Application.ProcessMessages;
{******************* }
GetOk;
{*******************}
end;

Procedure GetOk;
Begin
MessageDlg('Testing', mtInformation, [mbOK], 0);
end;

And Edit1 Remains Visible AND Label1 remains hidden until AFTER
MessageDlg is fired..

What am I missing?
Is there any way around this ?

Under no circumstances should you change the focus during a focus-changing event. During the OnExit event, the operating system is still in the process of switching the focus from one window to another. When you display a dialog box, you interrupt the normal sequence of focus-changing messages, and the controls involved in the original focus change get confused about whether they really have the focus. (One control might have focus, but another control is displaying the caret, for instance.)

The way to solve this is to post a message to the form. Posted messages go to the end of the message queue, so they will arrive after any other pending focus-related messages have already been handled. In your OnExit event, post a message to your form, like this:

PostMessage(Handle, wm_EditFocusLost, 0, 0);

You can define wm_EditFocusLost as a constant in the unit's interface section:

const
wm_EditFocusLost = wm_User + 1;

Now write a message handler for that message. In your form's private section, declare a method like this:

private
procedure WMEditFocusLost(var Message: TMessage); message wm_EditFocusLost;

Now implement the method:

procedure TLesForm.WMEditFocusLost(var Message: TMessage);
begin
if GetOK then begin
Label1.Caption := Edit1.Text;
Edit1.Visible := False;
Label1.Visible := True;
end else Edit1.SetFocus;
end;

Under very few circumstances should you call Application.ProcessMessages. I don't see why you need it in this situation.

--
Rob
.



Relevant Pages

  • Data Aware Component - Not getting value at app startup
    ... I'm trying to make a data aware edit control. ... with FDateEdit do begin ... if and not (csDesigning in ComponentState) ... procedure TKjrDBDateEdit.EditingChange(Sender: TObject); ...
    (alt.comp.lang.borland-delphi)
  • Re: Transparent Buttons
    ... >> Is there an easy way to make the background of a button transparent? ... >through the effort of dealing with a whole new control. ... procedure TForm1.Button1Click(Sender: TObject); ...
    (comp.lang.pascal.delphi.misc)
  • Re: Changing DefaultExt after changing Filter
    ... To change the filename you need to find the edit control on the ... common dialog the API way and set its contents with SetDlgItemtext. ... getClassname(ctrl, buf, 80); ... procedure TForm1.SaveDialog1TypeChange(Sender: TObject); ...
    (borland.public.delphi.language.objectpascal)
  • Re: Toggle button caption on mouseclick
    ... caption, but also enable/disable some control somewhere. ... select the control you're working with. ... But since Sender is declared as a TObject, and TObject doesn't have a Caption property, you need to type-cast Sender to a type that does have such a property. ...
    (comp.lang.pascal.delphi.misc)