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

Posted on 2009-05-01
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
    LVL 39

    Expert Comment

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

    Author Comment

    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),
                &Rect, DT_WORDBREAK |  DT_CALCRECT|  DT_NOCLIP | DT_LEFT);
        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];
        format =      DT_WORDBREAK | DT_LEFT | DT_CALCRECT |  DT_NOCLIP;
        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

    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
    LVL 1

    Author Comment

    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

    LVL 1

    Author Closing Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    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…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now