class Class1: Control
{
//private Button btn;
//private Type ItemClass; public void CreateItems<T>(int count, ref T ItemClass)
{ //Button<t> = new Button<t>();
//Type tp = Type.GetType(classname);
Button item = new Button();
item.Left = x;
item.Top = y;
item.Width = 65;
item.Height = 65;
item.Click += new EventHandler(Item_Click);
this.Controls.Add(item);
}
}
private void button1_Click(object sender, EventArgs e)
{
Class1 aa = new Class1();
aa.Left = 0;
aa.Top = 0;
aa.Width = 200;
aa.Height = 200;
this.Controls.Add(aa);
//aa.CreateItems(10, ButtonClass);
aa.CreateItems<Button>(10, typeof(Button)); }
有个类class1, 它有个方法createitems, 我希望传进去是button类的,就创建出button类,如果传进去是Panel类的,就创建出Panel类. 传进去的类不只这两种,可以有很多. class1不知道.
可以教教我怎么写吗? 最好是可以运行的代码.
{
//private Button btn;
//private Type ItemClass; public void CreateItems<T>(int count, ref T ItemClass)
{ //Button<t> = new Button<t>();
//Type tp = Type.GetType(classname);
Button item = new Button();
item.Left = x;
item.Top = y;
item.Width = 65;
item.Height = 65;
item.Click += new EventHandler(Item_Click);
this.Controls.Add(item);
}
}
private void button1_Click(object sender, EventArgs e)
{
Class1 aa = new Class1();
aa.Left = 0;
aa.Top = 0;
aa.Width = 200;
aa.Height = 200;
this.Controls.Add(aa);
//aa.CreateItems(10, ButtonClass);
aa.CreateItems<Button>(10, typeof(Button)); }
有个类class1, 它有个方法createitems, 我希望传进去是button类的,就创建出button类,如果传进去是Panel类的,就创建出Panel类. 传进去的类不只这两种,可以有很多. class1不知道.
可以教教我怎么写吗? 最好是可以运行的代码.
{
List<T> list = new List<T>();
for (int i = 0; i < count; i++)
list.Add(new T());
return list;
}
这样吗?
public T CreateItems<T>() where T : new()
{
return new T();
}
createitems(类的类: itemclass)
{
itemclass item = new itemclass()
}这样能实现吗?
如果是下面这样,感觉没什么意义了
public T CreateItems<T>() where T : class,new()
{
return new T();
}
{
public void CreateItems<T>(int count, ref T)
{
int x, y;
x = 0;
y = 0;
for (int i = 0; i < count; i++)
{
T item = new T();
(Control)item.Left = x;
item.Top = y;
item.Width = 65;
item.Height = 65;
item.Text = i.ToString();
item.Click += new EventHandler(Item_Click);
this.Controls.Add(item);
}
} private void button1_Click(object sender, EventArgs e)
{
Class1 aa = new Class1();
aa.Left = 0;
aa.Top = 0;
aa.Width = 200;
aa.Height = 200;
this.Controls.Add(aa);
aa.CreateItems<Button>(10, Button);
}还是不行啊, 出错部分要怎么改?
public void CreateItems<T>(int count) where T : Control,new()
{
int x, y;
x = 0;
y = 0;
for (int i = 0; i < count; i++)
{
T item = new T();
item.Left = x;
item.Top = y;
item.Width = 65;
item.Height = 65;
item.Text = i.ToString();
item.Click += new EventHandler(Item_Click);
this.Controls.Add(item);
}
}
{
private Dictionary<string, Control> Controls = new Dictionary<string, Control>();
public void CreateItems<T>(int count) where T : Control, new()
{
string str = Regex.Match(typeof(T).ToString(), @"(?<=\.)\w+$").Value;
int n = 0;
for (int i = 0; n < count; i++)
{
if (!Controls.ContainsKey(str + i) )
{
T item = new T();
item.Text = str + i;
Controls.Add(str + i, item);
n++;
} }
}
public Control this[string key]
{
get { return Controls[key]; }
}
}
hjywyj和superliu1122好像是写对了,
public void CreateItems<T>(int count) where T : Control, new()
不过这个过程调用要怎么写?写全一点吧,我是新手
搞定,结贴