problem with display of large text in a TStringGrid in Borland C++ Builder 5.0

Posted on 2009-05-01
Medium Priority
Last Modified: 2013-11-17
I am using Borland C++ builder 5.0 and am using a 2 column TStringGrid to display some text .It works fine when there is not much text in each cell of the TStringGrid. I have a problem when text is more than 500 or 1000 characters (multiple lines of text) and it has to be displayed in one cell. The full text is not shown. I tried setting DefaultDrawing to false and writing my own DrawCell callback function. I set the DT_WORDBREAK formatting option to the Windows API DrawText(...) in the DrawCell() event handler function. But even this is not showing the full text but only the first few lines of text.How do I solve the problem?

Thanks and Warm Regards
Question by:rraghu08
  • 3
  • 2
LVL 39

Expert Comment

ID: 24285628
>>>> more than 500 or 1000 characters (multiple lines of text) and it has to be displayed in one cell.
Does the text fit physically into the cell? Or would you need scrolling?

If the first I would assume the problem already is when you insert the text to the cell. Can you post the code sequence where this was done? And what were the buffers and string variables where the text comes from and was set to the grid cell?

When you were writing your own drawing callback, how did you get the text to draw? Is it already shortened there or only when displaying on the screen? If the latter, you might check the drawing rectangle? If it for example could only take 4 lines of text, it necessarily would cut the rest. If the text already was spoiled when the callback was called you might think about providing the full text at drawing time from own resources rather than storing it in the grid. Most grids have the option to only store some kind of handle (pointer) to a cell and only when the cell is going to be visualized the text was requested by means of a callback. Or if owner-drawn you anyhow can get the text to be drawn from other sources.

Author Comment

ID: 24285677
It has scrolling enabled. The test is fully there in the DrawCell  event handler function . It gets the right rectangle required for the text . I am using DrawText() Windows API to diplay the test with the given rectangle. Everyhtin is right but it still fails to display the full text.I have attached the code from the DrawCell() callback function.  

In the function where I am assigning the text to a particular cell in the TStringGrid, I am modifying the cell height as shown in the below code before I assign text to that cell.

   TRect Rect;
    Rect.Left = 636;
    Rect.Top  = 416;
    Rect.Right = Rect.Left + 235;

            strtmp, strlen(strtmp),
    FORM->resCmntStringGrid->RowHeights[Num] = Rect.Height();
    FORM->resCmntStringGrid->Cells[1][Num] = cmntStr;

Thanks and Warm Regards

void __fastcall TscanDataForm::resCmntStringGridDrawCell(TObject *Sender,
      int ACol, int ARow, TRect &Rect, TGridDrawState State)
    if (ARow == 0)
         FORM->resCmntStringGrid->Canvas->Brush->Color = CYAN_BG;
        FORM->resCmntStringGrid->Canvas->Brush->Color = 
    RECT R = RECT(Rect);
    UINT format ;
    AnsiString text = resCmntStringGrid->Cells[ACol][ARow];
    char* txtbuf = new char[ text.Length() + 1 ];
    sprintf(txtbuf, "%s", text.c_str());
    int len = strlen(txtbuf);
             txtbuf, len,
             &Rect, format);
    format =      DT_WORDBREAK | DT_LEFT |  DT_NOCLIP;
             txtbuf, len,
             &Rect, format);

Open in new window

LVL 39

Accepted Solution

itsmeandnobodyelse earned 200 total points
ID: 24285771
First, instead of copying the text to a buffer which never was freed after that, thus producing memory leaks, you should do

   AnsiString& text = resCmntStringGrid->Cells[ACol][ARow];  // note the &
   DrawText(resCmntStringGrid->Canvas->Handle, text.c_str(), text.Length(),
             &Rect, format);

Second, if drawing the text yourself, you also must handle the scrolling case. That means you need to find out which lines of the text should be outputted and parse the text for "\r\n" CRLF line wraps. You can forget the automatic line and word wrap if owner drawing. Instead, you need to make the wrap on your own, e. g. by putting all text to a hidden multi-line edit control of same size as the (visual) cell. Then you can retrieve line by line from that edit control. Unfortunatly I have no idea  how to get the scroll information from the cell. You would need to check the structure passed as argument to the drawing callback for any information regarding the scroll  status of a multi-line text cell.

Regards, Alex

Author Comment

ID: 24298446
I replaced the idea of directly displaying text in each cell of the TStringGrid  by using a dynamically created TMemo for each cell which would display the full text when the user clicks on the cell.

also are yousing DT_WORDBREAK doesn't work when used as formatting argument in DrawText() win API.  Is it not supposed to work since its a direct Windows API and you are directly calling it.

thanks and regards


Author Closing Comment

ID: 31577050
hi ,
I am not sure if  DT_WORDBREAK doesn't work when used as formatting argument in DrawText() Win32 API.  Is it not supposed to work since its a direct Windows API and you are directly calling it. Do let me know if you can confirm it either way.  thanks and regards Raghu

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 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