Recent

Author Topic: Active Control on a form  (Read 7538 times)

AKCarlow

  • New Member
  • *
  • Posts: 45
Active Control on a form
« on: August 13, 2014, 02:41:23 pm »
I have, inter alia, two TEdits on the form. When OnExit fires for TEDit2 (I'm tabbing out of it) I build their content into an SQL statement and execute it, then move the database on to the next entry. The last line of code sets TEdit1 as the active control, but if I type something it goes in TEdit2, not Tedit1 (named eNote in the code below).

I don't know if it is relevant, but the tab order is TEdit1 TEdit2.

Code: [Select]
   BuildSQL:= ... ;
   qReadings.Close;
   OldSQL:=qReadings.SQL.Text;
   qReadings.SQL.Text:=BuildSQL;
   qReadings.ExecSQL;
   qReadings.SQL.Text:=OldSQL;
   qConnections.Next; // the OnDataChange for qConnection's DS sets a parameter for qReadings then opens it
   fReadingsEntry.ActiveControl:=eNote;
Using: OS-X 10.6.8, Lazarus 1.2.4, FPC 2.6.4,  MySQL 5.6.19 (32-bit)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Active Control on a form
« Reply #1 on: August 13, 2014, 02:44:56 pm »
try
Code: [Select]
eNote.SetFocus;

AKCarlow

  • New Member
  • *
  • Posts: 45
Re: Active Control on a form
« Reply #2 on: August 13, 2014, 11:43:17 pm »
Thanks, but that didn't work. Both Tedits are highlighted, but typing goes to the 2nd, not to the 1st (eNote). I can't get out of the 2nd (eReading) without triggering its OnExit ...
Using: OS-X 10.6.8, Lazarus 1.2.4, FPC 2.6.4,  MySQL 5.6.19 (32-bit)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Active Control on a form
« Reply #3 on: August 14, 2014, 04:19:36 am »
Thanks, but that didn't work. Both Tedits are highlighted, but typing goes to the 2nd, not to the 1st (eNote). I can't get out of the 2nd (eReading) without triggering its OnExit ...

Code: [Select]
eReading.OnExit := nil;
eNote.SeFocus;
eReading.OnExit := @eReadingOnExit;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

AKCarlow

  • New Member
  • *
  • Posts: 45
Re: Active Control on a form
« Reply #4 on: August 14, 2014, 04:56:21 pm »
If that had worked, I would have asked "Why?".  :)

Background: Converting an old MSAccess database to MySQL and Lazarus. It is used for billing members of a coop on their water usage. The form is for entering meter readings. I've attached a screen shot of the form.

Set the date, then choose the area. Several dbTexts (here greyed out, as live info) display personal details from one query. The navigator can move through the query, and the datachange event updates the details in the dbGrid, showing historical data from another query for the selected member.

The wider of the two text fields is for notes ("damaged cover", "slight leak" ...) and the narrower for the latest reading. The idea is that the note field should have focus, and a tab will bring you to the reading field, type the reading then tab (onexit) inserts a record, and focus should be back in the notes field for the next member. But in fact focus is in the reading field, so if you've made a mistake you can't use the navigator to go back a member, then correct the error in the dbgrid.

If I put nothing in the notes field (which in practice would be typical), after the reading's onexit has run the cursor is flashing in the notes field, but typing still goes to the readings field.

Any ideas?
Using: OS-X 10.6.8, Lazarus 1.2.4, FPC 2.6.4,  MySQL 5.6.19 (32-bit)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Active Control on a form
« Reply #5 on: August 14, 2014, 05:23:05 pm »
You could also try
Code: [Select]
freadingsentry.FocusControl(eNote);
possibly preceded by freadingsentry.DefocusControl(...) depending on where the focus was previously.

However, moving focus programatically is very dependent on your GUI setup, and there is no one-solution-fits-all because several controls linked to data operations on a form can interact with each other in complex ways; and OS focus handling is not uniform across the platforms Lazarus supports.
If our suggestions don't work you may have to provide the full code/.lfm

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Active Control on a form
« Reply #6 on: August 14, 2014, 06:03:12 pm »
If that had worked, I would have asked "Why?".  :)

1) try after calling setfocus calling application.processMessages.
2) if that does not work try to postpone the onExit assignment and make it after all messages have been processed.

that it does not work makes me wander what the lcl developers implement.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

AKCarlow

  • New Member
  • *
  • Posts: 45
Re: Active Control on a form
« Reply #7 on: August 16, 2014, 01:35:45 am »
Thanks guys. Having tried all those suggestions, without cure, I stripped them all out, and the original "fReadingsEntry.ActiveControl:=eNote;", verified that the problem remained, and poked around. I found that I had left TabStop true on the Close button - setting it to the intended false appears to have fixed it. Now I type a note - or don't - tab to the readings field and type the reading, tab out, the OnExit executes, and control is back in the notes field.

The tab order is notes then readings, with all other controls now having tab stop false.

It might be helpful if the tab order viewer also showed a control's tab stop true or false setting.

Sorry for bothering you with something I should have found myself.
Using: OS-X 10.6.8, Lazarus 1.2.4, FPC 2.6.4,  MySQL 5.6.19 (32-bit)

 

TinyPortal © 2005-2018