You could use the OnSetText event to measure the text, using the
DrawTexr API function, and adjust the cell height if needed. I don't
know if that has changed in D6: in D5 the inplace editor did not cope
at all well with cells higher than one line.
Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.
Peter Below (TeamB) <10011...@compuXXserve.com> wrote in article
<VA.000079a...@antispam.compuserve.com>...
right.
Thanks dude.
Peter Below (TeamB) <10011...@compuXXserve.com> wrote in article
<VA.000079b...@antispam.compuserve.com>...
My method is to make the stringGrid readonly and bring up my own
secondary form as an editor, but I'd like it to autocalculate the new
rowheight based on the lines of text after posting. Is there a way to make
that happen anyone?
"rich" <ri...@alltel.net> wrote in message
news:01c145ef$ab742360$0200a8c0@p133...
It does not wrap text automatically but it is a multiline edit control
under the hood, you can use Ctrl-Enter to insert hard linebreaks into it.
It is possible to make a derived grid with an overridden CreateEditor method
to use a derivative of TInplaceEdit that works much like a TMemo, but i found
in a project i did a couple of years ago that it still did not work quite
right for a cell editor. If i remember correctly it had some weird scrolling
problems. Since it wasn't that important (and i'm of the opinion that a grid
is a poor input device anyway) i scrapped the idea at that time.
> My method is to make the stringGrid readonly and bring up my own
> secondary form as an editor, but I'd like it to autocalculate the new
> rowheight based on the lines of text after posting. Is there a way to make
> that happen anyone?
>
It is fairly easy to increase the row height as needed, e.g. using a
OnDrawCell event:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
Rect: TRect; State: TGridDrawState);
var
S: String;
drawrect :trect;
begin
S:= (Sender As TStringgrid).Cells[ Col, Row ];
If Length(S) > 0 Then Begin
drawrect := rect;
DrawText((Sender As TStringgrid).canvas.handle,
Pchar(S), Length(S), drawrect,
dt_calcrect or dt_wordbreak or dt_left );
If (drawrect.bottom - drawrect.top) >
(Sender As TStringgrid).RowHeights[row]
Then
(Sender As TStringgrid).RowHeights[row] :=
(drawrect.bottom - drawrect.top)
Else Begin
drawrect.Right := rect.right;
(Sender As TStringgrid).canvas.fillrect( drawrect );
DrawText((Sender As TStringgrid).canvas.handle,
Pchar(S), Length(S), drawrect,
dt_wordbreak or dt_left);
End;
End;
end;
It will automatically adjust the row height to larger values if needed.
The main problem is that it will not automatically *decrease* the row height
if the text would fit into a smaller row. It cannot do that since there may be
other cells in the row that need a taller row. You could fix that problem by
setting the rowheigh back to the defaultrowheight when you change the cell
data for a row, that would trigger a redraw and that in turn would adjust the
rowheight to what is needed. It would cause some flicker, though.
Good point about the secondary ramifications of having such a mechanism
(such as all the cells in a row competing for the proper grid row height).
I'm working on a Calendar/Scheduler and this would only affect the week
view, but I guess I'll have to use a seperate StringGrid for each Day if I
want this effect there. This will work find for the single day view, since
that's only got two columns. I think your idea about re-triggering the
redraw after resetting to defaultrowheigh is a good one, and will try it.
"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote in message
news:VA.000079f...@antispam.compuserve.com...
Yes, if you override the CreateParams procedure and XOR out
ES_AUTOHSCROLL. This will turn off automatic horizontal scrolling and
the edit control will then wordwrap.
I tried your code and it worked great. You were right in that it doesn't
shrink back down if new text takes up fewer lines. I'm a little confused as
to why that is. Doesn't the Drawtext reset the size of the new rect
parameter based on the number of lines determined by CALCRECT? I rem'med out
the following lines in the code. It still worked the same and still won't
shrink back down if the new text takes up fewer lines. It also still works
beautifully if the previous rect size is smaller than the new text. I'm sure
your idea about resetting to the defaultrowheight would work, but could you
explain what is happening here?:
DrawText((Sender As TStringgrid).canvas.handle,
Pchar(S), Length(S), drawrect,
dt_calcrect or dt_wordbreak or dt_left );
// If (drawrect.bottom - drawrect.top) >
// (Sender As TStringgrid).RowHeights[row]
// Then
(Sender As TStringgrid).RowHeights[row] :=
(drawrect.bottom - drawrect.top)
// Else Begin
// drawrect.Right := rect.right;
Thanks for all the help to everyone who responded.
DJSmith
"David J. Smith" <djsm...@pacbell.net> wrote in message
news:3bb4fc07$1_2@dnews...
It will only *increase* the height of the rect it is passed, not decrease it.
Try to set drawrect.bottom := drawrect.top before the call to DrawText.
"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote in message
news:VA.00007a8...@antispam.compuserve.com...
Well, it is somewhat implicit in the docs for the DT_CALCRECT flag
from MSDN Library:
DT_CALCRECT
Determines the width and height of the rectangle. If there are multiple
lines of text, DrawText uses the width of the rectangle pointed to by
the lpRect parameter and extends the base of the rectangle to bound the
last line of text. If there is only one line of text, DrawText modifies
the right side of the rectangle so that it bounds the last character in
the line. In either case, DrawText returns the height of the formatted
text but does not draw the text.
Note that it says "extends the base", which kind of implies that it
will not reduce the height of the rect....
Dave Smith
"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote in message
news:VA.00007a9...@antispam.compuserve.com...