开发者

UpdateData works, then data changes?

Language: C++

Development Environment: Microsoft Visual C++

Libraries Used: MFC

Background: So I've created an application that is basically a large preference dialog where the user can configure a number of pages, each with a bunch of different settings. When the user is finished, he/she has three options for saving the preferences (as XML): Save Current [page], Save These, and Save All.

I'm working with the Save These function right now. When the user chooses this option, a dialog appears with check boxes for each page, allowing them to choose which pages they wish to output. Once they choose the directory into which they wish the files to be saved, the magic happens and the XML files are written.

Problem: I have a function (UpdatePageData) that will detect which page is being displayed and update the current page's data by calling UpdateData. I have put in a break point to watch to make sure the variables are being filled with the user's inputted values, and everything is dandy and working correctly. Howeve开发者_Go百科r, when the program jumps from the checkbox dialog (where I call UpdatePageData) to the classes for the pages I'm saving, suddenly all of the values are wrong.

Below I've included some code that will help you guys understand the program flow.

NOTE: In SaveThese, I am currently just working on saving a single page...I will have it update all pages selected once I figure out the problem I'm having.


Location: Main Dialog

void CSAPrefsDialog::OnSaveThese()
  {
  int msgboxID = ::MessageBox(
      NULL,
      (LPCSTR)"Are you sure you want to save?",
      (LPCSTR)"Save These",
      MB_ICONQUESTION | MB_OKCANCEL
  );

  switch (msgboxID)
  {
      case IDCANCEL:
          break;
      case IDOK:
          UpdatePageData();
          CSaveThese m_sT;
          m_sT.DoModal();
          break;
  }
}

Location: Main Dialog

void CSAPrefsDialog::UpdatePageData() 
{
  if ((m_iCurPage >= 0) && (m_iCurPage < m_pages.GetSize()))
  {
      pageStruct *pPS = (pageStruct *)m_pages.GetAt(m_iCurPage);
      if (pPS)
      {
          ASSERT(pPS->pDlg);
          if (pPS->pDlg)
          {
              if (!pPS->pDlg->UpdateData()) // THIS WORKS. THE DATA IS UPDATED.
              {  
                  AfxMessageBox("Did not update data.");
              }
          }
      }
  }
}

Location: SaveThese Class

void CSaveThese::OnBnClickedOk()
{
  // TODO: Add your control notification handler code here
  UpdateData(TRUE); // figures out which boxes are checked for saving
  CDirDialog dir;
  CSAPrefsDialog prefsDialog;
  if(dir.DoBrowse())
  {
      prefsDialog.m_strDirectorySavePath = dir.m_strPath;
  }

      // [ other if-statements like the one below to check the check boxes ]

  }
  if(m_bST_FS)
  {
      FSC_Main m_FS;
      m_FS.Save(prefsDialog.m_strDirectorySavePath);
  }
  OnOK();
}

Location: FSC_Main Class

void FSC_Main::Save(CString dirPath)
{
  if(Validate())
  {
      dirPath += "\\FS_Config.xml";
      FILE *fp = fopen(dirPath, "w+");
      WriteXML(fp);
      fclose(fp);
  }
}

By the time it gets to WriteXML, the values have either reverted back to their initialized values (empty strings and -1 for all combo boxes), or have strange values (empty for strings, and large numbers for combo boxes).

I imagine I just have something in the wrong place. I'm just not sure why this is happening, and it's really the biggest hurdle between me and really getting this project rolling.


Jon, Very hard to understand your code. What i understood is you have a member variable m_pages in CSAPrefsDialog dialog class which you are updating by calling UpdateData. And then you are creating a local variable CSaveThese m_sT and calling DoModal. Do you expect CSaveThese class should hold the values of m_Pages?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜