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. creating dialogs in a loop

creating dialogs in a loop

Scheduled Pinned Locked Moved C / C++ / MFC
wpfhelptutoriallearning
15 Posts 6 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.
  • K K Sushilkumar

    Hi everybody, I am trying to create dialogs in a loop. I am having pointer to each dialog and resource id. Problem is while creating an object of that dialog using new operator. As new requires the typename which i am unable to provide in a loop. Please refer the code below. DlgA *pDlgA; DlgB *pDlgB; typedef struct tagDLG_INFO { CDialog *pDlg; DWORD dwDlgID; } DLG_INFO, *P_DLG_INFO; DLG_INFO arrDlgInfo[2] = { {pDlgA, IDD_A}, {pDlgB, IDD_B} }; for (DWORD dwLoopCount = 0; dwLoopCount < 2; ++dwLoopCount) { arrDlgInfo[dwLoopCount].pDlg = new XXX; //(How to provide typename in a loop). arrDlgInfo[dwLoopCount].pDlg->Create(arrDlgInfo[dwLoopCount].dwDlgID, this); } I tried using macros, templates, RTTI, typeinfo, but i did'nt find any proper solution. Thanks in advance.

    S Offline
    S Offline
    sushrut83
    wrote on last edited by
    #2

    If not needed to create a modaless dialog then better to call DoMadal function of CDialog class to create Modal Dialog. Sushrut

    K 1 Reply Last reply
    0
    • K K Sushilkumar

      Hi everybody, I am trying to create dialogs in a loop. I am having pointer to each dialog and resource id. Problem is while creating an object of that dialog using new operator. As new requires the typename which i am unable to provide in a loop. Please refer the code below. DlgA *pDlgA; DlgB *pDlgB; typedef struct tagDLG_INFO { CDialog *pDlg; DWORD dwDlgID; } DLG_INFO, *P_DLG_INFO; DLG_INFO arrDlgInfo[2] = { {pDlgA, IDD_A}, {pDlgB, IDD_B} }; for (DWORD dwLoopCount = 0; dwLoopCount < 2; ++dwLoopCount) { arrDlgInfo[dwLoopCount].pDlg = new XXX; //(How to provide typename in a loop). arrDlgInfo[dwLoopCount].pDlg->Create(arrDlgInfo[dwLoopCount].dwDlgID, this); } I tried using macros, templates, RTTI, typeinfo, but i did'nt find any proper solution. Thanks in advance.

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

      skmk1247 wrote:

      arrDlgInfo[dwLoopCount].pDlg = new XXX; //(How to provide typename in a loop).

      Does this work for you?arrDlgInfo[dwLoopCount].pDlg = IDD_A == arrDlgInfo[dwLoopCount].dwDlgID ? new pDlgA(this):new pDlgB(this);
      Could also check for NULL

      K 1 Reply Last reply
      0
      • S sushrut83

        If not needed to create a modaless dialog then better to call DoMadal function of CDialog class to create Modal Dialog. Sushrut

        K Offline
        K Offline
        K Sushilkumar
        wrote on last edited by
        #4

        I am sorry that u didn't get the question. what i want to do is similar to the following, float *pf; float *pf = new float; (This is the normal case where float is the typename passed to new operator). (Now consider our problem) int *pi; char *pc; If i have such many different types of pointers. And i want to create memory to that respective pointer with it's respective type in loop. So how am i able to provide typename to the new operator generically in a loop.

        1 Reply Last reply
        0
        • L Lost User

          skmk1247 wrote:

          arrDlgInfo[dwLoopCount].pDlg = new XXX; //(How to provide typename in a loop).

          Does this work for you?arrDlgInfo[dwLoopCount].pDlg = IDD_A == arrDlgInfo[dwLoopCount].dwDlgID ? new pDlgA(this):new pDlgB(this);
          Could also check for NULL

          K Offline
          K Offline
          K Sushilkumar
          wrote on last edited by
          #5

          Your solution is good. But i had given a sample of code. What actually i want to do is to create around 20 dialogs of different types. The resource i can have for this is the array of DLG_INFO. For acheiving this we can extend or add more members to DLG_INFO. Thanks.

          K L 2 Replies Last reply
          0
          • K K Sushilkumar

            Your solution is good. But i had given a sample of code. What actually i want to do is to create around 20 dialogs of different types. The resource i can have for this is the array of DLG_INFO. For acheiving this we can extend or add more members to DLG_INFO. Thanks.

            K Offline
            K Offline
            KarstenK
            wrote on last edited by
            #6

            "around 20 dialogs of different types" which user should use such messy GUI. X| X| X| This is confusing and unusable, you better rethink your software architecture :-O PS: I have written a project which a bunch of modeless dialogs, which will be shown if a vertical tab got selected. It got to messy and crappy, but my boss wanted it.

            Greetings from Germany

            K 1 Reply Last reply
            0
            • K K Sushilkumar

              Your solution is good. But i had given a sample of code. What actually i want to do is to create around 20 dialogs of different types. The resource i can have for this is the array of DLG_INFO. For acheiving this we can extend or add more members to DLG_INFO. Thanks.

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

              Perhaps I fail to see what problem you are having. The solution I gave you will work for 2 dialogs or 20 dialogs if you modify it to a switch statement. In addition it should work for any CDialog derived class. What exactly is the problem you are having? Here are some points: 1.) If your dialogs have a unique dialog resource ID associated with each CDialog derived class then you can easily identify which dialog belongs to which class. 2.) You can assign the CDialog pointer in your DLG_INFO structure to your derived class using the dynamic_cast operator[^] or the STATIC_DOWNCAST[^] macro. 3.) You can regain the type information by upcasting the CDialog pointer to access members. Maybe I do not fully understand the problems you are having, if this is the case please clarify. Best Wishes, -David Delaune

              K 1 Reply Last reply
              0
              • L Lost User

                Perhaps I fail to see what problem you are having. The solution I gave you will work for 2 dialogs or 20 dialogs if you modify it to a switch statement. In addition it should work for any CDialog derived class. What exactly is the problem you are having? Here are some points: 1.) If your dialogs have a unique dialog resource ID associated with each CDialog derived class then you can easily identify which dialog belongs to which class. 2.) You can assign the CDialog pointer in your DLG_INFO structure to your derived class using the dynamic_cast operator[^] or the STATIC_DOWNCAST[^] macro. 3.) You can regain the type information by upcasting the CDialog pointer to access members. Maybe I do not fully understand the problems you are having, if this is the case please clarify. Best Wishes, -David Delaune

                K Offline
                K Offline
                K Sushilkumar
                wrote on last edited by
                #8

                Your solution was actually very good and convincing. But i was trying the same without using switch/if-else or any user defined function. I feel that there may be any other means to achieve the problem. If it is not feasible then i am really sorry for the question i posted. Thanks.

                L 1 Reply Last reply
                0
                • K KarstenK

                  "around 20 dialogs of different types" which user should use such messy GUI. X| X| X| This is confusing and unusable, you better rethink your software architecture :-O PS: I have written a project which a bunch of modeless dialogs, which will be shown if a vertical tab got selected. It got to messy and crappy, but my boss wanted it.

                  Greetings from Germany

                  K Offline
                  K Offline
                  K Sushilkumar
                  wrote on last edited by
                  #9

                  Yes that is confusing but it is usable. I'll give you the scenario, I need to write options/settings module for a product. The product is dialog based. Now the options will be in a tree control on the left side. When user clicks a particular option i need to display appropriate dialog. There a 20 such options. To hide or show the respective dialog first they need to be created. The question i posted doesn't relate to number of dialogs to be created. Instead, in a loop how can we provide the typename for new operator. Just refer the root post. Thanks.

                  1 Reply Last reply
                  0
                  • K K Sushilkumar

                    Your solution was actually very good and convincing. But i was trying the same without using switch/if-else or any user defined function. I feel that there may be any other means to achieve the problem. If it is not feasible then i am really sorry for the question i posted. Thanks.

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

                    skmk1247 wrote:

                    But i was trying the same without using switch/if-else or any user defined function. I feel that there may be any other means to achieve the problem.

                    Yes, there are other ways to accomplish the same thing. You could probably also use a complicated template scheme to accomplish the same thing but why go through all that trouble? Are you attempting this for educational purpose? Templated code easily produces code bloat if used incorrectly. If you add a template function in the middle of that for-loop it will likely generate much larger code than directly accessing the struct member and generating conditional jump instructions which jmp directly into the class constructor. There is nothing to gain here by using templated code to create dialogs. Best Wishes, -David Delaune

                    K 1 Reply Last reply
                    0
                    • L Lost User

                      skmk1247 wrote:

                      But i was trying the same without using switch/if-else or any user defined function. I feel that there may be any other means to achieve the problem.

                      Yes, there are other ways to accomplish the same thing. You could probably also use a complicated template scheme to accomplish the same thing but why go through all that trouble? Are you attempting this for educational purpose? Templated code easily produces code bloat if used incorrectly. If you add a template function in the middle of that for-loop it will likely generate much larger code than directly accessing the struct member and generating conditional jump instructions which jmp directly into the class constructor. There is nothing to gain here by using templated code to create dialogs. Best Wishes, -David Delaune

                      K Offline
                      K Offline
                      K Sushilkumar
                      wrote on last edited by
                      #11

                      Yes you are right. To do that task now i am sequentially writing those many statements for each dialogs. I thought this task as challenging. I continued with some research and tryouts. You have suggested about templates, can you please help me out with a piece of code. This is just educational. I haven't stuck for this but I am trying to complete the task in any ways. Though it is silly problem I have spend enough time for it. I hope you understood the problem. Thanks & Regards, K. Sushilkumar.

                      1 Reply Last reply
                      0
                      • K K Sushilkumar

                        Hi everybody, I am trying to create dialogs in a loop. I am having pointer to each dialog and resource id. Problem is while creating an object of that dialog using new operator. As new requires the typename which i am unable to provide in a loop. Please refer the code below. DlgA *pDlgA; DlgB *pDlgB; typedef struct tagDLG_INFO { CDialog *pDlg; DWORD dwDlgID; } DLG_INFO, *P_DLG_INFO; DLG_INFO arrDlgInfo[2] = { {pDlgA, IDD_A}, {pDlgB, IDD_B} }; for (DWORD dwLoopCount = 0; dwLoopCount < 2; ++dwLoopCount) { arrDlgInfo[dwLoopCount].pDlg = new XXX; //(How to provide typename in a loop). arrDlgInfo[dwLoopCount].pDlg->Create(arrDlgInfo[dwLoopCount].dwDlgID, this); } I tried using macros, templates, RTTI, typeinfo, but i did'nt find any proper solution. Thanks in advance.

                        I Offline
                        I Offline
                        Iain Clarke Warrior Programmer
                        wrote on last edited by
                        #12

                        I've had a similar problem in the past. I used the following article to solve it: http://secure.codeproject.com/KB/cpp/enumleafclasses.aspx[^] I created a common ancester for the dialogs

                        class CMyDialog : public CDialog
                        {
                        ...
                        DECLARE_ROOT_CLASS (CMyDialog);
                        ...
                        };

                        and then a bunch of inheriting dialogs:

                        class CMyDialog1 : public CMyDialog
                        {
                        ...
                        DECLARE_LEAF_CLASS (CMyDialog);
                        ...
                        }

                        Then I could add new child classes to my hearts content - and loop through and create them.

                        int n, nCount = CMyDialog::GetRegisteredManufacturingPlantCount ();
                        CBootStrapper *pBS;
                        CMyDialog *pSub;

                        for (n = 0; n < nCount; n++)
                        {
                        pBS = CMyDialog::GetRegisteredManufacturingPlant (n);
                        pSub = pBS->CreateObject ();
                        ....
                        }

                        You'll need to read the article for fine detail, but I've used the concept a few times, and it worked nicely for me. I hope that helps, Iain.

                        K 1 Reply Last reply
                        0
                        • I Iain Clarke Warrior Programmer

                          I've had a similar problem in the past. I used the following article to solve it: http://secure.codeproject.com/KB/cpp/enumleafclasses.aspx[^] I created a common ancester for the dialogs

                          class CMyDialog : public CDialog
                          {
                          ...
                          DECLARE_ROOT_CLASS (CMyDialog);
                          ...
                          };

                          and then a bunch of inheriting dialogs:

                          class CMyDialog1 : public CMyDialog
                          {
                          ...
                          DECLARE_LEAF_CLASS (CMyDialog);
                          ...
                          }

                          Then I could add new child classes to my hearts content - and loop through and create them.

                          int n, nCount = CMyDialog::GetRegisteredManufacturingPlantCount ();
                          CBootStrapper *pBS;
                          CMyDialog *pSub;

                          for (n = 0; n < nCount; n++)
                          {
                          pBS = CMyDialog::GetRegisteredManufacturingPlant (n);
                          pSub = pBS->CreateObject ();
                          ....
                          }

                          You'll need to read the article for fine detail, but I've used the concept a few times, and it worked nicely for me. I hope that helps, Iain.

                          K Offline
                          K Offline
                          K Sushilkumar
                          wrote on last edited by
                          #13

                          Thank you. It is a good solution. Yes it could help me to some extent in my code. Because the dialogs are already being implemented. And i cannot change them from my side. I can only create the dialogs. As i discussed in earlier post that i was searching for some different technique instead of using if-else/switch for new operator. But as part of education/knowledge the technique used is mind blowing. Thanks & Regards, K. Sushilkumar.

                          I 1 Reply Last reply
                          0
                          • K K Sushilkumar

                            Thank you. It is a good solution. Yes it could help me to some extent in my code. Because the dialogs are already being implemented. And i cannot change them from my side. I can only create the dialogs. As i discussed in earlier post that i was searching for some different technique instead of using if-else/switch for new operator. But as part of education/knowledge the technique used is mind blowing. Thanks & Regards, K. Sushilkumar.

                            I Offline
                            I Offline
                            Iain Clarke Warrior Programmer
                            wrote on last edited by
                            #14

                            If the dialogs are already implemented somewhere else, and out of your control, then the article I pointed you at is of little use. I don;t think you have any alternative to brute force sadly. You can make it more or less elegant, but at some point you are going to have to match option with dialog... Maybe populate a map between option DWORD, and CDialog *? then you can iterate a biiiiit. Iain.

                            1 Reply Last reply
                            0
                            • K K Sushilkumar

                              Hi everybody, I am trying to create dialogs in a loop. I am having pointer to each dialog and resource id. Problem is while creating an object of that dialog using new operator. As new requires the typename which i am unable to provide in a loop. Please refer the code below. DlgA *pDlgA; DlgB *pDlgB; typedef struct tagDLG_INFO { CDialog *pDlg; DWORD dwDlgID; } DLG_INFO, *P_DLG_INFO; DLG_INFO arrDlgInfo[2] = { {pDlgA, IDD_A}, {pDlgB, IDD_B} }; for (DWORD dwLoopCount = 0; dwLoopCount < 2; ++dwLoopCount) { arrDlgInfo[dwLoopCount].pDlg = new XXX; //(How to provide typename in a loop). arrDlgInfo[dwLoopCount].pDlg->Create(arrDlgInfo[dwLoopCount].dwDlgID, this); } I tried using macros, templates, RTTI, typeinfo, but i did'nt find any proper solution. Thanks in advance.

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #15

                              You need to implement a class factory.

                              "Love people and use things, not love things and use people." - Unknown

                              "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                              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