Solved

StringGrid question.

Posted on 2006-07-15
15
454 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now