0

I encountered something really really really odd with TStringGrid (Delphi XE). I have seen that sometimes when I click the first line in my grid, it turns black (or shows scrambled canvas 'stolen' from other controls on form). It happens ONLY in certain configurations, when the grid receives focus. Once you click another area in the grid everything looks ok until the focus is moved to another TStringGrid.

How to reproduce:

  • put TWO string grids on a form
  • set them as shown below (Update: I realized that goRowSelect and goEditing must be 'true')
  • click the first cell in one grid -> nothing happens
  • click the first cell in the second grid -> the first cell gets black (see screenshot)

The problem appears also in other circumstances (not necessary to have 2 grids on a form), but I managed to reproduce it only when I have 2 grids.

object grid1: TStringGrid <------- same for Grid2
  Left = 2
  Top = 8
  Width = 422
  Height = 381
  BevelEdges = [beLeft, beTop]
  DefaultColWidth = 80
  DefaultRowHeight = 15
  DoubleBuffered = True
  FixedCols = 0
  Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goDrawFocusSelected, goColSizing, goEditing, goRowSelect, goFixedHotTrack]
  ParentDoubleBuffered = False
  TabOrder = 1
end

Any idea on how to fix this?

enter image description here

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • 1
    I can reproduce it with the given information. – Sertac Akyuz Jun 11 '14 at 13:20
  • Spent 2 hours tracking this down. I though it was a problem in my custom TStringGrid lib (especially the 'Draw' method). – Gabriel Jun 11 '14 at 13:21
  • @SertacAkyuz-which Delphi? – Gabriel Jun 11 '14 at 13:23
  • 1
    With XE. Does not seem to happen with XE2. – Sertac Akyuz Jun 11 '14 at 13:25
  • I could reproduce it partially with Delphi XE3 on Windows 7. In that cell I can see rendered themed header rectangle. And it happens only when I *focus* the control by clicking on the first column of the selected row. – TLama Jun 11 '14 at 13:27
  • Quick/dirty solution: Don't activate goRowSelect and goEditing at the same time (and remember it every time you use TStringGrid). Waiting for a better solution. – Gabriel Jun 11 '14 at 13:58
  • 2
    Options `goEditing` and `goRowSelect` are mutually exclusive. How would you edit the grid if you have selected whole row ? – TLama Jun 11 '14 at 14:29
  • @TLama If you want to have unique content for each cell in that row :D – Sir Rufo Jun 11 '14 at 14:33
  • @TLama-Yes, but the grid allows them both active at the same time. Which can lead to strange situations like this. Anyway, my 'bug' does not involve editing so, I don't know if these activating these two options together really causes the bug. – Gabriel Apr 11 '16 at 10:58

1 Answers1

3

I can reproduce your problem with XE3 as well. After I have reset ParentDoubleBuffered to True, the problem is gone.

Generally speaking, when I see odd black rectangle in a control, I will first check the ParentDoubleBuffered settings. I saw you have enabled double buffering for the two grids. Do you have any special reason to do that? If you intend to avoid flickering during resizing or cell update, there are some techniques helpful.

Community
  • 1
  • 1
stanleyxu2005
  • 8,081
  • 14
  • 59
  • 94
  • Setting ParentDoubleBuffered will override DoubleBuffered (more exactly the Grid will use DoubleBuffered of the parent). So, it is pointless when you set ParentDoubleBuffered to true but you don't tell us what is the DoubleBuffered value of your parent. We can only guess that the DoubleBuffered of the parent is False (since something changed). – Gabriel Apr 11 '16 at 11:04
  • Good to hear you back after a two year delay ;-) I cannot remember the exact reason. I guess my point was the Form has actually set `DoubleBuffered` as `True`. There is no need to set everything double buffered. – stanleyxu2005 Apr 11 '16 at 11:08
  • "There is no need to set everything double buffered" - Is this a personal opinion or it can be explained? I thought there is a purpose for which Borland put that property there... – Gabriel Apr 11 '16 at 12:25
  • So, basically you mean this is a Delphi bug ('undocumented feature' :) ). And the problem can ONLY be fixed by disabling the DoubleBuffered ? – Gabriel Aug 14 '16 at 08:43