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 access form controls in another view

how to access form controls in another view

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
7 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.
  • E Offline
    E Offline
    elephantstar
    wrote on last edited by
    #1

    Hi there, I have a class called CChecklist that derives from CFormview and a checkmark image that I made invisible until the user finishes entry of another view called CFruits. How can I access IDC_CHECKMARK from CFruits class? I tried creating a object of class CChecklist to access its member function in order to change the visibility property of the control, but it gave me a run-time error. Please see below. How can I accomplish this? void CCheckList::makeVisible() { GetDlgItem(IDC_CHECKMARK)->ShowWindow(TRUE); } void CFruits::OnOK() { CChecklist cl; cl.makeVisible(); //run-time error PostMessage(WM_COMMAND,ID_FILE_CLOSE); }

    C J 2 Replies Last reply
    0
    • E elephantstar

      Hi there, I have a class called CChecklist that derives from CFormview and a checkmark image that I made invisible until the user finishes entry of another view called CFruits. How can I access IDC_CHECKMARK from CFruits class? I tried creating a object of class CChecklist to access its member function in order to change the visibility property of the control, but it gave me a run-time error. Please see below. How can I accomplish this? void CCheckList::makeVisible() { GetDlgItem(IDC_CHECKMARK)->ShowWindow(TRUE); } void CFruits::OnOK() { CChecklist cl; cl.makeVisible(); //run-time error PostMessage(WM_COMMAND,ID_FILE_CLOSE); }

      C Offline
      C Offline
      Cohen
      wrote on last edited by
      #2

      The CChecklist object you create in the CFruits::OnOK() isn't the same you have subclassed somewhere else in your code. That object isn't subclassed at all and I suppose that this is why you get that runtime error. You could try to do something like this (ain't that pretty though and I'm not sure does this work):

      void CFruits::OnOK()
      {
      
      // Obtain handle to checklists parent here
      
      HWND hCheckMark =
      ::GetDlgItem( ::GetDlgItem(handle_to_checklists_parent, IDC_CHECKLIST), IDC_CHECKMARK );
      
      ::ShowWindow( hCheckMark, SW_SHOW );
      PostMessage(WM_COMMAND,ID_FILE_CLOSE);
      }
      

      Cohen

      1 Reply Last reply
      0
      • E elephantstar

        Hi there, I have a class called CChecklist that derives from CFormview and a checkmark image that I made invisible until the user finishes entry of another view called CFruits. How can I access IDC_CHECKMARK from CFruits class? I tried creating a object of class CChecklist to access its member function in order to change the visibility property of the control, but it gave me a run-time error. Please see below. How can I accomplish this? void CCheckList::makeVisible() { GetDlgItem(IDC_CHECKMARK)->ShowWindow(TRUE); } void CFruits::OnOK() { CChecklist cl; cl.makeVisible(); //run-time error PostMessage(WM_COMMAND,ID_FILE_CLOSE); }

        J Offline
        J Offline
        Johan Rosengren
        wrote on last edited by
        #3

        makeVisible will try to find a child-control of CCheckList with the id IDC_CHECKMARK. Furthermore, you are creating an instance of CChecklist out of the blue - this instance has nothing to do with your other view/control. You should not try to access controls between views, this makes it impossible to move one view to another application, and is considered Real Bad Programming. If you have two views, I assume that you have a single document holding both of them? In that case, the proper way to go about this is via the CDocument update-mechanism. You do this by calling CDocument::OnUpdateAllViews when you want to change stuff across view-boundaries, in (pseudo)code:

        void CFirstView::SomeFunction()
        {
        // some code making it necessary to change some control
        // in another view...

        CSomeDoc* pDoc = GetDocument();
        pDoc->UpdateAllViews( this, HINT_SOME_VALUE_YOU_DEFINE );

        }

        and in the recipient view, you add an update handler, so:

        void CSecondView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
        {
        if( lHint == HINT_SOME_VALUE_YOU_DEFINE )
        {
        // Do your stuff
        }
        }

        You should also get a basic textbook on C++, you'll have to learn the difference between a class and an instance of a class, for example. Lots of grief lies ahead if you don't know this :-)

        E 1 Reply Last reply
        0
        • J Johan Rosengren

          makeVisible will try to find a child-control of CCheckList with the id IDC_CHECKMARK. Furthermore, you are creating an instance of CChecklist out of the blue - this instance has nothing to do with your other view/control. You should not try to access controls between views, this makes it impossible to move one view to another application, and is considered Real Bad Programming. If you have two views, I assume that you have a single document holding both of them? In that case, the proper way to go about this is via the CDocument update-mechanism. You do this by calling CDocument::OnUpdateAllViews when you want to change stuff across view-boundaries, in (pseudo)code:

          void CFirstView::SomeFunction()
          {
          // some code making it necessary to change some control
          // in another view...

          CSomeDoc* pDoc = GetDocument();
          pDoc->UpdateAllViews( this, HINT_SOME_VALUE_YOU_DEFINE );

          }

          and in the recipient view, you add an update handler, so:

          void CSecondView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
          {
          if( lHint == HINT_SOME_VALUE_YOU_DEFINE )
          {
          // Do your stuff
          }
          }

          You should also get a basic textbook on C++, you'll have to learn the difference between a class and an instance of a class, for example. Lots of grief lies ahead if you don't know this :-)

          E Offline
          E Offline
          elephantstar
          wrote on last edited by
          #4

          I did just as you suggested but I might have missed something. Here are my code samples just in case you can pinpoint the problem. //Main application class BOOL CMPSApp::InitInstance() { /* additional code present */ CMultiDocTemplate* pFirstView = new CMultiDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMyDoc), RUNTIME_CLASS(CMDIChildWnd), RUNTIME_CLASS(CFirstView)); AddDocTemplate(pMHCEditDocTemplate); CMultiDocTemplate* pFirstView = new CMultiDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMyDoc), RUNTIME_CLASS(CMDIChildWnd), RUNTIME_CLASS(CSecondView)); AddDocTemplate(pMHCEditDocTemplate); } void CFirstView::OnOK() { if (g_selected == TRUE) { CDocument* pDoc = GetDocument(); pDoc->UpdateAllViews(NULL); } PostMessage(WM_COMMAND, ID_FILE_CLOSE); } void CSecondView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) { If (g_list->items.selected == 0) { GetDlgItem(IDC_APPLE)->ShowWindow(TRUE); } } When UpdateAllViews() is called, there is only one view in the list and that is CFirstView, the calling view. Why does it not recognize the other views? Am I missing something? Thanks!

          J 1 Reply Last reply
          0
          • E elephantstar

            I did just as you suggested but I might have missed something. Here are my code samples just in case you can pinpoint the problem. //Main application class BOOL CMPSApp::InitInstance() { /* additional code present */ CMultiDocTemplate* pFirstView = new CMultiDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMyDoc), RUNTIME_CLASS(CMDIChildWnd), RUNTIME_CLASS(CFirstView)); AddDocTemplate(pMHCEditDocTemplate); CMultiDocTemplate* pFirstView = new CMultiDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMyDoc), RUNTIME_CLASS(CMDIChildWnd), RUNTIME_CLASS(CSecondView)); AddDocTemplate(pMHCEditDocTemplate); } void CFirstView::OnOK() { if (g_selected == TRUE) { CDocument* pDoc = GetDocument(); pDoc->UpdateAllViews(NULL); } PostMessage(WM_COMMAND, ID_FILE_CLOSE); } void CSecondView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) { If (g_list->items.selected == 0) { GetDlgItem(IDC_APPLE)->ShowWindow(TRUE); } } When UpdateAllViews() is called, there is only one view in the list and that is CFirstView, the calling view. Why does it not recognize the other views? Am I missing something? Thanks!

            J Offline
            J Offline
            Johan Rosengren
            wrote on last edited by
            #5

            You are not only trying to send data between two views, they reside in different documents as well. First, you have to get the doc templates from, the app, loop them and get the documents from each doc template. Well, it's easier to explain in code :-)

            POSITION templatePosition = AfxGetApp()->GetFirstDocTemplatePosition();
            while( templatePosition != NULL )
            {
            CDocTemplate* documentTemplate = AfxGetApp()->GetNextDocTemplate( templatePosition );
            if( documentTemplate )
            {
            POSITION documentPosition = documentTemplate->GetFirstDocPosition();
            while( documentPosition != NULL )
            {
            CDocument* document = documentTemplate->GetNextDoc( documentPosition );
            if( document )
            document->UpdateAllViews( NULL );
            }
            }
            }

            Now, OnUpdate will be called for all views.

            E 1 Reply Last reply
            0
            • J Johan Rosengren

              You are not only trying to send data between two views, they reside in different documents as well. First, you have to get the doc templates from, the app, loop them and get the documents from each doc template. Well, it's easier to explain in code :-)

              POSITION templatePosition = AfxGetApp()->GetFirstDocTemplatePosition();
              while( templatePosition != NULL )
              {
              CDocTemplate* documentTemplate = AfxGetApp()->GetNextDocTemplate( templatePosition );
              if( documentTemplate )
              {
              POSITION documentPosition = documentTemplate->GetFirstDocPosition();
              while( documentPosition != NULL )
              {
              CDocument* document = documentTemplate->GetNextDoc( documentPosition );
              if( document )
              document->UpdateAllViews( NULL );
              }
              }
              }

              Now, OnUpdate will be called for all views.

              E Offline
              E Offline
              elephantstar
              wrote on last edited by
              #6

              It works beautifully! Thanks Johan!

              J 1 Reply Last reply
              0
              • E elephantstar

                It works beautifully! Thanks Johan!

                J Offline
                J Offline
                Johan Rosengren
                wrote on last edited by
                #7

                My pleasure :-)

                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