برای اینکار باید ابتدا با استفاده از Set و Get برای کنترلهایی که باید مقدارشون تغییر کنه یه متد تعریف کنید که بعد بتونیم مقدار رو تنظیم کنیم:
پس این کدها رو به userControl اضافه میکنم:
[CSHARP]
public string title
{
get { return linkLabel1.Text; }
set { linkLabel1.Text = value; }
}
public string text
{
get { return MsgBody.Text; }
set { MsgBody.Text = value; }
}
public string UName
{
get { return labelName.Text; }
set { labelName.Text = value; }
}
public string Time
{
get { return labelTime.Text; }
set { labelTime.Text = value; }
}
[/CSHARP]
خوب حالا برای ضافه کردن کنترل کافیه به این شکل عمل کنید:
[CSHARP]
int i = 0;
int Ntop = 0;
private void button1_Click(object sender, EventArgs e)
{
UserControl1 uc1 = new UserControl1();
uc1.title = "title here";
uc1.text = "Message body here";
uc1.UName = "name here";
uc1.Time = DateTime.Now.ToString();
// set position
uc1.Top =Ntop+ 5;
uc1.Left = 6;
//set controls name
uc1.Name = "UserControl" + (++i);
// add to panel 3 and show it
panel3.Controls.Add(uc1);
// add value of Ntop variable for next control
Ntop += uc1.Height+5;
}
[/CSHARP]
اینم پروژه کامل
http://www.s1.softafzar.net/uploads/201310101381405137UsCnt.rar
موفق باشید:happysmiley: