Recent

Author Topic: [SOLVED] How to move record in TDBLookupComboBox dataset - Lazarus 2  (Read 3664 times)

incendio

  • Sr. Member
  • ****
  • Posts: 269
Hi all,

I have 2 TDBLookUpComboBox, ComboA attached to TSQLQuery Qry1, ComboB, attached to Qry2.

Qry1 & Qry2 has a same sql statement.

On each combo, ScrolllistDataSet was set to True.

On ComboA OnChange event, I have this code
Code: Pascal  [Select][+][-]
  1. ComboB.KeyValue:= ComboA.KeyValue;

With this code, I hope that when user choose an item from ComboA, ComboB text shows the same item as ComboA, but it didn't.

Why ? Am I missing something here?
« Last Edit: February 13, 2019, 03:32:08 am by incendio »

GAN

  • Sr. Member
  • ****
  • Posts: 370
Re: How to move record by TDBLookupComboBox KeyValue - Lazarus 2
« Reply #1 on: February 12, 2019, 05:54:31 am »
Try ItemIndex

Code: Pascal  [Select][+][-]
  1. ComboB.ItemIndex:= ComboA.ItemIndex;

If dosn't work, use OnEditingDone method.
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: How to move record by TDBLookupComboBox KeyValue - Lazarus 2
« Reply #2 on: February 12, 2019, 06:04:28 am »
There is no ItemIndex property on TDBLookupComboBox.

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: How to move record by TDBLookupComboBox KeyValue - Lazarus 2
« Reply #3 on: February 12, 2019, 07:55:48 am »
I don't understand the endgame of this, but if both combo's use the same listsource then combo2 should show the same row as you selected in combo1 and without using any code at all. Query2 won't be used though, but since the sql statement is the same as in query1 maybe you dont need it. If you want to move to another row manually by code, you can do that by asking the query to move the row cursor, not by using the visual control. Check out Query2.Locate and Query2.Lookup.
« Last Edit: February 12, 2019, 08:01:21 am by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: How to move record by TDBLookupComboBox KeyValue - Lazarus 2
« Reply #4 on: February 12, 2019, 08:04:47 am »
Both combo did not use the same listsource.

ComboA has listsource qry1 and comboB has listsource qry2.

Usually using keyvalue works, but I don't know why this time did not work.

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: How to move record in TDBLookupComboBox dataset - Lazarus 2
« Reply #5 on: February 12, 2019, 10:24:02 pm »
It's nothing wrong with your code. I made a small testproject and found that DBLookupComboBox.OnChange doesn't fire when an item is selected. This must be a bug, I have never seen this happen until just now. You could use the DBLookupComboBox.OnCloseUp or Query1.AfterScroll while the bug is being fixed.

I reported it to the bugtracker, you can follow the progress here:
https://bugs.freepascal.org/view.php?id=35068
« Last Edit: February 12, 2019, 11:07:02 pm by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: How to move record in TDBLookupComboBox dataset - Lazarus 2
« Reply #6 on: February 12, 2019, 11:24:37 pm »
There were changes made with the COmboBox…

The Onchange would fire no matter what, even if you were still on the same selection and you clicked it
again.

 This comes from windows actually, there is no OnSelect or OnChange difference, when the user clicks on
an item it sends a OnChange message even if the item was already selected.

 If you use the OnSelect instead, this will trigger when you select an item from the list..

 As for the OnChange, maybe some code got mixed up but what should be happening is when text is actually
changed it should be triggered.

 Since windows always sends this message ether way, This has to be done in the widget to generate a
true OnChange.

 Its possible something went wrong between the widget sets when this change was made, I need to experiment
a little.

---Edit

I stand corrected, looking at the Release on Win32 2.0.0, OnChange works as it did before, it triggers no
matter what.

 BUt I do know that setting the index of the list via code does not trigger the Onchange, that has been fixed
because only user input should be doing that.

 Maybe its related to that or you are using a Trunk version that is being worked on, still.
« Last Edit: February 12, 2019, 11:39:59 pm by jamie »
The only true wisdom is knowing you know nothing

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: How to move record in TDBLookupComboBox dataset - Lazarus 2
« Reply #7 on: February 13, 2019, 02:49:21 am »
@jamie, Thanks for the info, it helped a lot. So if I try to sum it up.

Up to Lazarus 1.8.4 release the OnChange event was triggered for example by selecting items, changing ItemIndex or KeyValue.

But in Lazarus 2.0.0 and trunk DBComboBox.OnChange triggers only when a character is typed or pasted into the control, not changed from code or another item selected.

That means in a case like this that DBComboBox.OnSelect should be used instead? It triggers when a new item is selected, like OnChange used to do. (And doesn't trigger again if the item is already selected. At least not under Linux/GTK2, where in fact OnChange triggers only when text is changed via the visual control by typing or pasting but not by clicking)

EDIT: OnChange do fire by clicking, but the DBComboBox needs to have the datasource and datafield assigned. It's not enough to just assign a listsource and click items in the combobox.
« Last Edit: February 13, 2019, 04:58:25 pm by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: How to move record in TDBLookupComboBox dataset - Lazarus 2
« Reply #8 on: February 13, 2019, 03:31:41 am »
I add codes to OnChange & OnSelect, now it worked as expected, thanks guys.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: How to move record in TDBLookupComboBox dataset - Lazarus 2
« Reply #9 on: February 13, 2019, 03:31:50 am »
To Clarify more here.

  Some events have been canceled if you do things in code that would otherwise happen if the user did.

  Reason, Delphi compliant and it prevents loops and it also informs you that a USER did the trigger not your
code..

 But you can call the code if you think its safe directly from your code of course, so you are not lost, just need to
edit your code a little..

 you'll find that less automatic things happening gives you more control over events.

   
 
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018