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
Printable View
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'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
Here is what I tried:
I had declared label5 in the class Form1 as such: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();
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').Code:private: System::Windows::Forms::Label * label5;
Does anyone know how to actually get the control ONTO the form (so it can be seen)?
Thanks,
Philipp
Thank you for the replies.
After playing around a bit I worked out how to do it.
Something like:
Works nicely.Code:using namespace System::Windows::Forms;
Label *MyLabel = new Label();
this::Controls::Add(Label);
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