Add control dynamically
-
MyControl c = new MyControl();
// set the properties for the control
this.Controls.Add(c);
Simple :) I assume that this code is being executed in the forms class. This is also how you add a control to a windows form normally. James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
-
I would guess Container.Controls.Add(new Control()); without having tested it yet *woops, didnt notice the earlier post agreeing.
-
MyControl c = new MyControl();
// set the properties for the control
this.Controls.Add(c);
Simple :) I assume that this code is being executed in the forms class. This is also how you add a control to a windows form normally. James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
Thank you! I can add a new control but i can't set the properties for the control. this is my code: TextBox tb = new TextBox(); this.Controls.Add(tb); this.tb.Location = new System.Drawing.Point(150, 100); this.tb.Multiline = true; this.tb.Name = "textBox2"; this.tb.ReadOnly = false; this.tb.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.tb.Size = new System.Drawing.Size(168, 32); this.tb.TabIndex = 1; this.tb.Text = "textBox1"; this.tb.Visible = false; It gives errors. How to set the properties on the control?
-
Thank you! I can add a new control but i can't set the properties for the control. this is my code: TextBox tb = new TextBox(); this.Controls.Add(tb); this.tb.Location = new System.Drawing.Point(150, 100); this.tb.Multiline = true; this.tb.Name = "textBox2"; this.tb.ReadOnly = false; this.tb.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.tb.Size = new System.Drawing.Size(168, 32); this.tb.TabIndex = 1; this.tb.Text = "textBox1"; this.tb.Visible = false; It gives errors. How to set the properties on the control?
What errors are you getting? From what I see that code is fine; though I don't think it matters can you try psetting the properties before you do the 'this.Controls.Add(tb)'? James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
-
What errors are you getting? From what I see that code is fine; though I don't think it matters can you try psetting the properties before you do the 'this.Controls.Add(tb)'? James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
-
Ok, i got it ! I am not suppose to put 'this.tb.Location(....)' but this: 'tb.Location(....)' ok... Thank you very much!! thongkk
lol, I can't believe I missed that! At least you have it figured out now :-) James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972