Creating controls in runtime

This is a discussion on Creating controls in runtime within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm using Visual C++ and am creating a Forms Application. How can you create a new control during runtime? ...

  1. #1
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Question Creating controls in runtime

    Hi,
    I'm using Visual C++ and am creating a Forms Application.
    How can you create a new control during runtime?
    For example, how could I create a new label in my Form during runtime when the user clicks a button or something?

    Thanks,
    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I'm sure you can. Put the "label create" call in its own function, and call that function whenever you need a new one. It should probably return the instance of the label created. Pass any variable information as parms to the function, like text for the label, position, etc.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Question

    Here is what I tried:
    Code:
    this->label5=new Label();
    this->label5->CreateControl();
    this->label5->BackColor = System::Drawing::Color::Transparent;
    this->label5->Location = System::Drawing::Point(400,400);
    this->label5->Name = S"label5";
    this->label5->Size = System::Drawing::Size(176,32);
    this->label5->Text = S"This is MY label!";
    this->label5->BringToFront();
    I had declared label5 in the class Form1 as such:
    Code:
    private: System::Windows::Forms::Label *  label5;
    However, I only tried that because if I declared label5 in the function itself it didn't work. (I would rather want the label to be declared in the function, I just didn't change it back). Anyway, this doesn't work either (no errors) because either the label never turns up on the Form (which I think is the case) or it hides behind my tabpages etc... (which I don't think is the case because I used 'BringToFront').

    Does anyone know how to actually get the control ONTO the form (so it can be seen)?

    Thanks,
    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by starcatcher View Post
    Here is what I tried:
    Code:
    this->label5=new Label();
    this->label5->CreateControl();
    this->label5->BackColor = System::Drawing::Color::Transparent;
    this->label5->Location = System::Drawing::Point(400,400);
    this->label5->Name = S"label5";
    this->label5->Size = System::Drawing::Size(176,32);
    this->label5->Text = S"This is MY label!";
    this->label5->BringToFront();
    I had declared label5 in the class Form1 as such:
    Code:
    private: System::Windows::Forms::Label *  label5;
    However, I only tried that because if I declared label5 in the function itself it didn't work. (I would rather want the label to be declared in the function, I just didn't change it back). Anyway, this doesn't work either (no errors) because either the label never turns up on the Form (which I think is the case) or it hides behind my tabpages etc... (which I don't think is the case because I used 'BringToFront').

    Does anyone know how to actually get the control ONTO the form (so it can be seen)?

    Thanks,
    Philipp
    i use borland personally, and the trick there is you have to set the property "Parent". i bet there is a similar property in MSVS

  5. #5
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Lightbulb The answer...

    Thank you for the replies.
    After playing around a bit I worked out how to do it.
    Something like:
    Code:
    using namespace System::Windows::Forms;
    Label *MyLabel = new Label();
    this::Controls::Add(Label);
    Works nicely.
    You can set all the other properties as well like I did when I first tried (only it would be simply 'MyLabel::...')
    If I run into any more troubles (which is very likely) I will post them under this thread to avoid creating a new one.

    Thanks again!

    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  2. Silverlight 2 - creating controls
    By DavidP in forum C# Programming
    Replies: 0
    Last Post: 05-08-2008, 03:07 PM
  3. Creating Custom Controls
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 12-29-2003, 01:41 PM
  4. Creating Controls on Window
    By samudrala_99 in forum Windows Programming
    Replies: 4
    Last Post: 01-30-2003, 07:40 AM
  5. BCBuilder Users! Help!! Runtime controls
    By Robert in forum C++ Programming
    Replies: 0
    Last Post: 03-24-2002, 04:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21