0
votes

I've installed a Windows XP Professional SP3 on a VMWare image and the Visual Studio 2005 on it. I've created a new dialog based C++ MFC project with /clr support. I've put a RichEdit 2.0 control onto the auto-generated dialog and I'm trying to read up a text file and put its content into this RichEdit 2.0 control by button click without formatting. I've added a variable to the RichEdit 2.0 called pCRichEditCtrl and here is my code which doesn't work.

CWinApp inheritance:

BOOL CTextFormatterApp::InitInstance()
{
    ...
    AfxInitRichEdit2();
    CWinApp::InitInstance();
    ...
}

CDialog inheritance:

void CTextFormatterDlg::OnBnClickedButton1()
{   
    StreamReader^ objReader = gcnew StreamReader("c:\\text.txt");
    String ^sLine = "";
    sLine = objReader->ReadLine();
    while (sLine != nullptr)
    {
            pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
            pCRichEditCtrl.ReplaceSel(CString(sLine));
            sLine = objReader->ReadLine();
    }

    objReader->Close();
}

I don't know whether it counts but I get the following warnings at linking:

TextFormatterDlg.obj : warning LNK4248: unresolved typeref token (01000016) for 'AFX_CMDHANDLERINFO'; image may not run

TextFormatter.obj : warning LNK4248: unresolved typeref token (01000012) for 'AFX_CMDHANDLERINFO'; image may not run

TextFormatterDlg.obj : warning LNK4248: unresolved typeref token (01000015) for 'IAccessibleProxy'; image may not run

I'm not sure what I'm doing because I'm familiar only with newer frameworks and I don't know either Windows.

Input file exists, I can see the read text if I debug the application but I can't see any changes in the edit box. I've tried to call pCRichEditCtrl.UpdateData(true); but nothing has changed.

Is it enough to add a variable for getting the controller of the box (pCRichEditCtrl)? It seems to the pointer doesn't point to the proper control item.

Do you have any idea what is missing?

2
I have given an answer to load your control using straight MFC -- how/where do you declare pCRichEditCtrl? and how do you connect it to your control?Edward Clements
I added it via GUI: right click on the RichEdit control -> Add variable... Additional info: if I add the following line then RichEdit shows the text, but only from the 2nd line: UpdateData(TRUE); It's weird.bdevay
edited my answer with settings for pCRichEditCtrl, a simple test project as below works for me without UpdateData(), this is necessary only if you have variables of type "value"Edward Clements

2 Answers

1
votes

There's no need to use CLI to just read text files, try something like:

void CTextFormatterDlg::OnBnClickedButton1()
{   CStdioFile f1;
    CString sLine;
    if (!f1.Open(_T("c:\\text.txt"), CFile::modeRead | CFile::typeText))
        return;
    while (f1.ReadString(sLine))
    {   pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
        pCRichEditCtrl.ReplaceSel(sLine);
    }

    f1.Close();
}



EDIT: control variable pCRichEditCtrl

a) should be declared in the dialog class as CRichEditCtrl pCRichEditCtrl;

b) should be connected to the ID of the control (e.g.: IDC_RICHEDIT21), like

void CTextFormatterDlg::DoDataExchange(CDataExchange* pDX)
{   CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_RICHEDIT21, pCRichEditCtrl);
}

c) I have tested the following code and it works for me (adds "aa" to the control window on each button click)

pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
pCRichEditCtrl.ReplaceSel(TEXT("aa"));
0
votes

I share the final solution with the community to be available for those who faces with the same issue. I don't know why do I have to use Update(FALSE); on the CWinApp inheritance two times but it solves everything. If someone has an idea or a better (nicer) solution don't hesitate to share it with us, I'll move the accepted flag to that version (if it is possible, I haven't tried it before).

void CTextFormatterDlg::OnBnClickedButton1()
{
    StreamReader^ objReader = gcnew StreamReader("c:\\text.txt");
    String ^sLine = objReader->ReadLine();
    UpdateData(FALSE); //this is the first unexpected first aid
    while (sLine != nullptr)
    {
        pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
        pCRichEditCtrl.ReplaceSel(CString(sLine + "\r\n"));
        UpdateData(FALSE); //this is the second unexpected first aid
        sLine = objReader->ReadLine();
    }

    objReader->Close();
}