Note that there are some explanatory texts on larger screens.

plurals
  1. POConsolidating functions for cleaner code?
    text
    copied!<p>Don't be turned off by the length...I don't think it's too difficult of a problem.</p> <p><strong>Language:</strong> C++</p> <p><strong>Development Environment:</strong> Microsoft Visual C++</p> <p><strong>Libraries Used:</strong> MFC</p> <p><strong>Problem:</strong> I am creating a large preference dialog with several "pages". Many of them require the user to specify a file path. Right now, the user will click on the button, and it will jump to OnCommand(). This will verify that the command was from a button, then jump to the Browse() function where it will figure out which button was pressed. Finally, it will call FileDialog() which will launch a file chooser, then return the file path, assign it to the correct variable, and append it to the correct edit control.</p> <p>I am trying to consolidate all of these "open file" buttons into one class or function, but I'm not sure the best way to approach it. I would love for it to be clean so that I don't have to feed it specific IDs (2001, 2002, ...).</p> <p>Right now, these three functions (below) are in each of my files...this is messy and unnecessary. I want to have a single file called <strong>OpenFile.cpp</strong> or something that contains the necessary functions to handle opening a file, and <em>appending the chosen path to the correct text box within the correct dialog.</em></p> <pre><code>BOOL FSC_3DPersp::OnCommand(WPARAM wParam, LPARAM lParam) { if (HIWORD(wParam) == BN_CLICKED) { Browse(LOWORD(wParam)); return TRUE; } return CWnd::OnCommand(wParam, lParam); } </code></pre> <p>//</p> <pre><code>CString OpenFile::FileDialog(CWnd* wnd, int uiID) // dialog from which the call came and the ID of the edit control where the path is going { CFileDialog dlg( TRUE // Open = TRUE, Save = FALSE , NULL //filename extension , "" // initial filename , OFN_ENABLESIZING|OFN_EXPLORER|OFN_FILEMUSTEXIST // flags , "" // filter , wnd // parent window ); if (dlg.DoModal() == IDOK) { CEdit *Display; CString path = dlg.GetPathName(); Display = reinterpret_cast&lt;CEdit *&gt;(GetDlgItem(uiID)); Display-&gt;SetWindowText((LPCTSTR)path); return path; } } </code></pre> <p>//</p> <pre><code>void FSC_3DPersp::Browse(UINT uiID) { switch(uiID) { case IDC_BUTTON1: m_strPersTexture = FileDialog(this, 2004); break; case IDC_BUTTON2: m_strSkyFront = FileDialog(this, 2005); break; case IDC_BUTTON3: m_strSkyRight = FileDialog(this, 2006); break; case IDC_BUTTON4: m_strSkyBack = FileDialog(this, 2007); break; case IDC_BUTTON5: m_strSkyTop = FileDialog(this, 2008); break; case IDC_BUTTON6: m_strSkyLeft = FileDialog(this, 2009); break; case IDC_BUTTON7: m_strSkyBottom = FileDialog(this, 2010); break; } } </code></pre> <hr> <p><strong>Header File Definitions:</strong></p> <pre><code>afx_msg CString FileDialog(CWnd* wnd, int uiID); afx_msg void Browse(UINT uiID); virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam); </code></pre> <p>So how can I adjust the parameters to put all three of them into one file, and how would I reference them? If I did this, I feel like I need to add an additional parameter to the OnCommand() function, but I don't think I can do that.</p> <p>Thank you in advance for the help!</p> <p>~ Jon</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload