Thread: Creating an object at run-time

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    98

    Creating an object at run-time

    Is it possible to create an object such as a button or text box at run-time? For example, is it possible to start the program with an empty window, and then add all the controls in code when the program is running?
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    BTW, I'm using Visual C# 2005 Express edition

  3. #3
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    like on a timer? or just from user interaction?

    like if you have only one button on your form, then when clicked it would create text boxes or other buttons? something like this?

    the easiest way is to right click on your InitializeComponent(); method in your main form and then go to "Go To Definition" and it will point you to the forms behind the scenes area. You can look at how things are created there like the definitions it sets like tab index, name, size, location, text and any listeners like Click or MouseMove in that area. Then you should use either this.Controls.Add(the button) like at the bottom of that function.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Quote Originally Posted by AtomRiot View Post
    like on a timer? or just from user interaction?

    like if you have only one button on your form, then when clicked it would create text boxes or other buttons? something like this?

    the easiest way is to right click on your InitializeComponent(); method in your main form and then go to "Go To Definition" and it will point you to the forms behind the scenes area. You can look at how things are created there like the definitions it sets like tab index, name, size, location, text and any listeners like Click or MouseMove in that area. Then you should use either this.Controls.Add(the button) like at the bottom of that function.
    I will try what you said, but what I want to do is start with an empty form with nothing at all on it. Then in the code I want to add all the objects such as buttons and text boxes etc, and move them into their positions. All that must take place when the program is running.

  5. #5
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    well thats what i was confused about, you want to start with an empty form so you run the app and then have stuff appear later?
    or you just want to manually add all the controls in the code yourself without the help of the ide and then run the app?
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    A mix of the two, actually. The form will start blank, and then at the beggining of the program some controls will be added, and then maybe later in the program some more. But not with a timer.

  7. #7
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    ok, i see. You just dont want the GUI portion to do everything for you. You want to handle everything past the creation of the form.

    in that case, i would make a test project and just drag some controls like buttons and whatnot onto your form, and then check out how it created them. That would give you a start. From there, you could go over to your app and put what you need in the form constructor and then handle the rest as you go.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Quote Originally Posted by AtomRiot View Post
    ok, i see. You just dont want the GUI portion to do everything for you. You want to handle everything past the creation of the form.

    in that case, i would make a test project and just drag some controls like buttons and whatnot onto your form, and then check out how it created them. That would give you a start. From there, you could go over to your app and put what you need in the form constructor and then handle the rest as you go.
    Thanks, now this is where this thread and my other one come together. How would I create, and control the controls on form2 while on form1? In the mean time I'll play around with what you said.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to time how long a program takes to run?
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 08-18-2008, 07:50 PM
  2. Displaying Function Run Time
    By pf732k3 in forum C Programming
    Replies: 1
    Last Post: 03-21-2008, 12:36 PM
  3. Run time differences in Linux gcc versions
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2008, 11:09 PM
  4. Please help - Run time error
    By barkercs in forum C Programming
    Replies: 6
    Last Post: 05-03-2006, 11:15 AM
  5. Create class at run time
    By muaed in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-11-2002, 08:13 AM