C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-26-2008, 05:14 AM   #1
Registered User
 
starcatcher's Avatar
 
Join Date: Feb 2008
Location: Australia
Posts: 41
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:
Quote:
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
starcatcher is offline   Reply With Quote
Old 03-26-2008, 06:09 AM   #2
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
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.

Memorable Quotes From Recent Posts:

I can't remember.
Dino is offline   Reply With Quote
Old 03-26-2008, 04:40 PM   #3
Registered User
 
starcatcher's Avatar
 
Join Date: Feb 2008
Location: Australia
Posts: 41
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:
Quote:
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
starcatcher is offline   Reply With Quote
Old 03-27-2008, 07:52 AM   #4
3735928559
 
Join Date: Mar 2008
Posts: 662
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
m37h0d is offline   Reply With Quote
Old 03-27-2008, 05:29 PM   #5
Registered User
 
starcatcher's Avatar
 
Join Date: Feb 2008
Location: Australia
Posts: 41
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:
Quote:
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
starcatcher is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Profiler Valgrind afflictedd2 C++ Programming 4 07-18-2008 09:38 AM
Silverlight 2 - creating controls DavidP C# Programming 0 05-08-2008 02:07 PM
Creating Custom Controls Grayson_Peddie C# Programming 1 12-29-2003 01:41 PM
Creating Controls on Window samudrala_99 Windows Programming 4 01-30-2003 07:40 AM
BCBuilder Users! Help!! Runtime controls Robert C++ Programming 0 03-24-2002 04:04 PM


All times are GMT -6. The time now is 02:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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