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.