Dynamic Control Creation At Runtime? 
Author Message
 Dynamic Control Creation At Runtime?

Hi there.

I have some controls I would like to create at runtime.  I have tried
the following code without success:

Button but = new Button();
this.Controls.Add (but);

The load event on the controls fires, but they never physicially show
on the form.  What am I missing?

Thanks in advance.

-Gabe



Tue, 27 Apr 2004 10:32:53 GMT  
 Dynamic Control Creation At Runtime?
Gabriel,

Quote:
> I have some controls I would like to create at runtime.  I have tried
> the following code without success:

> Button but = new Button();
> this.Controls.Add (but);

> The load event on the controls fires, but they never physicially show
> on the form.  What am I missing?

but.Visible = true; ?

--
Tomas Restrepo



Tue, 27 Apr 2004 11:10:20 GMT  
 Dynamic Control Creation At Runtime?
Yes. i have but.Visible = true as well.  It doesn't apper to work the
same as in VB6.



Quote:
>Gabriel,
>> I have some controls I would like to create at runtime.  I have tried
>> the following code without success:

>> Button but = new Button();
>> this.Controls.Add (but);

>> The load event on the controls fires, but they never physicially show
>> on the form.  What am I missing?

>but.Visible = true; ?

>--
>Tomas Restrepo




Tue, 27 Apr 2004 11:13:12 GMT  
 Dynamic Control Creation At Runtime?
This works for me on Beta 2:

Button btn = new Button();
btn.Name = "btnNew";  
btn.Text = "A New Button";
btn.Location = new Point(20, 20);
btn.Size = new Size(100, 30);

btn.Click += new EventHandler(OnNewButtonClick);
this.Controls.Add(btn);


141.com says...

Quote:

> Hi there.

> I have some controls I would like to create at runtime.  I have tried
> the following code without success:

> Button but = new Button();
> this.Controls.Add (but);

> The load event on the controls fires, but they never physicially show
> on the form.  What am I missing?

> Thanks in advance.

> -Gabe

Tim


Tue, 27 Apr 2004 13:16:34 GMT  
 Dynamic Control Creation At Runtime?
I did that too.
All your controls must be added to the Controls Array in the form.



Quote:
>This works for me on Beta 2:

>Button btn = new Button();
>btn.Name = "btnNew";      
>btn.Text = "A New Button";
>btn.Location = new Point(20, 20);
>btn.Size = new Size(100, 30);

>btn.Click += new EventHandler(OnNewButtonClick);
>this.Controls.Add(btn);


>141.com says...

>> Hi there.

>> I have some controls I would like to create at runtime.  I have tried
>> the following code without success:

>> Button but = new Button();
>> this.Controls.Add (but);

>> The load event on the controls fires, but they never physicially show
>> on the form.  What am I missing?

>> Thanks in advance.

>> -Gabe

>Tim



Tue, 27 Apr 2004 19:00:11 GMT  
 Dynamic Control Creation At Runtime?
Well... this code works for Panels:

Button btn = new Button();                      
btn.Name = "btnNew";  
btn.Text = "A New Button";
btn.Location = new Point(20, 20);
btn.Size = new Size(100, 30);
btn.Visible = true;
btn.Click += new EventHandler(OnNewButtonClick);
panel2.Controls.Add(btn);

But it doe not work if i add directly to the Control Array of the form
it'se (form.Controls.Add(btn).  The button will not show.  Is there
something special you have to do if you're creating the control on the
form itself?

I mean I suppose i could layer a panel over the form.... but there has
to be a better way....



Wed, 28 Apr 2004 12:34:23 GMT  
 Dynamic Control Creation At Runtime?
Gabriel,

Quote:
> Well... this code works for Panels:

> Button btn = new Button();
> btn.Name = "btnNew";
> btn.Text = "A New Button";
> btn.Location = new Point(20, 20);
> btn.Size = new Size(100, 30);
> btn.Visible = true;
> btn.Click += new EventHandler(OnNewButtonClick);
> panel2.Controls.Add(btn);

> But it doe not work if i add directly to the Control Array of the form
> it'se (form.Controls.Add(btn).  The button will not show.  Is there
> something special you have to do if you're creating the control on the
> form itself?

Shouldn't be. In fact, I've done it more than once. Could you post (or email
me) a small, compilable project that demonstrates the problem? I'd be happy
to take a look and see if I can help you out...

--
Tomas Restrepo



Wed, 28 Apr 2004 13:20:17 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Runtime creation of controls

2. ActiveX WebBrowser control creation during runtime

3. Runtime control creation and event handling?

4. Dynamic Control Creation

5. Dynamic Control Creation in C#

6. dynamic control creation

7. Dynamic creation of controls without ressource ID

8. Dynamic Creation of Controls

9. Dynamic creation of controls...

10. Dynamic creation of controls in a FormView

11. Dynamic control creation on a DialogBar

12. ATL Bug? Dynamic control Creation problem

 

 
Powered by phpBB® Forum Software