Link to home
Start Free TrialLog in
Avatar of x4u
x4u

asked on

Creating a simple Custom Control

Hello, once again I wasted a lot of time today trying to get something supposedly trivial done that was actually supposed to save me some time.

- Put some controls (a textfield, 2 buttons and maybe a static label or picture control) into a small property sheet and layout them within the resource editor.
- Create a subclass of CDialog with variables for these controls and initialize them with the mentioned property sheet.
- Put a dozen of these subclass instances into another dialog and layout them as Custom Controls.

It worked more or less well except the "... and initialize them with the mentioned property sheet" part.

I'm pretty new to MFC and Win32 and assumed that all I need to do is to get the DoDataExchange method invoked from somewhere and this would create and initialize the subcontrols. But I keep getting Assertion failed and various errors when I try to do this in a similar way as seems to be done by the CTabCtrl. The class itself (without it's contents) run's well and instances of it can be placed in a dialog.

I'm sure someone of you can help me to get this done.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

First do you want a property sheet (tab control + different page) or just a dialog.

For a dialog.  In resource editor choose addnew / dialog.  Add controls you require.  Select class wizard, it prompts for you to create a new class, choose CDialog as base.  From class wizard go to the second tab - variables - assign variables to the controls you want mapping.

For a property sheet (with VC 6 there is a wizard - component gallery to help) you have more work to do.  The PAGES to be displayed are created as above BUT you require CPropertyPage as the base class.  You then need a class based on CPropertySheet and it is to this you add the pages.
eg.

CMyPropertySheet::CMyPropertySheet(CWnd* pWndParent)
       : CPropertySheet(IDS_PROPSHT_CAPTION, pWndParent)
{
      // Add all of the property pages here.  Note that
      // the order that they appear in here will be
      // the order they appear in on screen.  By default,
      // the first page of the set is the active one.
      // One way to make a different property page the
      // active one is to call SetActivePage().

      AddPage(&m_Page1);
      AddPage(&m_Page2);
}
Avatar of AlexFM
AlexFM

Avatar of x4u

ASKER

Thank you for your feedback.

AndyAinscow, I tried it this way and it worked well. But how do I place this dialog into another one now? When I add it as a custom control another dialog I run into the following Assert:

Assertion failed in winocc.cpp line 330:

BOOL CWnd::ShowWindow(int nCmdShow)
{
----->      ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));

      if (m_pCtrlSite == NULL)
            return ::ShowWindow(m_hWnd, nCmdShow);
      else
            return m_pCtrlSite->ShowWindow(nCmdShow);
}

The debugger says:
m_hWnd = 0x00000000
m_pCtrlSite = 0x00000000


AlexFM, this is the example I started with and it worked well. But now I have the problem how to place other controls into this custom control.


Well, now I have a custom dialog where I can put some controls into and I have a custom control that can itself be put into a dialog. What I want is a custom dialog control that allows me to do both.
It can be difficult to use a dialogbox as a control within another dialogbox.   Before getting into this relatively complex arena, answer this:  

Are you sure that is necessary?  Can't you just put whatever you are putting in the control-dialog directly into the parent dialog?  Or perhaps make a series of property pages (depending upon your ultimate goal).
Avatar of x4u

ASKER

> Are you sure that is necessary?

Well, I have two cases where this would be helpfull.

First one is a configuration dialog that displays about a dozen very similar options where each needs a text field, two buttons and a readonly text field or an image that displays some state. It is not yet clear whether the customer ultimatly want's a combo box instead of the text field or a slider instead of the buttons. There is also some logic and validating neccessary for each option and the sub controls need to interact with each other. I think think this cries for some kind of encapsulation although I do not necessarily need to be able to layout the sub controls in the visual resource editor.

The second one is is a rather complex dialog which I need only once. But it is not yet decided whether this will become a page in a tab control or should be part of the main window without a tab control around it (maybe even configurable by the user).
If you mean you want for example 5 sub dialogs all looking the same but displaying different info then I would jst have one dialog and reset the contents depending on what needs to be shown.
To display different sub dialogs dynamically swapped inside another is a description of the PropertySheet - PropertyPage logic.
Avatar of x4u

ASKER

AndyAinscow, I'm not sure whether I understand you right. I need these similar subdialogs to be visible side by side at the same time inside a larger dialog not just one at a time. Is this possible with PropertySheet - PropertyPage logic? How?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial