tags:

views:

255

answers:

4

I have a view which is derived from CEditView. It is read only. I would like to set the text as a kind of logging, but nothing shows up on the screen. If I inspect temp in the debugger after GetEditCtrl().GetWindowText(temp); I can see that the text is indeed changing internally, but I see nothing on the screen.

// HistoryView.cpp : implementation file
//

#include "stdafx.h"
#include "HistoryView.h"


// CHistoryView

IMPLEMENT_DYNCREATE(CHistoryView, CEditView)

CHistoryView::CHistoryView()
{

}

CHistoryView::~CHistoryView()
{
}

BEGIN_MESSAGE_MAP(CHistoryView, CEditView)
END_MESSAGE_MAP()


// CHistoryView diagnostics

#ifdef _DEBUG
void CHistoryView::AssertValid() const
{
    CEditView::AssertValid();
}

#ifndef _WIN32_WCE
void CHistoryView::Dump(CDumpContext& dc) const
{
    CEditView::Dump(dc);
}
#endif
#endif //_DEBUG


// CHistoryView message handlers

void CHistoryView::OnInitialUpdate()
{
    CEditView::OnInitialUpdate();

    // TODO: Add your specialized code here and/or call the base class
    GetEditCtrl().SetReadOnly(TRUE);
}

//!
/*!
*/
void CHistoryView::AddRow(CString message)
{
    CString temp;
    GetEditCtrl().GetWindowText(temp);

    if(temp.IsEmpty())
    {
     GetEditCtrl().SetWindowText(message);
    }
    else
    {
     GetEditCtrl().SetWindowText(temp + "\r\n" + message);
    }

    GetEditCtrl().LineScroll(2, 0);
    //GetEditCtrl().UpdateWindow(); // no effect
}
+2  A: 

The problem seems to lie somewhere other than in the code you posted. I created a new MFC app with the view derived from CEditView, and the code you're using to add the text worked ok, although I did have to wrap the literal "\r\n" inside an explicit temporary CString, as in:

GetEditCtrl().SetWindowText(temp + CString("\r\n") + message);
Joel
Thanks for the effort. While similar code worked (well, as best as you could expect given ChrisN's point) previously, it seems I'v broken something moving from VS2003->VS2005.
Nick
+2  A: 

As Joel says, the problem lies elsewhere. But, there's a bigger problem with what you're doing. Copying the text from the control, appending to the string, then setting the text will have terrible performance once you add more than a few lines of text to the control.

In the past, when I've needed a window to show log messages I've created a view that contains a CListBox control. To add a row, call CListBox::AddString, then when the list-box reaches some maximum number of rows, call CListBox::DeleteString to delete the oldest item. This way, adding rows is always fast and the amount of memory used by the control doesn't grow indefinitely.

If the text is only for display and you don't need it to be editable, I'd suggest you consider using a CListBox instead.

I hope this helps!

ChrisN
A: 

In addition to ChrisN's answer, if you want to keep a CEdit, You could use

// sets cursor to end of text
int nCurrentLength= GetEditCtrl().GetWindowTextLength();
GetEditCtrl().SetSel(nCurrentLength,nCurrentLength); 

// appends text
GetEditCtrl().ReplaceSel("\r\nMynew line");
Serge - appTranslator
A: 

Turns out that a third-party UI toolkit was reconstructing the View (who knows why?) so my pointer to it was stale. Thus, I was actually refreshing a different view!

Nick