Dynamic Control Example for 2.0?

L

lucius

Could someone provide a quick example for .NET 2.0 WinForms of how to
dynamically add a control "btnDynamic" to a form when button
"btnStandard" is clicked. I don't want to hardcode "btnDynamic" to the
form and make it simply visible, I don't want it to exist at runtime
until "btnStandard" is clicked, but when "btnDynamic" is created and
instantiated, it participates in the form's full event cycle and has
its own click event and etc.

Thanks.
 
S

Stephany Young

Private Sub btnStandard_Click(ByVal sender As Object, ByVal e As
EventArgs)

Dim _button As New Button

_button.Name = "btnDynamic"

_button.Text = "Whatever You Want"

_button.Size = <whatever you want>

_button.Location = <whatever you want>

AddHandler _button.Click, Addressof btnDynamic_Click

Me.Controls.Add(_button)

End Sub
 
L

Linda Liu [MSFT]

Hi Lucius,

To add a control on a form at run time, we need to add it to the Controls
collection of the form.

Stephany has provided a good VB.NET sample code. I will give you a C#
sample. It requires that you add a Button called 'btnStandard' on the form.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.btnStandard.Click += new EventHandler(btnStandard_Click);
}

void btnStandard_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Name = "btnDynamic";
btn.Text = "Dynamic Button";
btn.Location = new Point(10, 20);
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}

void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi, I am a dynamic button!");
}
}

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Lucius,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top