Link to home
Start Free TrialLog in
Avatar of nhomc
nhomc

asked on

how to make stringgrid's selection invisible?

When I click a stringgrid's cell,it will select the cell,and  the cell will turn blue.Now I don't want the cell turn blue.how can I do it?
Avatar of ZifNab
ZifNab

Hi nhomc,

 Grid1.Options := Grid1.Options - [goDrawFocusSelected];

Regards, Zif.

It's worth looking at these free components :

 TStringGrid of RX-Lb (http://rx.demo.ru)
 THyperGrid/TSuperGrid of Marly (http://www.pablop.demon.co.uk/)
Avatar of nhomc

ASKER

ZifNab:
   I'm sorry,it doesn't work,in my program,the goDrawFocusSelected option sets false,so when I use Grid1.Options := Grid1.Options - [goDrawFocusSelected],it has no change.But when I click another object that is  not in the stringgrid,the blue rectangle in the selected cell appears,
Now I  want the blue rectangle doesn't appear forever,how can I do that?
Avatar of nhomc

ASKER

ZifNab:
   I'm sorry,it doesn't work,in my program,the goDrawFocusSelected option sets false,so when I use Grid1.Options := Grid1.Options - [goDrawFocusSelected],it has no change.But when I click another object that is  not in the stringgrid,the blue rectangle in the selected cell appears,
Now I  want the blue rectangle doesn't appear forever,how can I do that?
Do the same thing as ZifNab told you, but do it before you compile the program not in run time like so....Before you run the app: Select the StringGrid, Go to its properties and double click on Options, then choose  goDrawFocusSelected := False;.....That's before you run your app, if you want to change that in run-time, just enter this somewhere...
Grid1.Options := Grid1.Options + [goDrawFocusSelected];
That's all you need to do...

Regards,
Viktor Ivanov

Set the defaultDrawing property to false and write your own onDrawCell procedure, this is the most flexible way as you it enable you to choose us the color you like. For example, you may change the cell text into read color (white backgroup) when the grid is not in focus. You can also subclass TstringGrid and overwrite the drawCell procedure.
Let me know if you need sample code. I will modify my code for you if need be.
What's wrong with my answer?I tried it and it works????

Regards,
Viktor Ivanov
Matvey: using your method, the selected cell would be highlighted again when you tab to other component in the same form ie when the grid is not in focus.

nhomc:
{
Set the defaultDrawing property to false and write your own onDrawCell procedure as follows:
}
procedure TForm1.Grid1DrawCell(Sender: TObject; Col, Row: Longint;  Rect: TRect; State: TGridDrawState);
var str0: array[0..255] of char; textAlign: word;
begin
  with grid1.canvas do begin
    if ( gdFixed in state)  then
      brush.color:= grid1.fixedColor
    else if ( gdSelected in State) then begin
      brush.color:= clWindow;
      if ( grid1.focused) then
        grid1.canvas.font.color:= clBlue
      else
        grid1.canvas.font.color:= clRed;
    end else begin
      font.color:= clBlack;
      brush.color:= clWindow;
    end;
    strPCopy( str0, grid1.cells[ col, row] );
    fillRect( rect);
    textAlign:= DT_LEFT or DT_SINGLELINE OR DT_VCENTER;
    drawText( handle, str0, -1, rect, textAlign);
  end;
end;
Avatar of nhomc

ASKER

  I'm sorry,it doesn't work,in my program,the goDrawFocusSelected option sets false,so when I use Grid1.Options := Grid1.Options - [goDrawFocusSelected],it has no change.But when I click another object that is  not in the stringgrid,the blue rectangle in the selected cell appears,
Now I  want the blue rectangle doesn't appear forever,how can I do that?
Avatar of nhomc

ASKER

When I set goDrawFocusSelected to false,if I click the stringgrid, the blue rectangle doesn't appear in the stringgrid.
But when I click another object that is not in the stringgrid,
such as a button,the blue rectangle appear in the stringgrid.
Now I want the blue rectangle doesn't appear forever,
How can I do It?

ASKER CERTIFIED SOLUTION
Avatar of vladika
vladika

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
nhomc: did you try my code? what's wrong with it?