Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store data from edit box

Status
Not open for further replies.

Newbie79

MIS
Aug 5, 2003
1
US
have a question to ask:
I am a newbie in mfc programming.
How can I store and save the value enter by user on an edit box and transfer these data onto a new file. The file maybe a database format or even a normal text file. These data might be delete and revise at a later stage.
Anticipating for your brilliant mind. Thanks
 
There are several ways to do it; you can use ClassWizard to add a data member of
Code:
CEdit[\code] type or [code]CString[\code] type that is associated to the edit control.  

Assume the ID of your edit control is [code]ID_MY_EDIT[\code].

Using data member of CEdit type:[code]
    // CMyDialog.h
    class CMyDialog : public CDialog
    {
        ...
        CEdit   m_editMyEdit;
        ...
    };
[\code]
[code]
    // CMyDialog.cpp
    void CMyDialog::DoDataExchange(CDataExchange *pDX)
    {
       ...
       DDX_Control(pDX, ID_MY_EDIT, m_editMyEdit);
       ...
    }
[\code]
Note: the data member m_editMyEdit and the entry in DoDataExchange() is added by ClassWizard.
    
Read text from the edit control:[code]
    CString strMyEdit;
    m_editMyEdit.GetWindowText(strMyEdit);
[\code]

Write text to the edit control:[code]
    CString strMyEdit;
    // Set strMyEdit from external file...
    // ...
    m_editMyEdit.SetWindowText(strMyEdit);
[\code]

Using data member of [code]CString[\code] type required a little extra code, and I normally do not recommend if the dialog's UI logic is complicated.  


Here's another way without using additional data member.  

Read text from the edit control:[code]
    CString strMyEdit;
    GetDlgItemText(ID_MY_EDIT, strMyEdit);
[\code]

Write text to the edit control:[code]
    CString strMyEdit;
    // Set strMyEdit from external file...
    // ...
    SetDlgItemText(ID_MY_EDIT, strMyEdit);
[\code]

HTH
Shyan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top