I am using the MFC dialog application.
There are 3 edit box, add1, add2 and sum
and a button "add",
when exectuing, I input add1, add2, and use the "add" function dlg.m_sum=dlg.m_add1+dlg.m_add2
how to make the sum appears in the "sum" edit box? Do I have to add a view class or how to modify the OnPaint?
I now only have add.cpp and addDlg.cpp

Dani AI

Generated

A couple of practical options for storing the three values were not shown yet. For a beginner the easiest, most robust route is CSV: no Excel dependency, easy to inspect, and Excel will open it. If you truly need a binary .xls/.xlsx file (multiple sheets, formatting, formulas) use Excel Automation, ADO (Jet/ACE) or a library that writes XLSX — those are more complex and platform/version dependent.

Below are two small MFC-friendly helpers: one appends a row "add1,add2,sum" to a CSV, the other reads the last non-empty row and parses three integers. Call the save helper after you compute the sum; call the read helper from your ReadLastStored handler and populate your dialog members.

void SaveRowToCsv(int a, int b, int s)
{
    CStdioFile file;
    if (file.Open(_T("data.csv"),
                  CFile::modeCreate | CFile::modeNoTruncate |
                  CFile::modeWrite | CFile::shareDenyNone | CFile::modeAppend))
    {
        CString line;
        line.Format(_T("%d,%d,%d\r\n"), a, b, s);
        file.WriteString(line);
        file.Close();
    }
}
bool ReadLastRowFromCsv(int& a, int& b, int& s)
{
    CStdioFile file;
    if (!file.Open(_T("data.csv"), CFile::modeRead | CFile::shareDenyNone))
        return false;
    CString line, last;
    while (file.ReadString(line))
        if (!line.IsEmpty())
            last = line;
    file.Close();
    if (last.IsEmpty()) return false;
    return (_stscanf(last, _T("%d,%d,%d"), &a, &b, &s) == 3);
}

Notes and tips: use a full path (or AppData) if the EXE folder is not writeable; add basic validation and error messages; handle locale-related separators if users have non-comma CSV settings; protect against concurrent access if multiple processes may write. As showed, integrate these helpers from your button handlers once dialog members hold the values.

Recommended Answers

All 4 Replies

in the OnButtonAdd() event handler for the button
1. assume m_sum, m_add1 and m_add2 are integer class member variables which were defined using ClassWizard (VC++ 6.0 compiler), just call UpdateData() after setting m_sum = m_add1 + m_add2.

void OnButtonAdd()
{
   // move window text into member variables
   UpdateData(TRUE); 
   m_sum = m_dat1 + m_data2;
   // move data member variables back to windows
   UpdateData(FALSE);
}

Great !! thank you,
this I don't even have to define a new dlg instance

DO you know how to write this add1, add2, sum values into 2 excel file which I could read and write through added buttons on this dialog box?

I know how to read and write .xls file,
but I just don't know how to save these "add1","add2","sum" into 3 columns in .xls file and how to extract them into these 3 edit box again if I add a button called "ReadLastStored"

in the OnButtonAdd() event handler for the button
1. assume m_sum, m_add1 and m_add2 are integer class member variables which were defined using ClassWizard (VC++ 6.0 compiler), just call UpdateData() after setting m_sum = m_add1 + m_add2.

void OnButtonAdd()
{
   // move window text into member variables
   UpdateData(TRUE); 
   m_sum = m_dat1 + m_data2;
   // move data member variables back to windows
   UpdateData(FALSE);
}

very similar to the code I already posted. Assume you have a Read button Note: there are other c++ ways to convert from std::string to ints, what I show below works most of the time but will not catch errors in the data read from the xls file or integer overflow problems.

void OnButtonRead()
{
    // do something to read the xls file into member 
    // this assumes the data is read into std::string object
    string add1 = ??? // read xls
    string add2 = ??? // read from xls
    m_add1 = atoi(add1.c_str());
    m_add2 = atoi(add2.c_str());
    m_sum = m_add1 + m_add2;
    UpdateData(FALSE);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.