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. Combo Box Problem

Combo Box Problem

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
12 Posts 5 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.
  • M Offline
    M Offline
    mvworld
    wrote on last edited by
    #1

    Hello, Well I have this funny problem. I want to add items to my combo box in a child dialog box from my parent dialog box just before the child is shown. To do this I have the following line ChildDlg.m_ComboBox.AddString("Some Text"); I even tried ChildDlg.m_ComboBox.InsertString(0, "Some Text"); I made sure too that the member variable m_ComboBox in the class ChildDlg referred to the contro (not the value) of my Combo box. What is wrong? Thanks, Mike

    S M 2 Replies Last reply
    0
    • M mvworld

      Hello, Well I have this funny problem. I want to add items to my combo box in a child dialog box from my parent dialog box just before the child is shown. To do this I have the following line ChildDlg.m_ComboBox.AddString("Some Text"); I even tried ChildDlg.m_ComboBox.InsertString(0, "Some Text"); I made sure too that the member variable m_ComboBox in the class ChildDlg referred to the contro (not the value) of my Combo box. What is wrong? Thanks, Mike

      S Offline
      S Offline
      Sidney Chong
      wrote on last edited by
      #2

      Hi Mike, you have to tell us what the funny problem is. e.g. the app crashed (hm... thats really helpful), combo box not being updated, etc... Oh, and in which message handler are you calling the "AddString"/"InsertString" methods?

      M 1 Reply Last reply
      0
      • S Sidney Chong

        Hi Mike, you have to tell us what the funny problem is. e.g. the app crashed (hm... thats really helpful), combo box not being updated, etc... Oh, and in which message handler are you calling the "AddString"/"InsertString" methods?

        M Offline
        M Offline
        mvworld
        wrote on last edited by
        #3

        Well the application crashes. I have a main dialog box and in one of the functions I create a child dialog box which has the member variable m_ComboBox as public. So in my main dialog box I have CChildDialog ChildDlg; ChildDlg.m_ComboBox.AddString("Some Text"); ChildDlg.DoModal(); And then when I run this application, it crashes. So to test again I tried to Add a string to the combobox in the constructor of my child dialog but the application still crashed! What shall I do?

        M S 2 Replies Last reply
        0
        • M mvworld

          Well the application crashes. I have a main dialog box and in one of the functions I create a child dialog box which has the member variable m_ComboBox as public. So in my main dialog box I have CChildDialog ChildDlg; ChildDlg.m_ComboBox.AddString("Some Text"); ChildDlg.DoModal(); And then when I run this application, it crashes. So to test again I tried to Add a string to the combobox in the constructor of my child dialog but the application still crashed! What shall I do?

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

          Hello, the codegurus around the world.;) I think that DoModal() must call OnInitDialog() in CChildDialog. So, the location between ChilDlg and DoModal() will not create ComboBox yet. So, I think that you had better put AddString(...) in OnInitDialog() of CChildDialog.

          CChildDialog ChildDlg;
          ChildDlg.m_string = _T("Some Text");
          ChildDlg.DoModal();

          And in OnInitDialog() of ChildDlg

          //Already, m_string is updated!
          m_ComboBox.AddString (m_string);

          Have a nice day!

          -Masaaki Onishi-

          M 1 Reply Last reply
          0
          • M Masaaki Onishi

            Hello, the codegurus around the world.;) I think that DoModal() must call OnInitDialog() in CChildDialog. So, the location between ChilDlg and DoModal() will not create ComboBox yet. So, I think that you had better put AddString(...) in OnInitDialog() of CChildDialog.

            CChildDialog ChildDlg;
            ChildDlg.m_string = _T("Some Text");
            ChildDlg.DoModal();

            And in OnInitDialog() of ChildDlg

            //Already, m_string is updated!
            m_ComboBox.AddString (m_string);

            Have a nice day!

            -Masaaki Onishi-

            M Offline
            M Offline
            mvworld
            wrote on last edited by
            #5

            But there is no OnInitDialog() in my child dialog class!! Even when I tried to override OnInitDialog (hoping it was there in CDialog) it did not have it already defined in the base class.

            M 1 Reply Last reply
            0
            • M mvworld

              Well the application crashes. I have a main dialog box and in one of the functions I create a child dialog box which has the member variable m_ComboBox as public. So in my main dialog box I have CChildDialog ChildDlg; ChildDlg.m_ComboBox.AddString("Some Text"); ChildDlg.DoModal(); And then when I run this application, it crashes. So to test again I tried to Add a string to the combobox in the constructor of my child dialog but the application still crashed! What shall I do?

              S Offline
              S Offline
              Sidney Chong
              wrote on last edited by
              #6

              Well, here's the problem... the control hasn't been created yet! MFC's UI classes are really wrappers around the Win32 equivalents. Having constructed the class does not imply that the corresponding controls have been created. So what you want to do is the following: (1) create a data member (a string or an array of strings maybe?) in your child dialog to hold the data thats going into the combo box. (2) set this data member after you have created the child dialog but before the call to DoModal (DUH!) (3) override OnInitDialog of the child dialog (or OnCreate if its not a dialog) to pass this data into the combo box using AddString or InsertString, whichever you prefer. --update-- Oh bugger, Masaaki beat me to it..... ;p my link sux! :mad:

              M 1 Reply Last reply
              0
              • S Sidney Chong

                Well, here's the problem... the control hasn't been created yet! MFC's UI classes are really wrappers around the Win32 equivalents. Having constructed the class does not imply that the corresponding controls have been created. So what you want to do is the following: (1) create a data member (a string or an array of strings maybe?) in your child dialog to hold the data thats going into the combo box. (2) set this data member after you have created the child dialog but before the call to DoModal (DUH!) (3) override OnInitDialog of the child dialog (or OnCreate if its not a dialog) to pass this data into the combo box using AddString or InsertString, whichever you prefer. --update-- Oh bugger, Masaaki beat me to it..... ;p my link sux! :mad:

                M Offline
                M Offline
                mvworld
                wrote on last edited by
                #7

                Hey, I did as you told me. But now the application still mysteriously crashes. What I want to do is that just before the child appears, a function in the main dialog box (the parent) provides the child with stuff the child needs to show in the combo box. Since in the child I have a pointer to the parent, so I used it to call the function which should provide the child with the data for the combo box. But somehow the application crashes! Are we allowed to call other functions inside the OnInitDialog/OnInitInstance?

                S 1 Reply Last reply
                0
                • M mvworld

                  Hey, I did as you told me. But now the application still mysteriously crashes. What I want to do is that just before the child appears, a function in the main dialog box (the parent) provides the child with stuff the child needs to show in the combo box. Since in the child I have a pointer to the parent, so I used it to call the function which should provide the child with the data for the combo box. But somehow the application crashes! Are we allowed to call other functions inside the OnInitDialog/OnInitInstance?

                  S Offline
                  S Offline
                  Sidney Chong
                  wrote on last edited by
                  #8

                  Yes you are allowed to call other functions in those methods. But note that in your particular case, you should be using OnInitDialog instead of OnInitInstance -- sample pulled from MSDN -- IMPT NOTE: Remember to call the base class' OnInitDialog as shown in the sample!!

                  // MyDialog.cpp
                  #include "MyDialog.h"

                  BOOL CMyDialog::OnInitDialog()
                  {
                  //call the base implementation first!
                  CDialog::OnInitDialog();

                  // TODO: Add extra initialization here
                  m_cMyEdit.SetWindowText("My Name"); // Initialize control values
                  m_cMyList.ShowWindow(SW_HIDE); // Show or hide a control, etc.

                  return TRUE; // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
                  }

                  1 Reply Last reply
                  0
                  • M mvworld

                    But there is no OnInitDialog() in my child dialog class!! Even when I tried to override OnInitDialog (hoping it was there in CDialog) it did not have it already defined in the base class.

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

                    Hello, the codegurus around the world.;) You can manually add OnInitDialog() with WM_INITDIALOG. Or use WM_INITDIALOG by the class wizard, and this automatically generates the message and its handler in CChildDlg class.:rolleyes: Have a nice day!

                    -Masaaki Onishi-

                    1 Reply Last reply
                    0
                    • M mvworld

                      Hello, Well I have this funny problem. I want to add items to my combo box in a child dialog box from my parent dialog box just before the child is shown. To do this I have the following line ChildDlg.m_ComboBox.AddString("Some Text"); I even tried ChildDlg.m_ComboBox.InsertString(0, "Some Text"); I made sure too that the member variable m_ComboBox in the class ChildDlg referred to the contro (not the value) of my Combo box. What is wrong? Thanks, Mike

                      M Offline
                      M Offline
                      Michael Dunn
                      wrote on last edited by
                      #10

                      The other replies have been hit-and-miss, so I'll summarize here. You're confusing the dialog C++ object with the dialog window. The window does not get created until you call DoModal(). There is no dialog, and thus no controls to manipulate, before the DoModal() call. The proper way is to override OnInitDialog() in your dialog's C++ class. At the time WM_INITDIALOG is sent, the window and all child controls have been created, but not yet shown. So at that time, it's fine to set up the controls. --Mike-- http://home.inreach.com/mdunn/ While I can't be correct on all matters, I can make the reassuring claim that where I am inaccurate, I am at least definitively inaccurate. :love: your :bob: with :vegemite: and :beer: Sonork ID - 100.10414 AcidHelm

                      L 1 Reply Last reply
                      0
                      • M Michael Dunn

                        The other replies have been hit-and-miss, so I'll summarize here. You're confusing the dialog C++ object with the dialog window. The window does not get created until you call DoModal(). There is no dialog, and thus no controls to manipulate, before the DoModal() call. The proper way is to override OnInitDialog() in your dialog's C++ class. At the time WM_INITDIALOG is sent, the window and all child controls have been created, but not yet shown. So at that time, it's fine to set up the controls. --Mike-- http://home.inreach.com/mdunn/ While I can't be correct on all matters, I can make the reassuring claim that where I am inaccurate, I am at least definitively inaccurate. :love: your :bob: with :vegemite: and :beer: Sonork ID - 100.10414 AcidHelm

                        L Offline
                        L Offline
                        luckylourson
                        wrote on last edited by
                        #11

                        I don't understand why you don't fill the combo box after the DoModal()..It's not a properly way but it is sure.

                        M 1 Reply Last reply
                        0
                        • L luckylourson

                          I don't understand why you don't fill the combo box after the DoModal()..It's not a properly way but it is sure.

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

                          Hello, the codegurus around the world.;) Fisrt of all, we are talking about the initalization of the string in ComboBox. And, OnInitDialog() in the parent dialog of ComboBox is best location to do that. Second, the location after DoModal() means that ChildDlg will disapper since ChildDlg is the modal dialog like the message box. This means that as long as ChildDlg is opened (DoModal still works), we can add the new strings to this ComboBox inside or outside this dialog as long as we can get the right CWnd of ComboBox.:rolleyes: Have a nice day!

                          -Masaaki Onishi-

                          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