Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to save the filename retrieved from a dialog class using CMFCEditBrowseCtrl?

How to save the filename retrieved from a dialog class using CMFCEditBrowseCtrl?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lolici
    wrote on last edited by
    #1

    Hello! I'm trying to browse a text file using CMFCEditBrowseCtrl (it is used in CFirstDialog Class) and use this file in another Dialog Class (named CSecondDialog Class). My problem is on how to save the filename retrieved from the CMFCEditBrowseCtrl when the CFirstDialog::OnEnChangeMfceditbrowse1() goes out of scope and then pass the filename to the CSecondDialog. Here is the code:

    void CFirstDialog::OnEnChangeMfceditbrowse1()
    {
    CString str;
    m_browser.GetWindowText(str); //CMFCEditBrowseCtrl m_browser (variable)
    }

    void CSecondDialog::OnBnClickedOk()
    {
    CStdioFile fileSource("here I need to put the filename retrieved from the previous dialog class", CFile::modeRead);
    CStdioFile fileDest;
    CString strLine;
    CString strFile;
    int num = 1;

    while (fileSource.ReadString(strLine))
    {
    strFile.Format(L"over%d.txt", num++);
    fileDest.Open(strFile, CFile::modeWrite);
    fileDest.WriteString(strLine);
    fileDest.Close();
    }

    fileSource.Close();

    }

    L J 2 Replies Last reply
    0
    • L lolici

      Hello! I'm trying to browse a text file using CMFCEditBrowseCtrl (it is used in CFirstDialog Class) and use this file in another Dialog Class (named CSecondDialog Class). My problem is on how to save the filename retrieved from the CMFCEditBrowseCtrl when the CFirstDialog::OnEnChangeMfceditbrowse1() goes out of scope and then pass the filename to the CSecondDialog. Here is the code:

      void CFirstDialog::OnEnChangeMfceditbrowse1()
      {
      CString str;
      m_browser.GetWindowText(str); //CMFCEditBrowseCtrl m_browser (variable)
      }

      void CSecondDialog::OnBnClickedOk()
      {
      CStdioFile fileSource("here I need to put the filename retrieved from the previous dialog class", CFile::modeRead);
      CStdioFile fileDest;
      CString strLine;
      CString strFile;
      int num = 1;

      while (fileSource.ReadString(strLine))
      {
      strFile.Format(L"over%d.txt", num++);
      fileDest.Open(strFile, CFile::modeWrite);
      fileDest.WriteString(strLine);
      fileDest.Close();
      }

      fileSource.Close();

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The OnEnChangeMfceditbrowse1 method should store the file name in one of its object variables (or str) should be an object variable. The calling code can then capture that variable when the dialog returns.

      1 Reply Last reply
      0
      • L lolici

        Hello! I'm trying to browse a text file using CMFCEditBrowseCtrl (it is used in CFirstDialog Class) and use this file in another Dialog Class (named CSecondDialog Class). My problem is on how to save the filename retrieved from the CMFCEditBrowseCtrl when the CFirstDialog::OnEnChangeMfceditbrowse1() goes out of scope and then pass the filename to the CSecondDialog. Here is the code:

        void CFirstDialog::OnEnChangeMfceditbrowse1()
        {
        CString str;
        m_browser.GetWindowText(str); //CMFCEditBrowseCtrl m_browser (variable)
        }

        void CSecondDialog::OnBnClickedOk()
        {
        CStdioFile fileSource("here I need to put the filename retrieved from the previous dialog class", CFile::modeRead);
        CStdioFile fileDest;
        CString strLine;
        CString strFile;
        int num = 1;

        while (fileSource.ReadString(strLine))
        {
        strFile.Format(L"over%d.txt", num++);
        fileDest.Open(strFile, CFile::modeWrite);
        fileDest.WriteString(strLine);
        fileDest.Close();
        }

        fileSource.Close();

        }

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        It depends on how your dialog classes can be accessed and if they both exist at the same time. A possible solution: You need to store pointers to your dialogs when they are created (usually in the parent window). Store the file name in a member variable of CFirstDialog and provide a public function to get the name:

        CFirstDialog
        {
        public:
        // ...
        const CString& GetFileName() const { return m_strFileName; }
        protected:
        // ...
        CString m_strFileName;
        };

        void CFirstDialog::OnEnChangeMfceditbrowse1()
        {
        m_browser.GetWindowText(m_strFileName);
        }

        Similar for CSecondDialog but there with a set function:

        CSecondDialog
        {
        public:
        // ...
        void SetFileName(const CString& strFileName) { m_strFileName = strFileName; }
        protected:
        // ...
        CString m_strFileName;
        };

        You can now get and set the name from the class that holds the pointers to the dialogs. If both dialogs might not exist at the same time you have to store the file name in a member variable of that class (e.g. get the name when DoModal() returns). Finally you need access to the upper class from CSecondDialog. If the upper class is the parent window, always of the same type, and has been passed as parent, you can use casting:

        void CSecondDialog::OnBnClickedOk()
        {
        CParentOfDialogs *pParent = static_cast(GetParent());
        // Use pParent here to get the stored name or retrieve it from the first dialog
        CString strFileName = pParent->m_pFirstDialog->GetFileName();
        }

        Otherwise you have to use a member variable in your second dialog that holds a pointer to the upper class and is initalised upon dialog creation or using a set function. There are also other solutions and variations of the above.

        L 1 Reply Last reply
        0
        • J Jochen Arndt

          It depends on how your dialog classes can be accessed and if they both exist at the same time. A possible solution: You need to store pointers to your dialogs when they are created (usually in the parent window). Store the file name in a member variable of CFirstDialog and provide a public function to get the name:

          CFirstDialog
          {
          public:
          // ...
          const CString& GetFileName() const { return m_strFileName; }
          protected:
          // ...
          CString m_strFileName;
          };

          void CFirstDialog::OnEnChangeMfceditbrowse1()
          {
          m_browser.GetWindowText(m_strFileName);
          }

          Similar for CSecondDialog but there with a set function:

          CSecondDialog
          {
          public:
          // ...
          void SetFileName(const CString& strFileName) { m_strFileName = strFileName; }
          protected:
          // ...
          CString m_strFileName;
          };

          You can now get and set the name from the class that holds the pointers to the dialogs. If both dialogs might not exist at the same time you have to store the file name in a member variable of that class (e.g. get the name when DoModal() returns). Finally you need access to the upper class from CSecondDialog. If the upper class is the parent window, always of the same type, and has been passed as parent, you can use casting:

          void CSecondDialog::OnBnClickedOk()
          {
          CParentOfDialogs *pParent = static_cast(GetParent());
          // Use pParent here to get the stored name or retrieve it from the first dialog
          CString strFileName = pParent->m_pFirstDialog->GetFileName();
          }

          Otherwise you have to use a member variable in your second dialog that holds a pointer to the upper class and is initalised upon dialog creation or using a set function. There are also other solutions and variations of the above.

          L Offline
          L Offline
          lolici
          wrote on last edited by
          #4

          Is m_pFirstDialog a new variable?

          Jochen Arndt wrote:

          If both dialogs might not exist at the same time you have to store the file name in a member variable of that class (e.g. get the name when DoModal() returns).

          The two dialogs do not exist at the same time, actually CSecondDialog gets on screen after I finish with CFirstDialog which goes out of scope. Could you explain me how to get the file name after DoModal() returns? Thank you in advance!

          void CInputView::OnFirstDlg()
          {
          // TODO: Add your command handler code here
          CInputDoc* pDoc = GetDocument();
          CFirstDialog DialogWindow;
          //.....
          CSecondDialog *pDlg = new CSecondDialog(this);
          //...
          pDlg->DoModal();
          delete pDlg;
          }

          void CMyView::OnSecondDlg()
          {
          // TODO: Add your command handler code here
          CMyDoc* pDoc = GetDocument();
          CSecondDialog DialogWindow;
          DialogWindow.DoModal();

          }

          J 1 Reply Last reply
          0
          • L lolici

            Is m_pFirstDialog a new variable?

            Jochen Arndt wrote:

            If both dialogs might not exist at the same time you have to store the file name in a member variable of that class (e.g. get the name when DoModal() returns).

            The two dialogs do not exist at the same time, actually CSecondDialog gets on screen after I finish with CFirstDialog which goes out of scope. Could you explain me how to get the file name after DoModal() returns? Thank you in advance!

            void CInputView::OnFirstDlg()
            {
            // TODO: Add your command handler code here
            CInputDoc* pDoc = GetDocument();
            CFirstDialog DialogWindow;
            //.....
            CSecondDialog *pDlg = new CSecondDialog(this);
            //...
            pDlg->DoModal();
            delete pDlg;
            }

            void CMyView::OnSecondDlg()
            {
            // TODO: Add your command handler code here
            CMyDoc* pDoc = GetDocument();
            CSecondDialog DialogWindow;
            DialogWindow.DoModal();

            }

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            Add a member variable to the first dialog (as already described) and to the parent window. Then get the file name from the dialog after calling:

            void CInputView::OnFirstDlg()
            {
            CFirstDialog DialogWindow;
            DialogWindow.DoModal();
            // Get file name here before DialogWindow goes out of scope
            m_strFileName = DialogWindow.GetFileName();
            // Or (when DialogWindow.m_strFileName is public):
            //m_strFileName = DialogWindow.m_strFileName
            }

            If you create the second dialog from another class (not CInputView), you have to pass the name to the other class in a similar way. Then set the file name before calling DoModal() of the second dialog:

            CSecondDialog DialogWindow;
            // Pass file name before calling DoModal()
            DialogWindow.SetFileName(m_strFileName);
            DialogWindow.DoModal();

            L 2 Replies Last reply
            0
            • J Jochen Arndt

              Add a member variable to the first dialog (as already described) and to the parent window. Then get the file name from the dialog after calling:

              void CInputView::OnFirstDlg()
              {
              CFirstDialog DialogWindow;
              DialogWindow.DoModal();
              // Get file name here before DialogWindow goes out of scope
              m_strFileName = DialogWindow.GetFileName();
              // Or (when DialogWindow.m_strFileName is public):
              //m_strFileName = DialogWindow.m_strFileName
              }

              If you create the second dialog from another class (not CInputView), you have to pass the name to the other class in a similar way. Then set the file name before calling DoModal() of the second dialog:

              CSecondDialog DialogWindow;
              // Pass file name before calling DoModal()
              DialogWindow.SetFileName(m_strFileName);
              DialogWindow.DoModal();

              L Offline
              L Offline
              lolici
              wrote on last edited by
              #6

              I create the second dialog from the CInputView (the same class). I wrote it wrong above. Thank you very much for your help!I'm grateful!

              1 Reply Last reply
              0
              • J Jochen Arndt

                Add a member variable to the first dialog (as already described) and to the parent window. Then get the file name from the dialog after calling:

                void CInputView::OnFirstDlg()
                {
                CFirstDialog DialogWindow;
                DialogWindow.DoModal();
                // Get file name here before DialogWindow goes out of scope
                m_strFileName = DialogWindow.GetFileName();
                // Or (when DialogWindow.m_strFileName is public):
                //m_strFileName = DialogWindow.m_strFileName
                }

                If you create the second dialog from another class (not CInputView), you have to pass the name to the other class in a similar way. Then set the file name before calling DoModal() of the second dialog:

                CSecondDialog DialogWindow;
                // Pass file name before calling DoModal()
                DialogWindow.SetFileName(m_strFileName);
                DialogWindow.DoModal();

                L Offline
                L Offline
                lolici
                wrote on last edited by
                #7

                Could you please explain me how to store pointers to the dialogs when they are created so that I can get and set the name from the class that holds the pointers to the dialogs? I'm sorry for asking again but I got a little confused with this part.

                J 1 Reply Last reply
                0
                • L lolici

                  Could you please explain me how to store pointers to the dialogs when they are created so that I can get and set the name from the class that holds the pointers to the dialogs? I'm sorry for asking again but I got a little confused with this part.

                  J Offline
                  J Offline
                  Jochen Arndt
                  wrote on last edited by
                  #8

                  Such is only useful with modeless dialogs. Modal dialogs will block so that there is usually no need to have a pointer. However:

                  // With class member CSomeDialog *m_pSomeDlg
                  if (NULL == m_pSomeDlg)
                  {
                  m_pSomeDlg = new CSomeDialog(this);
                  }

                  When doing so m_pSomeDlg must be initialised with NULL in the constructor and deleted in the destructor.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups