Question

I am trying to add a text box to my form (down the road I wanted to pass data entered from this box to the Document for storage), but I am having trouble getting the text box to work.

Bellow is the code that I have tried. Why is my text box not appearing, and how do I properly get a text box to appear in my view.

void CMy861a4VasilkovskiyView::OnDraw(CDC* pDC)
{
    CMy861a4VasilkovskiyDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
    const CString str("Hello");
    pDC->TextOut(0, 0, str);
    CEdit m_edit;
    CRect rect(20,20,10,10);// (85, 110, 180, 210);

    m_edit.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP |
             ES_AUTOHSCROLL | WS_BORDER, rect, this, ES_LEFT);
   // m_edit.SetFocus();


    // TODO: add draw code for native data here
}
Was it helpful?

Solution

I can see a couple of problems :

  1. OnDraw() is not the right place for creating the CEdit control - CMy861a4VasilkovskiyView::Create() is probably the best place. If you leave it in OnDraw() this will recreate the CEdit control every time the view is redrawn, which isn't what you want.
  2. Your CEdit m_edit variable needs to be a member variable of your CMy861a4VasilkovskiyView class. This is the main reason it's not being drawn - at the moment m_edit goes out of scope before it gets drawn.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top