|
This course
- Adding Custom Controls Directly with Code - Tutorial 2
v 1.1 - last updated: 08/07/2006 - added step 4 |
Instead of using the Windows Forms Designer and Toolbox as in the previous tutorial, in this tutorial you manually add a custom control to a Windows application and then write code to instantiate the control on a form. There are eight steps...
public partial class RandomLabel : UserControlto this:
public partial class RandomLabel : System.Windows.Forms.Label
public RandomLabel()
{
InitializeComponent();
Random r = new Random();
this.Text = r.Next(100).ToString();
this.BackColor = Color.Yellow;
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
RandomLabel mylabel = new RandomLabel();
this.Controls.Add(mylabel);
mylabel.Location = new Point(15, i * 25);
mylabel.Size = new Size(50, 20);
System.Threading.Thread.Sleep(300);
}
}