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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ComboBox (and/or ListBox) shared in two dialogs (MFC)

ComboBox (and/or ListBox) shared in two dialogs (MFC)

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestionannouncementlearning
5 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.
  • J Offline
    J Offline
    J B 0
    wrote on last edited by
    #1

    Hello guys, I need some suggestions on this. I have two dialogs CDialog1 and CDialog2. CDialog1 has a ComboBox that contains a list of values. CDialog2 has a ListBox. The CDialog1 calls up CDialog2 using DoModal(). When the CDialog2 is up, its ListBox should be filled with the listed values in the ComboBox of the CDialog1 (of course, this would be done in the CDialog2's OnInitDialog()). Then in the CDialog2, the items in the ListBox can be changed and as soon as this dialog is closed (EndDialog()), the values in the ListBox will go back and replace the ones in the CDialog1's ComboBox. As you can see, I need to either share these two members (likely via the use of DDX control) between the two dialogs or have the CDialog1's ComboBox fully accessible by the CDialog2. I tried to create paired members (CComboBox or CListBox) in the other dialog for the original member, duplicate a copy and create functions to update the list on both sides whenever necessary. But the copying didn't work. I couldn't go, for example, in CDialog1 before calling DoModal,

    CDialog2 dlg2;
    dlg2.m_ComboBox = dlg1.m_ComboBox;
    

    The other way I could think of is to use SendMessage() to retieve/update the ComboBox' values from CDialog2 to CDialog1. Or, I could simply make the ComboBox globally accessible through pointer for CDialog2. Before I try anything, I'd really like to hear from anyone. What would be the best way (MS recommended way?)to go about it? Or could my first method be corrected? Thanks alot

    R R 2 Replies Last reply
    0
    • J J B 0

      Hello guys, I need some suggestions on this. I have two dialogs CDialog1 and CDialog2. CDialog1 has a ComboBox that contains a list of values. CDialog2 has a ListBox. The CDialog1 calls up CDialog2 using DoModal(). When the CDialog2 is up, its ListBox should be filled with the listed values in the ComboBox of the CDialog1 (of course, this would be done in the CDialog2's OnInitDialog()). Then in the CDialog2, the items in the ListBox can be changed and as soon as this dialog is closed (EndDialog()), the values in the ListBox will go back and replace the ones in the CDialog1's ComboBox. As you can see, I need to either share these two members (likely via the use of DDX control) between the two dialogs or have the CDialog1's ComboBox fully accessible by the CDialog2. I tried to create paired members (CComboBox or CListBox) in the other dialog for the original member, duplicate a copy and create functions to update the list on both sides whenever necessary. But the copying didn't work. I couldn't go, for example, in CDialog1 before calling DoModal,

      CDialog2 dlg2;
      dlg2.m_ComboBox = dlg1.m_ComboBox;
      

      The other way I could think of is to use SendMessage() to retieve/update the ComboBox' values from CDialog2 to CDialog1. Or, I could simply make the ComboBox globally accessible through pointer for CDialog2. Before I try anything, I'd really like to hear from anyone. What would be the best way (MS recommended way?)to go about it? Or could my first method be corrected? Thanks alot

      R Offline
      R Offline
      RChin
      wrote on last edited by
      #2

      J.B. I can only suggest the method that I have used in the past, so this may not ideally fit your requirements exactly. J.B. wrote: As you can see, I need to either share these two members (likely via the use of DDX control) between the two dialogs or have the CDialog1's ComboBox fully accessible by the CDialog2. Directly accessing another objects UI control is for me a general no, no! This applies to worker threads as well as separate UI classes. Share your information via a public member variable, declared in the dialog class being called. Since you are dealing with a number of distinct items, I would go for a container variable, such as an stl's vector class.

      • Before calling DoModal(), fill the instantiated dialog class's vector collection with the list of items.
      • In your OnInitDialog() overridden function, populate your now valid listbox/list control, with this member vector.
      • On successful termination of your dialog (OnOk), modify the public vector variable, if any changes are needed.
      • In your calling dialog function, use the returned vector of items to modify its control display.

      This method has worked for me in the past, so you may find it useful in your solution.


      I Dream of Absolute Zero

      J 1 Reply Last reply
      0
      • R RChin

        J.B. I can only suggest the method that I have used in the past, so this may not ideally fit your requirements exactly. J.B. wrote: As you can see, I need to either share these two members (likely via the use of DDX control) between the two dialogs or have the CDialog1's ComboBox fully accessible by the CDialog2. Directly accessing another objects UI control is for me a general no, no! This applies to worker threads as well as separate UI classes. Share your information via a public member variable, declared in the dialog class being called. Since you are dealing with a number of distinct items, I would go for a container variable, such as an stl's vector class.

        • Before calling DoModal(), fill the instantiated dialog class's vector collection with the list of items.
        • In your OnInitDialog() overridden function, populate your now valid listbox/list control, with this member vector.
        • On successful termination of your dialog (OnOk), modify the public vector variable, if any changes are needed.
        • In your calling dialog function, use the returned vector of items to modify its control display.

        This method has worked for me in the past, so you may find it useful in your solution.


        I Dream of Absolute Zero

        J Offline
        J Offline
        J B 0
        wrote on last edited by
        #3

        Thanks RChin! I shall try that~

        1 Reply Last reply
        0
        • J J B 0

          Hello guys, I need some suggestions on this. I have two dialogs CDialog1 and CDialog2. CDialog1 has a ComboBox that contains a list of values. CDialog2 has a ListBox. The CDialog1 calls up CDialog2 using DoModal(). When the CDialog2 is up, its ListBox should be filled with the listed values in the ComboBox of the CDialog1 (of course, this would be done in the CDialog2's OnInitDialog()). Then in the CDialog2, the items in the ListBox can be changed and as soon as this dialog is closed (EndDialog()), the values in the ListBox will go back and replace the ones in the CDialog1's ComboBox. As you can see, I need to either share these two members (likely via the use of DDX control) between the two dialogs or have the CDialog1's ComboBox fully accessible by the CDialog2. I tried to create paired members (CComboBox or CListBox) in the other dialog for the original member, duplicate a copy and create functions to update the list on both sides whenever necessary. But the copying didn't work. I couldn't go, for example, in CDialog1 before calling DoModal,

          CDialog2 dlg2;
          dlg2.m_ComboBox = dlg1.m_ComboBox;
          

          The other way I could think of is to use SendMessage() to retieve/update the ComboBox' values from CDialog2 to CDialog1. Or, I could simply make the ComboBox globally accessible through pointer for CDialog2. Before I try anything, I'd really like to hear from anyone. What would be the best way (MS recommended way?)to go about it? Or could my first method be corrected? Thanks alot

          R Offline
          R Offline
          Rafael Fernandez Lopez
          wrote on last edited by
          #4

          I'd use a public CStringArray object. It would save all the entries in the CDialog1 and you should write something like this for the CDialog2::OnInitDialog(): (Imagine that your CStringArray object has been called "my_array") for (int i=0; i

          Written by: Rafael Fernández López.

          Visit: http://www.maestroprogramador.com

          J 1 Reply Last reply
          0
          • R Rafael Fernandez Lopez

            I'd use a public CStringArray object. It would save all the entries in the CDialog1 and you should write something like this for the CDialog2::OnInitDialog(): (Imagine that your CStringArray object has been called "my_array") for (int i=0; i

            Written by: Rafael Fernández López.

            Visit: http://www.maestroprogramador.com

            J Offline
            J Offline
            J B 0
            wrote on last edited by
            #5

            Thanks Rafael, for the suggestion. at the end I use std::vector as that way I'm able to not specify the size and that gives a bit more freedom to implement the number of elements restriction on ComboBox, if required.

            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