[SOLVED] TBitBtn On Click not triggered lazarus / 31-LCL ---------------------------------------------------------------------- incendio · 2022-06-19 04:57 Hi guys, I have TDBGrid and TBitBtn on a form. Users can directly input/edit data on TDBGrid. When cursor on TDBGrid is on Column Disc(Price) and users pressed ENTER key, I set codes to append dataset underlying TDBGrid and moved cursor to Column Code. The display look like attachment SS1.png. This the problem occurs, when users clicked Save button,  the display will turn into SS2.png and event click for that button was not triggered. If I change TBitBtn to TEdit or TLabel, when users clicked this TEdit or TLabel, it worked as expected. Can this problem with TBitbtn be solved? ---------------------------------------------------------------------- bobby100 · 2022-06-19 08:37 There is something "fishy" about TBitBtn. I also have some weird behavior here: https://forum.lazarus.freepascal.org/index.php/topic,58357.msg445006.h tml#msg445006  (Problem Nr.1) I suspect it has to do with ModalResult, but I don't know how to inspect it ---------------------------------------------------------------------- jamie · 2022-06-19 12:59 i guess a couple of question are in order.  This is a Windows managed control which means the up/down and click are messaged pooled.  Are you doing any code within the OnDown or OnUp messages of the button ?  Those two events must property fire before a click event is seen. If for example you hide the control in the keyup event it could be possible that no click will ever arrive.  The same is true for the Keydown etc.   Graphic buttons are handled slightly different. ---------------------------------------------------------------------- incendio · 2022-06-20 02:17 Quote from: jamie on June 19, 2022, 02:59:46 pmi guess a couple of question are in order.  This is a Windows managed control which means the up/down and click are messaged pooled.  Are you doing any code within the OnDown or OnUp messages of the button ?  Those two events must property fire before a click event is seen. If for example you hide the control in the keyup event it could be possible that no click will ever arrive.  The same is true for the Keydown etc.   Graphic buttons are handled slightly different. TBitbtn only has codes in OnClick event. I tried to add codes to OnKeyDown, this event also not triggered. ---------------------------------------------------------------------- dje · 2022-06-20 03:03 How do you know the OnClick is not triggered? Try adding a fresh TBitBtn with a new OnClick event and confirm using ShowMessage('Clicked'); Have you tried a standard TButton? Again, try a fresh instance. It looks looks like focus shifting is cancelling the append state for the TDataSet. Which is standard. Without an example project, I can't offer much more, but, often if strange things are happening, I start commenting out code to bring some visible sanity back to a project. ---------------------------------------------------------------------- incendio · 2022-06-20 03:12 Quote from: derek.john.evans on June 20, 2022, 05:03:47 amHow do you know the OnClick is not triggered? Try adding a fresh TBitBtn with a new OnClick event and confirm using ShowMessage('Clicked'); Have you tried a standard TButton? Again, try a fresh instance. It looks looks like focus shifting is cancelling the append state for the TDataSet. Which is standard. Without an example project, I can't offer much more, but, often if strange things are happening, I start commenting out code to bring some visible sanity back to a project. Added a fresh TBitbtn Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrm.BitBtn1Click(Sender: TObject);begin  ShowMessage('Hi');end;  Message not showed, happen just like post # 1. TButton also didn't work, but TEdit and TLabel worked fine. ---------------------------------------------------------------------- wp · 2022-06-20 07:38 Please prepare a small, simplified project which shows the issue, and upload it here. Without it we can only guess wildly. ---------------------------------------------------------------------- incendio · 2022-06-21 04:42 Quote from: wp on June 20, 2022, 09:38:20 amPlease prepare a small, simplified project which shows the issue, and upload it here. Without it we can only guess wildly. I tried to create simply project, but have a problem, posted on here : https://forum.lazarus.freepascal.org/index.php/topic,59690.0.html ---------------------------------------------------------------------- Handoko · 2022-06-21 05:35 I tested the project your posted on that thread. No problem found. My guess is you're having problem with your Lazarus installation or your Windows or your hardware. ---------------------------------------------------------------------- jipété · 2022-06-21 11:02 Quote from: incendio on June 21, 2022, 06:42:11 amQuote from: wp on June 20, 2022, 09:38:20 amPlease prepare a small, simplified project which shows the issue, and upload it here. Without it we can only guess wildly. I tried to create simply project, but have a problem, posted on here : https://forum.lazarus.freepascal.org/index.php/topic,59690.0.html The project you're talking about doesn't use TBitBtn...  We need a small, simplified project which shows the issue ---------------------------------------------------------------------- incendio · 2022-06-22 03:09 Hi guys, Attached is the sample project. It use sqlite database, don't know why column with Text type in TDGrid displayed as [MEMO], never use sqlite. In that project, you will find TBitButtton, TButton, & TEdit. OnClick event only triggered in TEdit. PS : Using Lazarus 2.0.12 32 bit on Windows 10 64 bit ---------------------------------------------------------------------- Handoko · 2022-06-22 04:01 Quote from: incendio on June 22, 2022, 05:09:54 amIn that project, you will find TBitButtton, TButton, & TEdit. OnClick event only triggered in TEdit. No, everything is working correctly, see the picture below. As I already told you, there is something wrong with your OS or virus/antivirus or security settings or your Lazarus installation. You should fix that issue first. Quote from: incendio on June 22, 2022, 05:09:54 amIt use sqlite database, don't know why column with Text type in TDGrid displayed as [MEMO], never use sqlite. I think you're too lazy to read the documentation. Database is my weakest skill in programming and this is the first time I use SQLite. But reading the documentation, I can understand how to make the memo columns to show their contents: https://lazarus-ccr.sourceforge.io/docs/lcl/dbgrids/tdbgrid.html Sorry I won't tell you how to do it, read the Options > TDbGridOptions yourself if you want to know. ---------------------------------------------------------------------- incendio · 2022-06-22 04:25 Quote from: Handoko on June 22, 2022, 06:01:38 amQuote from: incendio on June 22, 2022, 05:09:54 amIn that project, you will find TBitButtton, TButton, & TEdit. OnClick event only triggered in TEdit. No, everything is working correctly, see the picture below. As I already told you, there is something wrong with your OS or virus/antivirus or security settings or your Lazarus installation. You should fix that issue first. Quote from: incendio on June 22, 2022, 05:09:54 amIt use sqlite database, don't know why column with Text type in TDGrid displayed as [MEMO], never use sqlite. I think you're too lazy to read the documentation. Database is my weakest skill in programming and this is the first time I use SQLite. But reading the documentation, I can understand how to make the memo columns to show their contents: https://lazarus-ccr.sourceforge.io/docs/lcl/dbgrids/tdbgrid.html Sorry I won't tell you how to do it, read the Options > TDbGridOptions yourself if you want to know. Please test this : 1. Put cursor on column id, type any number then press ENTER until cursor move to column num1 2. type any number on column num1, then press ENTER, Grid will add new row and cursor will move to column id 3. Press BitButton or Button, is it work? ---------------------------------------------------------------------- Handoko · 2022-06-22 04:28 The buttons still work. The new row will be cancelled then button's event triggered. Tested on both TBitButton and TButton. ---------------------------------------------------------------------- Handoko · 2022-06-22 04:39 I'm very curious why you got that weird 'things'. I still believe there's something wrong on your system or hardware. I have TeamViewer installed on my Linux system. If you want, I can let you 'use' my computer to test. PM me if you want. I see you're building an inventory/POS system, maybe I can learn one or two tricks from you. ---------------------------------------------------------------------- incendio · 2022-06-22 04:45 Quote from: Handoko on June 22, 2022, 06:28:10 amThe buttons still work. The new row will be cancelled then button's event triggered. Tested on both TBitButton and TButton. What's version of Lazarus you are using? ---------------------------------------------------------------------- Handoko · 2022-06-22 04:46 Lazarus 2.2.0 64-bit GTK2 FPC 3.2.2, Ubuntu Mate. ---------------------------------------------------------------------- wp · 2022-06-22 04:56 Quote from: incendio on June 22, 2022, 05:09:54 amAttached is the sample project. The project cannot be run because it is lacking the database file (your G:\Data\Lazarus\Sq\test). ---------------------------------------------------------------------- Handoko · 2022-06-22 05:16 Change that location to the extracted folder and select the file named test. ---------------------------------------------------------------------- wp · 2022-06-22 05:25 Ah, normally I see these left-overs. Not my best day today... ---------------------------------------------------------------------- incendio · 2022-06-22 06:18 Quote from: Handoko on June 22, 2022, 06:28:10 amThe buttons still work. The new row will be cancelled then button's event triggered. Tested on both TBitButton and TButton. This is very strange, I don't use Lazarus very often and don't remember messing around with already installed Lazarus. Will try to install latest version of Lazarus. ---------------------------------------------------------------------- wp · 2022-06-22 07:12 I see that this behaviour is a bit strange, but on the other hand your code is setting the dataset into edit mode, and this has a life of its own because the first priority here is to end or cancel edit mode. In case of the grid, edit mode ends when another record is selected, or when the grid loses focus. And this is what the click on BitBtn1 is doing: the grid gives focus to the bitbtn, and this posts the record. This way the OnClick event of the button seems to be swallowed. I am not sure if this is a bug or not... But I am rather sure that attempting to change this behaviour very probably will introduce another issue somewhere else because the data-aware components are very sophisticated. But I don't see the need for the BitBtn1 at all. As I said: saving of records is handled by the dataset internally, normally when another grid row or another control becomes active. There are only very few situations when it is required to call dataset.Post. And there is also no need to explicitly call dataset.Append from a DBGrid because when you press ENTER in the last field a new empty row will be appended automatically. As a test, remove the GridKeyDown method and run the demo. Begin with the first field in the first row, enter something, press ENTER. The second cell becomes active, enter something, press ENTER (you will have to add dgDisplayMemo to the grid's Options for being able to enter something into a memo field). Proceed in the same way with the other cells until you reach the last cell. When you now press ENTER a new empty row will be added for the next record - there's really no need for you to code this behaviour! ---------------------------------------------------------------------- incendio · 2022-06-22 07:21 Quote from: wp on June 22, 2022, 09:12:09 amI see that this behaviour is a bit strange, but on the other hand your code is setting the dataset into edit mode, and this has a life of its own because the first priority here is to end or cancel edit mode. In case of the grid, edit mode ends when another record is selected, or when the grid loses focus. And this is what the click on BitBtn1 is doing: the grid gives focus to the bitbtn, and this posts the record. This way the OnClick event of the button seems to be swallowed. I am not sure if this is a bug or not... But I am rather sure that attempting to change this behaviour very probably will introduce another issue somewhere else because the data-aware components are very sophisticated. But I don't see the need for the BitBtn1 at all. As I said: saving of records is handled by the dataset internally, normally when another grid row or another control becomes active. There are only very few situations when it is required to call dataset.Post. And there is also no need to explicitly call dataset.Append from a DBGrid because when you press ENTER in the last field a new empty row will be appended automatically. 1. Did you experience the same problem with mine, that OnClick event is not triggered on TBitBtn? 2. If I don't call dataset.Append, empty row will not appended automatically because in this sample project, last field is column ttl, and I want add a row when users pressed ENTER in column num1. 3. This is a simple project that don't need BitBtn to post the data, but on the real project, such need is exist. ---------------------------------------------------------------------- wp · 2022-06-22 07:27 Quote from: incendio on June 22, 2022, 09:21:44 am1. Did you experience the same problem with mine, that OnClick event is not triggered on TBitBtn?Yes. Quote from: incendio on June 22, 2022, 09:21:44 am2. If I don't call dataset.Append, empty row will not appended automatically because in this sample project, last field is column ttl, and I want add a row when users pressed ENTER in column num1. Sorry, this is very inconvenient for a user pressing ENTER after each cell input. How is he supposed to enter a value into the last cell then? Make the num1 column the last column in the grid. BTW: What is BitBtn1 supposed to do in the end? ---------------------------------------------------------------------- incendio · 2022-06-22 08:27 Quote from: wp on June 22, 2022, 09:27:59 amQuote from: incendio on June 22, 2022, 09:21:44 am1. Did you experience the same problem with mine, that OnClick event is not triggered on TBitBtn?Yes. Quote from: incendio on June 22, 2022, 09:21:44 am2. If I don't call dataset.Append, empty row will not appended automatically because in this sample project, last field is column ttl, and I want add a row when users pressed ENTER in column num1. Sorry, this is very inconvenient for a user pressing ENTER after each cell input. How is he supposed to enter a value into the last cell then? Make the num1 column the last column in the grid. BTW: What is BitBtn1 supposed to do in the end? So, the problem, probably in Lazarus, may I know what is the ver of Lazarus you use? The last column, in real project, is a calculated field. BitBtn is used to Apply Update. ---------------------------------------------------------------------- incendio · 2022-06-22 08:38 Quote from: Handoko on June 22, 2022, 06:39:16 am I see you're building an inventory/POS system, maybe I can learn one or two tricks from you. You are welcome, pm or post questions about what you want to know, I will answer if I can ---------------------------------------------------------------------- wp · 2022-06-22 10:03 Quote from: incendio on June 22, 2022, 10:27:55 amBitBtn is used to Apply Update. But then it does not make sense to click this "Apply update" button from that column in which you just called dataset.Append. When you want to call ApplyUpdates each time a record is posted you should call it in the dataset event AfterPost. ---------------------------------------------------------------------- dseligo · 2022-06-22 10:39 Quote from: incendio on June 22, 2022, 08:18:36 amQuote from: Handoko on June 22, 2022, 06:28:10 amThe buttons still work. The new row will be cancelled then button's event triggered. Tested on both TBitButton and TButton. This is very strange, I don't use Lazarus very often and don't remember messing around with already installed Lazarus. Will try to install latest version of Lazarus. No need to install latest version of Lazarus. I have latest version and I can confirm this behaviour on Windows 11 - neither TButton nor TBitBtn works. I tried BCButtonFocus from BGRA Controls and it works, so you can use button from some other package. I suggest you make a bug report. ---------------------------------------------------------------------- dseligo · 2022-06-22 10:46 Quote from: wp on June 22, 2022, 09:12:09 amI see that this behaviour is a bit strange, but on the other hand your code is setting the dataset into edit mode, and this has a life of its own because the first priority here is to end or cancel edit mode. In case of the grid, edit mode ends when another record is selected, or when the grid loses focus. And this is what the click on BitBtn1 is doing: the grid gives focus to the bitbtn, and this posts the record. This way the OnClick event of the button seems to be swallowed. I am not sure if this is a bug or not... But I am rather sure that attempting to change this behaviour very probably will introduce another issue somewhere else because the data-aware components are very sophisticated. And why it works then if you click in Edit1? It also cancels edit mode and it triggers OnClick event of Edit1. I think it is a bug. Quote from: wp on June 22, 2022, 09:12:09 amBut I don't see the need for the BitBtn1 at all. This is sample project. Maybe in final application he'll print records or update some other table based on rows he just inputted. ---------------------------------------------------------------------- wp · 2022-06-22 12:07 Well, maybe my description is not correct, it's a very complicated flow of events. Following the keypress with the debugger I saw that further code is executed after the OnKeyDown handler. Probably this should not be done, and I had the idea to "eat" the RETURN key by inserting "Key := 0" after "Qry.Append" - now the OnClick events of the Button and BitBtn are fired correctly. ---------------------------------------------------------------------- Thaddy · 2022-06-22 12:24 OnKeyDown needs to execute as much as possible, because it needs to maintain state. It is often misused and misunderstood. Usually it can be replaced with OnKeyUp which is a real single fire event. So no wonder OnKeyDown has more code... It should have more..... Not understanding the difference between the two is a very common design flaw. E.g. with ctrl-- to navigate, OnKeyDown is the option. Otherwise use OnKeyUp. LCL is literaly littered with that mistake. Even Delphi has a couple of issues, but far less so. ---------------------------------------------------------------------- Handoko · 2022-06-22 15:05 Quote from: wp on June 22, 2022, 09:27:59 amQuote from: incendio on June 22, 2022, 09:21:44 am1. Did you experience the same problem with mine, that OnClick event is not triggered on TBitBtn?Yes. I didn't have that problem, it worked correctly on Linux. Here I created a short video: https://imgur.com/a/n6qtAPJ (Because of the limitation of uploaded file size here, I have to use online file sharing service). ---------------------------------------------------------------------- dseligo · 2022-06-22 19:32 Quote from: Handoko on June 22, 2022, 05:05:57 pmQuote from: wp on June 22, 2022, 09:27:59 amQuote from: incendio on June 22, 2022, 09:21:44 am1. Did you experience the same problem with mine, that OnClick event is not triggered on TBitBtn?Yes. I didn't have that problem, it worked correctly on Linux. Here I created a short video: https://imgur.com/a/n6qtAPJ (Because of the limitation of uploaded file size here, I have to use online file sharing service). Since on Windows works differently than you posted I'd say it's definitely a bug. ---------------------------------------------------------------------- dseligo · 2022-06-22 19:37 Quote from: wp on June 22, 2022, 02:07:50 pmWell, maybe my description is not correct, it's a very complicated flow of events. Following the keypress with the debugger I saw that further code is executed after the OnKeyDown handler. Probably this should not be done, and I had the idea to "eat" the RETURN key by inserting "Key := 0" after "Qry.Append" - now the OnClick events of the Button and BitBtn are fired correctly. This is interesting. But why some controls (TEdit and TBCButtonFocus, e.g.) works without it, and others needs 'Key := 0'? ---------------------------------------------------------------------- incendio · 2022-06-23 02:17 Quote from: wp on June 22, 2022, 02:07:50 pmWell, maybe my description is not correct, it's a very complicated flow of events. Following the keypress with the debugger I saw that further code is executed after the OnKeyDown handler. Probably this should not be done, and I had the idea to "eat" the RETURN key by inserting "Key := 0" after "Qry.Append" - now the OnClick events of the Button and BitBtn are fired correctly. I choose this method to solve the problem  Quote from: dseligo on June 22, 2022, 09:32:11 pmSince on Windows works differently than you posted I'd say it's definitely a bug. Agreed. I will make a bug report.