0
votes

I am creating a very very simple spreadsheet type application. It has a grid that draws cells and the user can specify the cell type (text, check box, radio button).

I'm trying to get the text to work out. In the main View class I have:

void CSpreadView::OnInsertText()
{
    CEdit* pEdit = new CEdit;
    CWnd* pParentWnd = this;
    grid.CellType(pEdit, pParentWnd);
    Invalidate();   
    UpdateWindow();
}

I'm passing the parent window because I don't know if there is a way to find the parent window if I'm in another class.

So the function that receives it:

void Grid::CellType(CEdit* pEdit, CWnd* pParentWnd)
{
    for (int a=0; a<(int) cells.size(); a++)
    {
        if(cells[a]->selected)
            cells[a]->Type(pEdit, pParentWnd);
    }
}

Finally, when the cell is drawn it does:

if(type=="text")
    {
        CEdit* pEdit = new CEdit;
        pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(x1+10, y1+10, x2-10, y2-10), pParentWnd, 1);
    }

The problem: it actually draws the box, however, the text is invisible. When I type it flashes, but when I stop it disappears. Does anyone know why this is happening?

Just so you know, I want the cell to control its type and do the drawing because the user can add/delete rows and columns. That I way I don't need to keep track of what text boxes where previously drawn. The grid is drawn by:

CBrush brush(RGB(color, color, color));
pDC->SelectObject(&brush);
pDC->Rectangle(x1, y1, x2, y2);

This is what I see: enter image description here

2
Why do you create two different CEdits? There is one created in OnInsertText(), which you pass to CellType(), and another which is created in your "Finally when the cell is drawn" code. That second one actually has an edit control created, but it looks like it has a memory leak because the CEdit* value from new is just thrown away at the end of the block.AAT
AAT is right. You creating two different CEdit instances. As far as I remember, you can use GetParent() function to get window parent. msdn.microsoft.com/en-us/library/0x2wyab0(v=vs.80).aspxbesworland
Yes thank you, that was a mistake from trying different things.mgalal

2 Answers

0
votes

Don't create child windows on OnPaint(), that is bound for disaster. You need to do your own drawing, you can at most re-use a single CEdit when editing a single cell.

I take it you're trying to make a grid where each cell is control (window) which will e.g. move along when you scroll in the grid. This will always give visual artifacts (jerky movement) and cause problems when displaying half cells.

You're going to have to derive your own cell types, who do their own drawing (possibly using the DrawFrameControl() API for the more complex types) and who may pop up a child window when the content of the cell is being edited. This is roughly the design used by the fore-mentioned Maunder CGridCtrl.

0
votes

I guess if this is a homework problem, then it is worth re-inventing the wheel. OTOH, there are already decent grids out there like Chris Maunder's at codeproject.com.

If you are creating a CEdit control everytime you draw a cell, you are going to have problems. You basically only want to have a single edit cell for the cell you are editing, and probably no others. In your parent window (of the edit control), you might want to create with or set its style to have WS_CLIPCHILDREN so that it doesn't paint or erase over the child edit control. You can at least see if setting the style flag has any effect.