--------------------------------------------
| Row # 1
| Explanation of row #1
--------------------------------------------
| Row # 2
| Explanation of row #2
--------------------------------------------
Thanks in advance.
Regards,
Wasis
Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Although your attachment is not terribly big, please do not include such
attachments in future.
See the newsgroup guidelines at http://www.borland.com/newsgroups/guide.html
Thanks,
--
Wayne Niddery - WinWright Consulting
Delphi, C++Builder, JBuilder, InterDev --
http://home.ican.net/~wniddery/RADBooks.html
"It is not true that the function of law is to regulate our consciences, our
ideas, our wills, our education, our opinions, our work, our trade, our
talents or our pleasures. The function of law is to protect the free
exercise of these rights." - Frederic Bastiat, 1850
Thanks in advance.
Regards,
Wasis
> you can do this with a OnDrawCell handler for the grid:
Why don't you just try it? <g>
Mike Orriss (TeamB)
(No e-mail replies, please, unless explicitly requested!)
On Thu, 10 Jun 1999 17:06:32 +0200, "Peter Below (TeamB)"
<10011...@compuXXserve.com> wrote:
>In article <375F3B61...@rad.net.id>, Wasis Sugiono wrote:
>> How can I make a row of stringgrid have more than one line like this:
>>
Well, the routine only changes the row height if it is too small, it will
never make a row smaller. This is due to the fact that
a) i didn't think that far when i wrote the sample <g>.
b) it is not a task easily done by an ondraw cell handler. If you reduce
the row height when drawing cell A all cells of the row will be redrawn.
So cell B sees that the row is now too small for its text and resizes the
row larger. Which redraws all cells of the row, so cell A sees that the
row is too high for its text .... you see the problem, i think <g>.
So you need to handle this elsewhere. The grid does not have an event that
fires when the column width is changed but it has a dynamic method that is
called when that happens (due to user action or assignments to ColWidths
or DefaultColWidth):
procedure ColWidthsChanged; dynamic;
So derive a new class from Tstringgrid and override this method (it is
protected). Call the inherited method first and then you can either fire a
custom event to delegate action to the form or directly walk over the
cells and measure the text in each cell to adjust the row height if
required.
There is a different approach one could take that would not require a new
grid class: instead of changing the row height directly in the OnDrawCell
event handler one could call a method from the handler that checks the
text in *all* cells of the row and resizes the row to fit the largest
cell text. The method would be a function that returns whether it actually
changed the row height or not. The OnDrawCell handler would only draw the
cells content if the method did not change the row height. This approach
would make the row adapt to the text as the user changes it, not only when
he resizes a column.
Thanks for the effort you obviously put into researching this. I have
actually come up with a solution that works, although I'm not sure if
I like it very much. So I might try one or more of your suggestions
as well.
I got it to work using the OnDrawCell event handler that you provided
earlier, with some additions. What I did was this:
type
TForm1 = class(TForm)
...
private
CurrRowHeight: Integer;
...
end;
...
procedure TForm1.sgReceivedMessagesDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
S: String;
MyRect: TRect;
begin
with (Sender as TStringGrid) do
begin
S := Cells[ACol,ARow];
if Length(S) > 0 then
begin
MyRect := Rect;
DrawText(Canvas.Handle, PChar(S), Length(S),
MyRect, DT_CALCRECT or DT_WORDBREAK or DT_LEFT);
if ACol = 0 then
CurrRowHeight := 0;
if (MyRect.Bottom-MyRect.Top) > CurrRowHeight then
CurrRowHeight := MyRect.Bottom - MyRect.Top;
if CurrRowHeight > RowHeights[ARow] then
RowHeights[ARow] := MyRect.Bottom - MyRect.Top
else if (ACol = (ColCount-1)) and (CurrRowHeight <
RowHeights[ARow]) then
RowHeights[ARow] := CurrRowHeight
else
begin
MyRect.Right := Rect.Right;
Canvas.FillRect(MyRect);
DrawText(Canvas.Handle, PChar(S), Length(S),
MyRect, DT_WORDBREAK or DT_LEFT);
end;
end;
end;
end;
What I don't like about it is that it relies on the grid to always
redraw the cells from left to right one row at a time. If it were in
some other order, this probably wouldn't work.
Not a safe assumption to make...