Creating controls at runtime

Posted on 2006-05-18
Last Modified: 2010-04-24
Good afternoon experts:

I'm trying to locate some documentation on creating controls at runtime without much sucess.  I have a combobox that lists the different types of controls, and when the user selects a control in the combo box, I'd like to create it.  

Could you provide an example of how to create a control, like a radio button or textbox, during runtime?

I'm using C++.NET 2005

Any help is appreciated.
Question by:escheider
    LVL 49

    Expert Comment

    If you use MFC (and most vc++ programmers do) then check the documentation for objects such as
      ... etc...  

    Each of these object has a "Create" memeber function.  You can specify various "style" options in that call as well as the on-screen location to display the control.  For instance, in a dialog box, you could use code like:

       static CButton myBtn;
       myBtn.Create( "My button", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(10,10,100,30), this, 1);

    and a button would appear in the dialog.  There would be additional steps to take if you need to be able to take action when the button gets clicked (etc.) but the Create function is what you need to use to create it in the first place.

    -- Dan
    LVL 4

    Author Comment

    I'm fairly new to VC++.NET, so I'm using Windows Forms, not MFC, and I'm finding a lack of information on the subject.
    LVL 49

    Accepted Solution

    Look at:
    though, as with most .NET stuff, it refers to C# and VB. and it is a bit harder to use the .NET class objects with plain C++ (if you are new to that environment).

    There is C++ example of how to create a TextBox here:

    And each of the Control classes show example code in three programming languages.

    -- Dan
    LVL 4

    Author Comment

    Thank you Dan, this got me on the right path.  I appreciate it.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now