Solved

StringGrid question.

Posted on 2006-07-15
15
458 Views
Last Modified: 2010-04-05
Hi.

When i open my application 1st stringgrid cell is always selected.
How can i avoid that ?

Thanks in advance.
0
Comment
Question by:CodedK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
15 Comments
 
LVL 11

Expert Comment

by:calinutz
ID: 17114510
Focus another control on the form...
something like
Button1.SetFocus;
0
 
LVL 16

Author Comment

by:CodedK
ID: 17114554
Hi calinutz.
This doesnt work !

The problem is that i have painted the cells with some colors i like...
If a cell is focused then the focused cell font color is painted white
and the user cant read anything.

0
 
LVL 16

Author Comment

by:CodedK
ID: 17114564
Another problem... there isnt any control on this form to focus...
I tried to set the focus on the form. Didnt work.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Russell Libby
ID: 17114587

Could we see the code for the painting routine (guessing you are using the OnDrawCell event)?

Russell
0
 
LVL 16

Author Comment

by:CodedK
ID: 17114593
Hi Russell.

On StringGrid1DrawCell

If (ARow mod 2)=0 then
  begin
  StringGrid1.Canvas.Brush.Color:= RGB(226,226,226);//RGB(245,147,51);
  StringGrid1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, StringGrid1.Cells[ACol, ARow]);
  end else
  begin
  StringGrid1.Canvas.Brush.Color:= RGB(207,241,87);//RGB(156,241,93);
  StringGrid1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, StringGrid1.Cells[ACol, ARow]);
  end;
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17114755

The problem is that with the OnDrawCell event is skipped for the cell that has edit focus. (eg you might have  [goEditing, goAlwaysShowEditor] set). Also keep in mind that the cell with edit focus is actually displaying an edit control over the grid cell,
so even if you could paint the cell it would not do you any good, as there is an edit control over it. I am looking at a way to paint the inplace editor, but you may want to consider changing the font color (knowing that you will be dealing with an edit contol)

Russell
0
 
LVL 16

Author Comment

by:CodedK
ID: 17114769
[goEditing, goAlwaysShowEditor] are set to false.
User wont edit anything... Font color is set to black, i've tried blue.... but what ever font color i choose
there is always a row selected with white non readable letters. :/

Thank you :)
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17114788
Almost have the editor super classed and painted, let you know in a minute.

Russell
0
 
LVL 16

Author Comment

by:CodedK
ID: 17114789
:)
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17114810

Try the following  code, if it does not work then please post a simple example that reproduces the problem along with the dfm listing

Russell

---

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids;

type
  TForm1            =  class(TForm)
     StringGrid1:   TStringGrid;
     procedure      StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
     procedure      FormCreate(Sender: TObject);
     procedure      StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
  private
     // Private declarations
     procedure      UpdateEditor;
  public
     // Public declarations
  end;

type
  TSuperCustomGrid  =  class(TCustomGrid); // We need to access the protected props

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.UpdateEditor;
begin

  // Update editor if assigned
  if Assigned(TSuperCustomGrid(StringGrid1).InPlaceEditor) then
  begin
     if (StringGrid1.Row mod 2) = 0 then
        TWinControl(TSuperCustomGrid(StringGrid1).InPlaceEditor).Brush.Color:=RGB(226,226,226)
     else
        TWinControl(TSuperCustomGrid(StringGrid1).InPlaceEditor).Brush.Color:=RGB(207,241,87);
  end;

end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin

  // Check row
  if (ARow mod 2) = 0 then
     StringGrid1.Canvas.Brush.Color:=RGB(226,226,226)
  else
     StringGrid1.Canvas.Brush.Color:=RGB(207,241,87);
  // Clear rect
  StringGrid1.Canvas.FillRect(Rect);
  // Make sure color is not changed if selected
  StringGrid1.Canvas.Font.Color:=StringGrid1.Font.Color;
  // Draw text
  StringGrid1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, StringGrid1.Cells[ACol, ARow]);

end;

procedure TForm1.FormCreate(Sender: TObject);
var  dwRows, dwCols:      Integer;
begin

  // Update editor
  UpdateEditor;

  // Add some sample data
  for dwRows:=0 to Pred(StringGrid1.RowCount) do
  begin
     for dwCols:=0 to Pred(StringGrid1.ColCount) do
        StringGrid1.Cells[dwCols, dwRows]:=Format('%d%d', [dwRows, dwCols]);
  end;
 
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
begin

  // Update editor if needed
  UpdateEditor;

end;

end.
0
 
LVL 16

Author Comment

by:CodedK
ID: 17115072
Thank you very much Russell :)
:) :)
0
 
LVL 16

Author Comment

by:CodedK
ID: 17115087
200 -> 500 :)
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17115096
Thank you as well. Its working as expected for you, I hope?

Russell
0
 
LVL 16

Author Comment

by:CodedK
ID: 17115106
Yes perfect...

I only changed this :

type
  TSuperCustomGrid  =  class(TCustomGrid);

to this :

type
  TSuperCustomGrid  =  class(TCustomGrid)
end;

Dont know why it didnt work without this change :/
0
 
LVL 16

Author Comment

by:CodedK
ID: 17115112
Nop it works as it is... My mistake... I had multiple errors and did some changes ...
Code is perfect.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question