Problem adding controls programmaticaly to programmaticaly created form

 

Hi,

I'm trying to create a form at runtime,
and I need to add controls to that form.

Now the problem is that i get many errors like this one:
error C2664: 'System::Windows::Forms::Control::ControlCollection::Add' : cannot convert parameter 1 from 'System::Windows::Forms::GroupBox' to 'System::Windows::Forms::Control ^' 
when I try to add the controls.

Here is the code I'm using (in a void):

//Declare form + controls///////////////////////
//Form //
System::Windows::Forms::Form frm_settings;
//HL settings - groupbox + controls
System::Windows::Forms::GroupBox HL_settings;
System::Windows::Forms::Label lbl_path;
System::Windows::Forms::TextBox txt_path;
System::Windows::Forms::Button btn_browse;
System::Windows::Forms::Label lbl_params;
System::Windows::Forms::TextBox txt_params;
//MAIN settings - groupbox + controls
System::Windows::Forms::GroupBox MAIN_settings;
System::Windows::Forms::CheckBox ld_game;
System::Windows::Forms::CheckBox ld_steam;
System::Windows::Forms::Label i_delay;
System::Windows::Forms::NumericUpDown delay;
//////////////////////////////////////////////////////////

//Form/////////////////////////////////////////////////
frm_settings.Text="r0fl'z0r | Settings";
f
rm_settings.Location=System::Drawing::Point(300,250);
frm_settings.Size=System::Drawing::Size(350,217);
//////////////////////////////////////////////////////////////

//HL settings - groupbox////////////////////////////
HL_settings.Text="HL Settings";
HL_settings.Location=System::Drawing::Point(3,2);
HL_settings.Size=System::Drawing::Size(336,67);
//////////////////////////////////////////////////////////////

//Path//////////////////////////////////////////////////////
l
bl_path.Text="Path: ";
lbl_path.Location=System::Drawing::Point(7,16);
lbl_path.Size=System::Drawing::Size(32,13);
//////
txt_path.Location=System::Drawing::Point(50,13);
txt_path.Size=System::Drawing::Size(223,20);
//////
btn_browse.Text="Browse";
btn_browse.Location=System::Drawing::Point(279,11);
btn_browse.Size=System::Drawing::Size(51,23);
//////////////////////////////////////////////////////////////

//Params/////////////////////////////////////////////////
lbl_params.Text="Params: ";
lbl_params.Location=System::Drawing::Point(7,43);
lbl_params.Size=System::Drawing::Size(45,13);
//////
txt_params.Location=System::Drawing::Point(50,40);
txt_params.Size=System::Drawing::Size(280,20);
//////////////////////////////////////////////////////////////

//MAIN settings - groupbox////////////////////////
MAIN_settings.Text="Main Settings";
MAIN_settings.Location=System::Drawing::Point(3,75);
MAIN_settings.Size=System::Drawing::Size(336,104);
//////////////////////////////////////////////////////////////

//Start game - checkbox////////////////////////////
ld_game.Text="Start game automaticaly on load";
ld_game.Location=System::Drawing::Point(10,19);
ld_game.Size=System::Drawing::Size(190,20);
//////////////////////////////////////////////////////////////

//Start steam - checkbox//////////////////////////
ld_game.Text="Start steam automaticaly on load";
ld_game.Location=System::Drawing::Point(10,45);
ld_game.Size=System::Drawing::Size(190,20);
//////////////////////////////////////////////////////////////

//Injection delay - label//////////////////////////////
i_delay.Text="Injection Delay: ";
i_delay.Location=System::Drawing::Point(7,77);
i_delay.Size=System::Drawing::Size(78,13);
//////////////////////////////////////////////////////////////

//Delay - NumericUpDown/////////////////////////
delay.Minimum=0;
d
elay.Maximum=100;
delay.Location=System::Drawing::Point(91,75);
delay.Size=System::Drawing::Size(55,20);
//////////////////////////////////////////////////////////////

//Add the controls to the forms///////////////////
frm_settings.Controls->Add(HL_settings);
frm_settings.Controls->Add(lbl_path);
frm_settings.Controls->Add(txt_path);
frm_settings.Controls->Add(btn_browse);
frm_settings.Controls->Add(lbl_params);
frm_settings.Controls->Add(txt_params);
frm_settings.Controls->Add(MAIN_settings);
frm_settings.Controls->Add(ld_game);
frm_settings.Controls->Add(ld_steam);
frm_settings.Controls->Add(i_delay);
frm_settings.Controls->Add(delay);
//////////////////////////////////////////////////////////

//Show the form////////////////////////////////////
frm_settings.ShowDialog(); //
//////////////////////////////////////////////////////////

Now is there something I'm missing
Or do I have to do it in antoher way

Grtz, Tom.




Answer this question

Problem adding controls programmaticaly to programmaticaly created form

  • James Lean

    Hi Tom,

    If you want to use a function or type that is declared in a header file, just #include "header file" in the beginning of your cpp file. and then use it. For example:

    // example.h

    int exampleType;

    void exampleFunction(){}

    public class exampleClass{};

    // example.cpp

    #include "example.h"

    int main(){

    exampleType = 1;

    exampleFunction();

    exampleClass class;

    return 0;

    }

    I am not sure why you write: hx_loader ldr; is hx_loader a type defined in hx_loader.h Or you considered hx_loader a namespace and try to access the header file through this namespace

    regards,

    rico



  • Brennon

    Redirected from visual c++ express edition forum to windows forms general forum.

    note: if you still believe that your question should be reside in original forum, feel free to point it out, we will have further discuss after that.



  • Kannan.Perumal

    Thank you Zhi-Xen Ye.

    I'm very new to C++ (I use to code in VB),
    and I was wondering: what is the '^' for
    And what is 'gcnew' for is it the same as 'new' in VB

    Grtz, Tom.



  • Jan Kučera

    Thank you Bite Qiu,

    I don't know what 'managed heap', 'managed type' etc mean,
    but I do understand what your saying

    Now I don't know if I have to start another thread for this but:
    I've added a header file (hx_loader.h) to my project,
    and I have a void (void HookIntoDll() ) in it.

    Now my question is, how do I use the void in that file in my <project name>.cpp file
    I already did #include "hx_loader.h" , but how do I use it
    I tried:
    hx_loader ldr;
    ldr::
    ldr->
    ldr.
    but none of them seem to work.

    Any help would be appreciated.

    Grtz, Tom.



  • manasia

    Hello Rico,

    thanks again

    The reason why I type hx_loader ldr; is
    that in VB, when you import a class (don't know if I have to see a header file as a class)
    you need to declare the class like:

    imports mydll
    ...
    dim dll as new mydll
    dim foo as integer = mydll.<function>(args)

    so i thought in C++:

    #include "mydll.h"
    ...
    mydll dll;
    int foo = mydll-><function>

    But that's wrong I guess ^^

    Anyway, thank you for helping me out

    Grtz, Tom.



  • mfeo

    Use System::Windows::Forms::GroupBox^ instead of System::Windows::Forms::GroupBox, as:

    System::Windows::Forms::GroupBox^ HL_settings;

    HL_settings = (gcnew System::Windows::Forms::GroupBox());

    HL_settings->Text="HL Settings";

    HL_settings->Location=System::Drawing::Point(3,2);

    HL_settings->Size=System::Drawing::Size(336,67);

    this->Controls->Add(HL_settings);

    put attention to the "^"



  • mig16

    hi Tom,

    '^' is a handle to an object in managed heap, It is a new feature of Visual C++ 2005(C++/CLI), In Managed C++, both handle and pointer use '*' which generate confusion because handle and pointer is totally different concept.

    'gcnew' create a instance of managed type in managed heap while 'new' create a instance of object in native heap, and gcnew expression returns a handle to the type.

    If you feel above sentences unreadable, then I'd recommend you to read Overview of the .NET Framework in MSDN. Its a good place to start with programming under .NET platform.

    regards,

    rico



  • Problem adding controls programmaticaly to programmaticaly created form