Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TStringGrid Errors

Status
Not open for further replies.

PenCup

Programmer
May 5, 2009
5
0
0
US
Hello, I am a new user to borland and I am getting some errors with displaying data in my TStringGrid. Sometimes it works well, and then other times all the cells seem to get mixed up randomly. I notice that my catch block outputs that 2 sections of my code are failing, a method to write some data to the grid, and the overridden method of OnDrawCell. I am not really sure what is causing the error as the code does not do much in those 2 sections. Has anyone heard of a problem like this before, and perhaps know a way to solve it? Thanks for your input.

Method RefreshList: iterate through a list and display test data in the grid, then call repaint.

Code:
HRESULT __fastcall TPSM_Form1::RefreshList()
{
 try{
        EXVECT::iterator itrE;
        itrE = LogList.end()-1;
        Count->Caption = IntToStr(LogList.size());

       for(int Row = 1; Row != LogList.size()+1; Row++)
       {
                RTExGrid->Cells[0][Row] = "  +";
                //Date
                RTExGrid->Cells[1][Row] = Trim(FormatDateTime("mm/dd/yyyy",(*itrE)->Exc.StartDateTime));
                //Time
                RTExGrid->Cells[2][Row] = Trim(FormatDateTime("HH:nn:ss",(*itrE)->Exc.StartDateTime));
                //test
                RTExGrid->Cells[3][Row] = "000";
                RTExGrid->Cells[4][Row] = "R#";
                RTExGrid->Cells[5][Row] = "A#";
                RTExGrid->Cells[6][Row] = "T#";
                RTExGrid->Cells[7][Row] = "$0.00";
                RTExGrid->Cells[8][Row] = "E#";

                if(itrE == LogList.begin())
                        break;
                itrE--;
       }
       RTExGrid->Repaint();
       return S_OK;
 }
 catch(...)
 {
        WriteLog(L_ERROR, "RefreshList has FAILED");
 }
}

Method onDrawCelll: draw the headers and normal cells differently.

Code:
void __fastcall TPSM_Form1::RTExGridDrawCell(TObject *Sender,int ACol, int ARow,TRect &Rect, TGridDrawState State)
{

try{

        TStringGrid* StringGrid =
    static_cast<TStringGrid*>(Sender);
  assert(StringGrid != NULL);

  TCanvas* SGCanvas =StringGrid->Canvas;
  SGCanvas->Font = StringGrid->Font;

  RECT RText = static_cast<RECT>(Rect);
  const AnsiString text(
    StringGrid->Cells[ACol][ARow]);

  const bool fixed =
    State.Contains(gdFixed);
  const bool focused =
    State.Contains(gdFocused);
  bool selected =
    State.Contains(gdSelected);
  if (!StringGrid->Options.Contains(
    goDrawFocusSelected)) {
    selected = selected && !focused;
  }
  // if the cell is fixed (headers)
  if (fixed) {
    SGCanvas->Brush->Color =
      StringGrid->FixedColor;
    SGCanvas->Font->Color = clBtnText;
    SGCanvas->FillRect(Rect);
    Frame3D(SGCanvas, Rect,
      clBtnHighlight, clBtnShadow, 1);
  }
  // if the cell is selected
  else if (selected) {
    SGCanvas->Brush->Color =clHighlight;
    SGCanvas->Font->Color =
      clHighlightText;
    SGCanvas->FillRect(Rect);
  }
  // if the cell is normal
  else {
    SGCanvas->Brush->Color =
      StringGrid->Color;
    SGCanvas->Font->Color =
      StringGrid->Font->Color;
    SGCanvas->FillRect(Rect);
  }
  // if the cell is focused
  if (focused) {
    DrawFocusRect(
      SGCanvas->Handle, &RText);
  }

  // draw the text
  RText.left += 2; RText.top += 2;
  DrawText(SGCanvas->Handle,
    text.c_str(), text.Length(), &RText,
    DT_LEFT |DT_VCENTER |DT_SINGLELINE);

    }
    catch(...)
    {
        WriteLog(L_ERROR, "RTExGrid DrawCell Event FAILED.");
    }
}
 
Start with these articles and see if that can narrow down the problem.



James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Thank you for the articles 2ffat, I have looked through them. I replaced the code in my OnDrawCell method with the one suggested in one of the articles. This change does not seem to make a difference. I have narrowed down the failure in the event to be an EAccessViolation using Exception.message, but I can't find a way to get any more information out of it.

Both the code I had pasted and the code from the linked articles produces this EAccessViolation.

-PenCup
 
Access Violations are a royal pain to find. Some are caused by hardware and others by software. Most commonly they are caused by accessing memory that is no longer available (software errors) which it sounds like you have. For example, if you define an array with 5 items and then try to access the 6th item, you will get an access violation. Another common cause is not nulling pointers after they go out of scope.

Can you narrow down where the violation is happening? That might give you a clue as to what is happening.



James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Just wanted to thank you again for your help with this issue I was having 2ffat. I looked all over and took care of all the issues I could think of, with nothing eliminating the errors. The way i got rid of them was by running the program in compatibility mode for 98/ME (on an XP OS). Not exactly sure why this had any effect, but I'll take it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top