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. CComboBox in a Modeless Dialog

CComboBox in a Modeless Dialog

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearning
5 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.
  • S Offline
    S Offline
    softwaremonkey
    wrote on last edited by
    #1

    OK, I'm going mad here. I have a dialog class which I am creating from a dialog resource and displaying as modeless. I have data members assigned to the various controls and most of them work fine, except the CComboBox data member. I am trying to add some string data to the control in the CMyDialog::OnCreate(LPCREATESTRUCT lpCreateStruct) but it asserts because the control's hwnd is null. Do I need to do anything special with a CComboBox? My code is like this...

    int CFlushDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CDialog::OnCreate(lpCreateStruct) == -1)
    return -1;

    SetWindowPos(NULL, m\_nXPos, m\_nYPos, 0, 0, SWP\_NOSIZE | SWP\_NOZORDER);
    
    m\_comboDataMember.AddString("Hello");   // BOOM!
        // Other data members CEdit, CButton work OK
    return 0;
    

    }

    I display the dialog like this...

     m\_flushDialog.Create(IDD\_FLUSH\_DLG, this);
     m\_flushDialog.ShowWindow(SW\_SHOW);
    

    Anyone have any ideas? :confused: Tony

    M L A 3 Replies Last reply
    0
    • S softwaremonkey

      OK, I'm going mad here. I have a dialog class which I am creating from a dialog resource and displaying as modeless. I have data members assigned to the various controls and most of them work fine, except the CComboBox data member. I am trying to add some string data to the control in the CMyDialog::OnCreate(LPCREATESTRUCT lpCreateStruct) but it asserts because the control's hwnd is null. Do I need to do anything special with a CComboBox? My code is like this...

      int CFlushDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
      {
      if (CDialog::OnCreate(lpCreateStruct) == -1)
      return -1;

      SetWindowPos(NULL, m\_nXPos, m\_nYPos, 0, 0, SWP\_NOSIZE | SWP\_NOZORDER);
      
      m\_comboDataMember.AddString("Hello");   // BOOM!
          // Other data members CEdit, CButton work OK
      return 0;
      

      }

      I display the dialog like this...

       m\_flushDialog.Create(IDD\_FLUSH\_DLG, this);
       m\_flushDialog.ShowWindow(SW\_SHOW);
      

      Anyone have any ideas? :confused: Tony

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      Why don't you add the data to the combobox in the OnInitDialog method ?

      Watched code never compiles.

      1 Reply Last reply
      0
      • S softwaremonkey

        OK, I'm going mad here. I have a dialog class which I am creating from a dialog resource and displaying as modeless. I have data members assigned to the various controls and most of them work fine, except the CComboBox data member. I am trying to add some string data to the control in the CMyDialog::OnCreate(LPCREATESTRUCT lpCreateStruct) but it asserts because the control's hwnd is null. Do I need to do anything special with a CComboBox? My code is like this...

        int CFlushDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
        {
        if (CDialog::OnCreate(lpCreateStruct) == -1)
        return -1;

        SetWindowPos(NULL, m\_nXPos, m\_nYPos, 0, 0, SWP\_NOSIZE | SWP\_NOZORDER);
        
        m\_comboDataMember.AddString("Hello");   // BOOM!
            // Other data members CEdit, CButton work OK
        return 0;
        

        }

        I display the dialog like this...

         m\_flushDialog.Create(IDD\_FLUSH\_DLG, this);
         m\_flushDialog.ShowWindow(SW\_SHOW);
        

        Anyone have any ideas? :confused: Tony

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

        I think you will find that your ComboBox does not get created until after the dialog creation has completed. Use the OnInitDialog() function to handle intialisation of dialog controls.

        The best things in life are not things.

        S 1 Reply Last reply
        0
        • L Lost User

          I think you will find that your ComboBox does not get created until after the dialog creation has completed. Use the OnInitDialog() function to handle intialisation of dialog controls.

          The best things in life are not things.

          S Offline
          S Offline
          softwaremonkey
          wrote on last edited by
          #4

          Thanks Richard (and also Maximilien), your suggestion works great. I wasnt aware that OnInitDialog() still got called with modeless dialogs. :-D :-D Tony

          1 Reply Last reply
          0
          • S softwaremonkey

            OK, I'm going mad here. I have a dialog class which I am creating from a dialog resource and displaying as modeless. I have data members assigned to the various controls and most of them work fine, except the CComboBox data member. I am trying to add some string data to the control in the CMyDialog::OnCreate(LPCREATESTRUCT lpCreateStruct) but it asserts because the control's hwnd is null. Do I need to do anything special with a CComboBox? My code is like this...

            int CFlushDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
            {
            if (CDialog::OnCreate(lpCreateStruct) == -1)
            return -1;

            SetWindowPos(NULL, m\_nXPos, m\_nYPos, 0, 0, SWP\_NOSIZE | SWP\_NOZORDER);
            
            m\_comboDataMember.AddString("Hello");   // BOOM!
                // Other data members CEdit, CButton work OK
            return 0;
            

            }

            I display the dialog like this...

             m\_flushDialog.Create(IDD\_FLUSH\_DLG, this);
             m\_flushDialog.ShowWindow(SW\_SHOW);
            

            Anyone have any ideas? :confused: Tony

            A Offline
            A Offline
            Albert Holguin
            wrote on last edited by
            #5

            As others have suggested, all GUI initialization should occur in OnInitDialog(). The fact that some controls work is probably more of a coincidence in how they're implemented but should not be an indication that this is the proper place for this.

            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