Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TInplaceEdit

106 views
Skip to first unread message

rich

unread,
Sep 23, 2001, 5:21:53 PM9/23/01
to
Is there a way to get the TStringGrid cell to resize itself, so that
all the text entered in a multi-line InplaceEdit will be visible
during editing? Kind of like an "autosize" type thing.

Peter Below (TeamB)

unread,
Sep 24, 2001, 3:09:35 PM9/24/01
to

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.

rich

unread,
Sep 24, 2001, 11:10:48 PM9/24/01
to
The inplace editor seems to be doing well with multi-line editing
(that is if I have set the row height very tall at design time). But,
I just couldn't seem to get the stringgrid to cooperate. I will try
your suggestion and see what happens. You are talking about DrawText
with the DT_CALCRECT flag, right?

Peter Below (TeamB) <10011...@compuXXserve.com> wrote in article
<VA.000079a...@antispam.compuserve.com>...

Peter Below (TeamB)

unread,
Sep 25, 2001, 1:34:57 PM9/25/01
to
In article <01c1456f$426cd680$0200a8c0@p133>, Rich wrote:
> You are talking about DrawText
> with the DT_CALCRECT flag, right?
>

right.

rich

unread,
Sep 25, 2001, 2:30:03 PM9/25/01
to
That's what I needed.

Thanks dude.


Peter Below (TeamB) <10011...@compuXXserve.com> wrote in article

<VA.000079b...@antispam.compuserve.com>...

David J. Smith

unread,
Sep 28, 2001, 7:08:04 AM9/28/01
to

I'm still hung up on the inplace Editor able to multi-line edit. Are you
saying that the inplace editor will display more than one line as
in wrap text? I've never seen it do that and I have made the rowheight large
at design time. The Calcrect option would seem to merely output the height
of one line of text before word wrapping.

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

Peter Below (TeamB)

unread,
Sep 28, 2001, 1:49:57 PM9/28/01
to
In article <3bb45a1e$1_2@dnews>, David J. Smith wrote:
> I'm still hung up on the inplace Editor able to multi-line edit. Are you
> saying that the inplace editor will display more than one line as
> in wrap text?

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.

David J. Smith

unread,
Sep 28, 2001, 6:38:42 PM9/28/01
to

Thanks. This looks great. I've tried I think most of these ideas one at
a time, but never in the right combo to actually get it to work :-).

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

rich

unread,
Oct 1, 2001, 10:36:26 AM10/1/01
to
David J. Smith <djsm...@pacbell.net> wrote in article
<3bb45a1e$1_2@dnews>...

>
> I'm still hung up on the inplace Editor able to multi-line
edit. Are you
> saying that the inplace editor will display more than one line as
> in wrap text?

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.

David J. Smith

unread,
Oct 4, 2001, 6:16:11 AM10/4/01
to
Peter,

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

Peter Below (TeamB)

unread,
Oct 4, 2001, 2:54:06 PM10/4/01
to
In article <3bbc36f7_2@dnews>, David J. Smith wrote:
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?

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.

David J. Smith

unread,
Oct 4, 2001, 7:27:56 PM10/4/01
to
wow. is that completely undocumented API behavior? I don't see a clue as to
any of that in the Windows API documentation. I had no clue to any of this
until you presented your sample code.

"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote in message

news:VA.00007a8...@antispam.compuserve.com...

Peter Below (TeamB)

unread,
Oct 5, 2001, 2:24:19 PM10/5/01
to
In article <3bbcf098_2@dnews>, David J. Smith wrote:
> wow. is that completely undocumented API behavior?
>

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

David J. Smith

unread,
Oct 5, 2001, 5:03:05 PM10/5/01
to

You're right. I guess I was generally confused about everything :-).
Thanks for helping me get things clear.

Dave Smith

"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote in message

news:VA.00007a9...@antispam.compuserve.com...

0 new messages