Recent

Author Topic: [solved] TabSheet's OnShow Event Fires Before Form's OnShow -BUG?  (Read 2182 times)

emaza

  • Jr. Member
  • **
  • Posts: 56
    • http://GerenciaDeCondominios.com
Hi. I have a Form with a 3 page TabSheet. One  TabSheet (#2) shows info it must update every time it opens. For this it uses a query_2 with a parameter that takes its value from the ID field of a Query_1 that navigares a table following user's commandds.

I use OnShow event on the #2 Tab to read value from Query_1 and load parameter for query_2. To make sure value is always available query_1 is opened in the form's OnShow event. In this one-time Event it also loads the path  etc for the database connector.

Everything works fine except when the form first opens which gives an initial error because the tab's Show event fires BEFORE the Form's  Show event.

Is this a BUG or is this the way it is? Seems to me the Tab's OnShow should fire AFTER the Form's OnShow. Any tips?
« Last Edit: June 28, 2020, 09:37:58 pm by emaza »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TabSheet's OnShow Event Fires Before Form's OnShow -BUG?
« Reply #1 on: June 05, 2020, 12:45:19 pm »
That is a very bad approach to sync coding.
Never rely on the order of such child controls to receive messages in the order u want.

Also never do any gui coding in the on show events because at that point there many things not ready for such actions.
The only true wisdom is knowing you know nothing

emaza

  • Jr. Member
  • **
  • Posts: 56
    • http://GerenciaDeCondominios.com
Re: TabSheet's OnShow Event Fires Before Form's OnShow -BUG?
« Reply #2 on: June 06, 2020, 01:34:28 am »
Thanks for the quick reply. No surprise to me if my approach is not an example of best practice, the little I know I have taught myself. If I am not beyond help:

1.   When a form opens some things must happen or be in place. If not coded into the FormShow procedure, then where?

2.   I am only trying to find a quick fix  (that avoided re-starting from zero) to the REAL  problem which I posted in Stack Overflow, but got no solutions. Perhaps you might opine there or here. https://stackoverflow.com/questions/62124012/how-do-you-edit-records-in-a-form-that-diplays-records-from-two-joined-tables-f

Here is a copy/paste:

I am using Lazarus and a Firebird database to display several Forms. The datasource related query_1 joins two, sometimes three tables, and the data is shown in TDBEdit displays.

I need to place two buttons on forms: MODIFY and SAVE for the users. If data was only one table MODIFY would basically call Query_1. Edit, and SAVE would ApplyUpdates. With the joined tables in the query ApplyUpdates would give an error, but thought I could have the SAVE call a query_2 to select the record in one table (and later the corresponding one in the othery -all in the same transaction) and Edit the modified field taking the value from the TDBEdit.Text or the modified dataset’s fields, but this does not work because Lazarus/Firebirs does not let the user write into the TDBEdit of the joined table even though they are not ReadOnly.

I could instead use simple TEdits , populating them from Qry_1 fields to display the data and make changes, and then use the modified texts to edit the corresponding fields, but this will require a lot of changes as well as programing to keep the user from introducing garbage, and seems not to be the best solution.

I believe I am complicating something that should be more "standard" and simple but have not found other solutions. Would appreciate somebody pointing me to the right “normal” practice.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TabSheet's OnShow Event Fires Before Form's OnShow -BUG?
« Reply #3 on: June 06, 2020, 03:12:56 am »
Normally buttons that serve as special features on a form should be disabled while the form is warming up to a settle state.

 In your case the Save / Modify buttons should be in the disabled state until things are valid. This way users or code can not select these items until its ready...

 If you must continue with the OnShow which is only a graphical state when the form is coming from hiding or creation into visible.. Place a variable in your form's class that reports the state of the OnShow and don't allow your database code to make any changes or ignore changes in the database code until this state is in the ready mode.

Also the OnActive Event is yet another event that triggers when the form becomes focused but it appears  you are doing things behind the scenes that shouldn't be done until the form is fully in view and ready for user input.

 You need to wait for the form to fully stabilize before allowing the user or DB code to interact..

 IN most cases if there are some form of messages to be received they are usually received in the child controls first, those that are on top of the parent, which is your form in this case.

  There is a difference between software changes as to who has made the change in the DB controls over the user doing it... If control are disabled and they trigger then you know it was a software change..

 Come back if you still can't figure out something... Normally changing things via code should not trigger an event that a user action would trigger, this really confuses things

EDIT

  Assuming you  have a PageControl in play here, I think maybe what you should be doing is using a the OnChange or Onchanging, no the OnSHow of the tabsheet.
if the user changes the Tabs, then it could also process the code..
« Last Edit: June 06, 2020, 03:17:58 am by jamie »
The only true wisdom is knowing you know nothing

emaza

  • Jr. Member
  • **
  • Posts: 56
    • http://GerenciaDeCondominios.com
Re: TabSheet's OnShow Event Fires Before Form's OnShow -BUG?
« Reply #4 on: June 06, 2020, 05:58:55 am »
Thank you so much for your very helpful and knowledgeable comments and guidelines for this and future projects. You have explained the principal concepts and possible solutions well enough and I can see why things were not working as I expected. It will be some days before I come back to this post, hopefully with problem solved.  :D

emaza

  • Jr. Member
  • **
  • Posts: 56
    • http://GerenciaDeCondominios.com
Re: [Solved] TabSheet's OnShow Event Fires Before Form's OnShow -BUG?
« Reply #5 on: June 28, 2020, 09:25:51 pm »
I apologize for disappearing. Unrelated issues kept me away.  :)

1.   As you pointed out, Tab’s Onshow was firing after the Forn’s OnShow starded the visual displays but not yet executed the code written in that event. That is, the tab’s OnShow code executed before the Form’s was finished.

2.   Using the PageControl OnChange (TabSheet has no OnChange) solves this error (as well as another I had not mentioned) but -unseen to the user- unnecessarily updates the content of the Tab when the user moves between other tabs. It also requires the user to moves from one tab to another after opening the form in order to show data.

3.   Gave up on the bad ideas of showing the data  “automatically” when opening. Moved code to an Update button -which I was trying to avoid.  Form opens blank with all other buttons except “Update” disabled and works fine.

I hope some of this will be of help to others. Many thanks for the expert advice!

« Last Edit: June 28, 2020, 09:35:21 pm by emaza »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TabSheet's OnShow Event Fires Before Form's OnShow -BUG?
« Reply #6 on: June 28, 2020, 09:41:57 pm »
Hi!

To Point 2:

Insert to the PageControlOnChange

If PageControl.ActivePageIndex = 2 //or whatever
then ......

And for the automatic change on the start:

Design time: Set the PageControl.ActivePageIndex to 1 (or 2 or ....

In FormCreate: Set the PageControl.ActivePageIndex to 0 plus Application.ProcessMessages

Winni

emaza

  • Jr. Member
  • **
  • Posts: 56
    • http://GerenciaDeCondominios.com
Re: [solved] TabSheet's OnShow Event Fires Before Form's OnShow -BUG?
« Reply #7 on: June 28, 2020, 10:01:47 pm »
Ok sounds good. I had tried other coding (not mentoned) to achieve this but did not get it right. Will try this.
Many thanks.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: [solved] TabSheet's OnShow Event Fires Before Form's OnShow -BUG?
« Reply #8 on: June 28, 2020, 10:34:17 pm »
Arrrghhh

I forgot: They changed the behaviour of PageControlOnchange in one of the last versions.

To trigger the OnChange event if you set it by code you have to set in the Options of the PageControl the

nboDoChangeOnSetIndex

Winni

emaza

  • Jr. Member
  • **
  • Posts: 56
    • http://GerenciaDeCondominios.com
Re: [solved] TabSheet's OnShow Event Fires Before Form's OnShow -BUG?
« Reply #9 on: August 05, 2020, 02:30:54 am »
For the record:

After trying all determined that for my “automatic display” form  it was not necessary to write code Seting the PageControl.ActivePageIndex to 0 plus Application.ProcessMessages in the For,Create event so omitted that and also left blank the “Options” property  for the Pagecontrol in  the  Object inspedtor.

It was enough to write the “IF” statement in the PageControlOnChange event and Set the PageControl.ActivePageIndex properly in the Obiect inspector at design time as suggested, re-learning that Index 1 is Tab 2.

Now have two good working options to choose from. Many thanks to both,  :D


emaza

  • Jr. Member
  • **
  • Posts: 56
    • http://GerenciaDeCondominios.com
Re: [solved] TabSheet's OnShow Event Fires Before Form's OnShow -BUG?
« Reply #10 on: August 10, 2020, 01:43:24 am »
UPDATE :-[ :-[
Did not do enough testing. Glitches developed. Implemented all changes sugested, they werw all needed. !! 
Thanks again.

 

TinyPortal © 2005-2018