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. C / C++ / MFC
  3. Filling a combo box from outside its dialog

Filling a combo box from outside its dialog

Scheduled Pinned Locked Moved C / C++ / MFC
4 Posts 4 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.
  • W Offline
    W Offline
    Wayne Janaway
    wrote on last edited by
    #1

    I have a droplist combobox in a modal dlg called from mainfrm in an SDI. The combobox must obtain its initial selection ( a CString ) from the selected text in a listcontrol in a dlgbar, also in mainfrm. How do I accomplish this?

    D M R 3 Replies Last reply
    0
    • W Wayne Janaway

      I have a droplist combobox in a modal dlg called from mainfrm in an SDI. The combobox must obtain its initial selection ( a CString ) from the selected text in a listcontrol in a dlgbar, also in mainfrm. How do I accomplish this?

      D Offline
      D Offline
      David Fleming
      wrote on last edited by
      #2

      You should be able to get a pointer to the ComboBox (use .GetDlgItem(ID_OF_COMBO)). Then with that pointer you should be able to access the members and functions of the ComboBox (comboBox->...). Here's an example: CDialog dlgModal; //initialize the dialog you are about to show. CComboBox* pCBox = dlgModal.GetDlgItem(ID_OF_COMBO); //get pointer to the ComboBox. int nDx = pCBox->FindString(-1, "string to find"); //get index of the item you want selected //the -1 tells it to search from the beginning ...and whatever else you need to do. ... dlgModal.DoModal(); //display the dialog I've not done anything quite like what you are attempting, but you should be able to get a pointer to any control on the form and use that pointer to access its members and functions. I hope that helps. David.

      1 Reply Last reply
      0
      • W Wayne Janaway

        I have a droplist combobox in a modal dlg called from mainfrm in an SDI. The combobox must obtain its initial selection ( a CString ) from the selected text in a listcontrol in a dlgbar, also in mainfrm. How do I accomplish this?

        M Offline
        M Offline
        Masaaki Onishi
        wrote on last edited by
        #3

        Hello, the codegurus around the world.;) The easiest way to satisfy your requirement is to put public member value like m_sCurSelectedString in CMyMainFrame.

        CMyMainFrame is the parent window of the dialog, dialog bar with ListBox.
        I guess that ListBox is the same child level of the dialog bar since ListBox is declared
        at CMyMainFrame class?

        m_sCurSelectedString is always updated if the user updates the selection of ListBox.
        So, when the dialog is opened, m_sCurSelectedString compares the string in ComboBox
        in OnInitDialog().

        So, how do we access m_sCurSelectedString from Dialog Box?

        1. Pass the parent's CWnd of dialog constructor arguement. or
        2. Use CWnd::GetParent() or
        3. Use AfxGetMainWnd() and cast the pointer of CMyMainFrame.

        And, how do we update m_sCurSelectedString of ListBox.
        I think that we can use MessageMap of ListBox in CMyMainFrame.

        The other way is that we can pass CWnd of ListBox on Dialog Bar
        to the consturctor arguemnt of CMyDialog.
        At this case, we can direclty access the currnet selected item of ListBox.:cool:

        I think that there are several other ways to do this.:eek: Have a nice day! -Masaaki Onishi-

        1 Reply Last reply
        0
        • W Wayne Janaway

          I have a droplist combobox in a modal dlg called from mainfrm in an SDI. The combobox must obtain its initial selection ( a CString ) from the selected text in a listcontrol in a dlgbar, also in mainfrm. How do I accomplish this?

          R Offline
          R Offline
          realJSOP
          wrote on last edited by
          #4

          In the app class header file, after the app class definition, add the folowing line:

          extern CMyApp theApp;

          Add these declarations to your app class:

          private:
          CMainFrame* m_pMainFrame;
          public:
          CMainFrame* GetMainFramePtr() { return m_pMainFrame);

          In the OnInitInstance function of your app class, do this AFTER the main window is created:

          m_pMainFrame = pMainFrame;

          In the CMainFrame class, put a function that accepts a CComboBox* like so:

          int CMainFrame::FillComboBox(CComboBox* pCombo)
          {
          // fill the combo box
          // and return the number of things you put in it
          return pCombo->GetCount();
          }

          From within the OnInitDialog() function in the dialog box class, call your function like so:

          theApp.GetMainFrame()->FillComboBox(&m\_ctrlComboBox);
          

          I'm assuming that your dialog box class already has #include "MyApp.h" in it. :)

          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