Recent

Author Topic: Spawning a form from a TDBGrid OnCellClick freezes app  (Read 330 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 270
Spawning a form from a TDBGrid OnCellClick freezes app
« on: July 15, 2019, 06:05:12 am »

Platform:  Lazarus 1.8.4  FPC 3.0.4  OS: Windows Server 2016

I'm building a TDBgrid that has about 20 columns.  The expected behavior is that the user clicks on cell at row one, col one, and Form1 is spawned and .ShowModal 'd  . The user closes Form1.  Then the user clicks on cell at row one, col two, and Form2 is created and .ShowModal'd.  The grid has a OnPrepareCanvas event, and OnCellClick event.

I've built this and when I click on any of the cells, the form I want is displayed, but the app freezes.  The spawned window won't close, it's completely frozen.  I did some experimenting and changed ShowModal to just Show, and the behavior then is, the window is barely displayed, for about 2/100th of a second, you can just barely see the outline of the window then it disappears completely.  It is not behind the main form, it's just gone.

I suspect there is some processing of the grid's OnPrepareCanvas, or OnCellClick events that "wants" to happen but a modal window is preventing it from completing it's tasks, so the app freezes.

What is the correct way to accomplish the opening of a window (form), using ShowModal (Modal), from a TDBGrid, so that the app won't freeze?

Thanks in advance for any help you can provide.


zeljko

  • Hero Member
  • *****
  • Posts: 1079
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Spawning a form from a TDBGrid OnCellClick freezes app
« Reply #1 on: July 15, 2019, 09:13:36 am »
Without code example nobody can help you.

wp

  • Hero Member
  • *****
  • Posts: 6309
Re: Spawning a form from a TDBGrid OnCellClick freezes app
« Reply #2 on: July 15, 2019, 12:19:14 pm »
I am attaching a small demo constructed according to you description, and it works absolutely as expected. So, you must be doing something differently. Without source code it is impossible to give you further advice.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

RedOctober

  • Sr. Member
  • ****
  • Posts: 270
Re: Spawning a form from a TDBGrid OnCellClick freezes app
« Reply #3 on: July 16, 2019, 01:23:43 am »
Thank you wp.  There was a couple of things going on in my project. 

First, you cannot create a form and free it, within the OnCellClick event of a TDBGrid.  I changed my forms to being created when my main form is created, and that solved part of the freezing problem.  The next thing I did wrong was, instead of using (like you)

Code: Pascal  [Select]
  1.   if {condition} then DBGrid1.Canvas.Brush.Color := clMoneyGreen;
  2.  

I instead was using the following craziness:

Code: Pascal  [Select]
  1.   if {condition} then Column.Color := clMoneyGreen;
  2.  


I guess at some point I typed in "Column" and I saw the ".Color" property and decided "Hey, this must be how to set colors".  It actually "sort of" works for a while, then your project falls apart in smoking ruins.  Better to use the grid's canvas and brush as you demonstrated.

Thanks to all, I hope this helps someone in the future.