Getting pointer to edit box in MFC

Hi,

I'm learning to use MFC and I have encountered a problem... I have a main dialog window, with an edit box (which is used for displaying information), and when a button on the main dialog is pressed, function from another c++ file is called to load data from some txt files and processes it, and I wish to display any errors from processing the txt file to the edit box in my main dialog... say my main dialog's class is called StepDlg, I tried to do this in my other c++ file:

StepDlg dialog;
dialog.send_data(data);

Where "data" is the data I want to display in the edit box... I got an assertion error for this and after a bit of digging and searching I found that the problem is because I have created another dialog by doing "StepDlg dialog;", so I figured I need to pass a pointer of the original dialog into my other c++ file in order to display the information to the edit box... the question is how would I go about passing this pointer in I thought about passing it as a variable in the function but the problem with that is there's too many functions in between the dialog and the one that want to print information to the dialog and it becomes really messy.

Thanks for any help!
Danny


Answer this question

Getting pointer to edit box in MFC

  • moemen.ahmed

    um ok looking at it... this is what I can see:

    dialogTestDlg.cpp:um ok looking at it... this is what I can see:

    dialogTestDlg.cpp:
    CDialogTestDlg::CDialogTestDlg(CWnd* pParent /*=NULL*/)
        : CDialog(CDialogTestDlg::IDD, pParent)
    {
        //{{AFX_DATA_INIT(CDialogTestDlg)
        m_EditBox = _T("");
        //}}AFX_DATA_INIT
        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }

    and

    dialogTest.cpp:
    BOOL CDialogTestApp::InitInstance()
    {
        // Standard initialization
        // If you are not using these features and wish to reduce the size
        //  of your final executable, you should remove from the following
        //  the specific initialization routines you do not need.

    #ifdef _AFXDLL
        Enable3dControls();            // Call this when using MFC in a shared DLL
    #else
        Enable3dControlsStatic();    // Call this when linking to MFC statically
    #endif

        CDialogTestDlg dlg;
        m_pMainWnd = &dlg;
        int nResponse = dlg.DoModal();
        if (nResponse == IDOK)
        {
            // TODO: Place code here to handle when the dialog is
            //  dismissed with OK
        }
        else if (nResponse == IDCANCEL)
        {
            // TODO: Place code here to handle when the dialog is
            //  dismissed with Cancel
        }

        // Since the dialog has been closed, return FALSE so that we exit the
        //  application, rather than start the application's message pump.
        return FALSE;
    }

    I'm still a bit confused as to how exactly I'd go about getting the handler for my function, as it seems that I need to pass in this handler for the main dialog Is that the case or would I be able to somehow get the handler for the dialog without passing anything from my dialog file to my function file If possible could you please provide me with an example many thanks

    Danny

  • Rizzlers

    You can map your dialog's edit box to a DDX variable. Once you do that, you can directly use that control variable to access the edit box.

    Alternatively, use GetDlgItem(edit control's resource id) to get a CWnd* to the edit box.



  • talismax

    messages

    Kuphryn


  • SneakerXZ

    AfxGetApp() inside m_pMainWnd

    Kuphryn


  • Roto

    If you call your external function from the handler of your button, then you can pass "this" value, which is the pointer to the current dialog box:

    void CMyDialog::OnBnClickedMyButton()

    {

    myfunction(this);

    }

    If you have a control variable for your edit box, then you can send it instead:

    void CMyDialog::OnBnClickedMyButton()

    {

    myfunction(&mMyEditBox);

    }

    In this case the external function may look like this:

    void myfunction(CEdit * editBox)

    {

    . . .

    editBox->SetWindowText("some txt");

    }

    Alternatively, your function can return the text to be displayed in the edit box. In this case the handler may look like this:

    void CMyDialog::OnBnClickedMyButton()

    {

    CString message = myfunction();

    mMyEditBox.SetWindowText(message);

    }

    I hope this helps.


  • p_shah

    cool got it working, thanks!!!

  • ARme

    Hi,

    Thanks for the reply, yes I did think about doing it that way, the problem is the structure of the program is a bit like this:

    CMyDialog -> functionA -> functionB -> functionC/D/E/etc

    And I actually want to display information from functionC/D/E/etc to the edit box in CMyDialog. Now I realise that the method you showed works but however there's quite a few functions in the functionC level, and doing it that way would mean a lot of modification to the program (functionA onwards is actually a library of functions for controlling a machine, i.e. not my own program). I have to try this tomorrow but I was wondering if something like this would work (this is looking at some code that I did a while ago):

    class CMyDialog {
    ...
    CString *GetEditBox() const { return mEditBox; }
    ...
    };

    void functionC()
    {
    CString editbox = CMyDialog::GetEditBox();
    editbox.SetWindowText(message);
    }

    I tried something similar earlier today (with some syntax errors though) and it didn't seem to work, and looking at it I don't think it will work but is something similar to this (obviously not with this bad syntax.. I haven't touched C++ for a few months and need to work on getting back to it) possible

    Regards,
    Danny

  • rwerner

    onbutton
    {
    function
    }

    function
    {
    // thread
    ::AfxGetApp()->m_pMainWnd->PostMessage
    //
    default thread
    CEdit::FromHandle(::GetDlgItem(::AfxGetApp()->m_pMainWnd->GetSafeHwnd(), id))->SetWindowText
    }

    Kuphryn


  • GR101

    Thanks for the reply, but was wondering how do I get the handle for the dialog I have:

    #ifndef _FUNCTION_H_
    #define _FUNCTION_H_
    #include "dialogTestDlg.h"

    void displayData()
    {
    CWnd *EditBox = GetDlgItem(IDC_EDIT_BOX);
    EditBox->SetWindowText("Data");
    }

    #endif

    But I get this error message...:

    --------------------Configuration: dialogTest - Win32 Debug--------------------
    Compiling...
    dialogTestDlg.cpp
    h:\dialogtest\function.h(7) : error C2660: 'GetDlgItem' : function does not take 1 parameters
    Error executing cl.exe.

    dialogTest.exe - 1 error(s), 0 warning(s)

    I can do this in my dialogTestDlg.cpp (the main dialog function) and it works fine.

    void CDialogTestDlg::OnButtonData()
    {
    CWnd *EditBox = GetDlgItem(IDC_EDIT_BOX);
    EditBox->SetWindowText("Data");
    }


  • Getting pointer to edit box in MFC