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
Printable View
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
BTW, I'm using Visual C# 2005 Express edition
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.
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?
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.
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.