1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
private void button1_Click(object sender, EventArgs e)
{
Draw<Label, string>("test", "hello", 100, 10);
Draw<ComboBox, object[]>("test", new object[] { "hello", "world" }, 100, 70);
}
public void Draw<T,U>(string name, U text, int posx, int posy)
where T : Control, new()
{
T control = new T();
control.Name = string.Format("{0}_{1}", control.GetType().ToString(), name);
// bad code, no handling for exceptions aso...
if( control is Label )
control.Text = text as string;
else if( control is ComboBox )
(control as ComboBox).Items.AddRange(text as object[]);
control.Location = new Point(posx, posy);
control.Size = new Size(20, 20);
if( ActiveForm != null )
ActiveForm.Controls.Add(control);
} |
Partager