Solved

StringGrid question.

Posted on 2006-07-15
15
452 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
Comment Utility
Focus another control on the form...
something like
Button1.SetFocus;
0
 
LVL 16

Author Comment

by:CodedK
Comment Utility
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
Comment Utility
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
Comment Utility

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

Russell
0
 
LVL 16

Author Comment

by:CodedK
Comment Utility
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
Comment Utility

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
Comment Utility
[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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Expert Comment

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

Russell
0
 
LVL 16

Author Comment

by:CodedK
Comment Utility
:)
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
Comment Utility

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
Comment Utility
Thank you very much Russell :)
:) :)
0
 
LVL 16

Author Comment

by:CodedK
Comment Utility
200 -> 500 :)
0
 
LVL 26

Expert Comment

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

Russell
0
 
LVL 16

Author Comment

by:CodedK
Comment Utility
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
Comment Utility
Nop it works as it is... My mistake... I had multiple errors and did some changes ...
Code is perfect.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

8 Experts available now in Live!

Get 1:1 Help Now