Topic: Field OnChange Possible Bug

Dimitry,
I'm getting the following error on a field OnChange event when a form is initially opened to show a record in version 4.5.

https://s33.postimg.cc/rjhekdrwv/Cannot_Focus_Error.png


See attached sample. Edit (Double click) the record displayed in the tablegrid. You will get the error. I found this in one of my projects that was compiled under a version 3 (probably 3.6) and it worked fine. When I recompiled it under 4.5 it produces the error.

Post's attachments

Attachment icon Field OnChange.zip 336.44 kb, 359 downloads since 2018-08-15 

2 (edited by thezimguy 2018-08-15 22:57:12)

Re: Field OnChange Possible Bug

Hi ehwagner,
Please check the attached fixed of your project.
I think you were calling the onChange Event at a time the Form2 hadn't initialized.
Let me know if it works for you.

procedure Form2_OnShow (Sender: string; Action: string);
begin
     //Form2.Edit1.SetFocus;//it will still work without the setfocus
     Form2_Edit1_OnChange
end;

procedure Form2_Edit1_OnChange;
begin
    If Form2.Edit1.GetTextLen = 4 then Form2.SetFocusNextControl;
end;

begin

end.
Post's attachments

Attachment icon Field OnChange_fixed.zip 333.96 kb, 363 downloads since 2018-08-16 

@thezimguy

Re: Field OnChange Possible Bug

Sorry thezimguy, but that is not the process/effect I need. I need the OnChange event to test for the max length of a field before going automatically to the next field. MVD does the field OnChange event before the OnShow of the Form, but for some reason the way I have it in my project worked fine in version 3  but not in Version 4. Thanks for trying though. Appreciate it.

Re: Field OnChange Possible Bug

Try this to prevent the error

procedure Form2_Edit1_OnChange (Sender: TObject);
begin
    If not Form2.Visible then exit;
    If Form2.Edit1.GetTextLen = 4 then Form2.SetFocusNextControl;
end;

also you can add this:

procedure Form2_OnShow (Sender: string; Action: string);
begin
    Form2_Edit1_OnChange(nil);
end;
Dmitry.

Re: Field OnChange Possible Bug

Dimitry,
That worked to prevent the message. But I'm curious, why didn't I have to do that prior to version 4?   What changed?

Also, a general question, why does the field OnChange event occur before the OnShow event for the form? I would think logically that the OnSHow event would occur first to set the fields on the form and that the OnChange event would only occur when typing into a field. Doing the OnChange event first seems to be wasting a lot of code execution when it is not necessary especially if you have a lot of fields with OnChange events that do not need to be executed at all until changed. Just curious.

Re: Field OnChange Possible Bug

ehwagner wrote:

Dimitry,
That worked to prevent the message. But I'm curious, why didn't I have to do that prior to version 4?   What changed?

Unfortunately I don't remember all small changes.


ehwagner wrote:

Also, a general question, why does the field OnChange event occur before the OnShow event for the form? I would think logically that the OnSHow event would occur first to set the fields on the form

It's just work faster.


ehwagner wrote:

and that the OnChange event would only occur when typing into a field. Doing the OnChange event first seems to be wasting a lot of code execution when it is not necessary especially if you have a lot of fields with OnChange events that do not need to be executed at all until changed. Just curious.

For typing event you should use events like OnKeyDown, OnKeyPress, OnKeyUp

Dmitry.