Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

StringGrid question.

Posted on 2006-07-15
15
Medium Priority
?
463 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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