C# 将数据从UserControl传递到表单

C# 将数据从UserControl传递到表单,c#,winforms,C#,Winforms,我在主窗体中有一个FlowLayoutPanel,并且FlowLayoutPanel中填充了用户控件(类型为UCProducts) My UserControl有一个PictureBox和一个对象(属于ProductClass) 用户控制:(简化): 表格: Sender能否直接发送用户控件?如何在表单中提取产品对象?一种方法是通过PictureBox控件的属性访问UserControl。然后,您可以像平常一样访问其产品成员: void uCProducts_click(object sende

我在主窗体中有一个
FlowLayoutPanel
,并且
FlowLayoutPanel
中填充了用户控件(类型为
UCProducts

My UserControl有一个PictureBox和一个对象(属于
Product
Class)

用户控制:(简化):

表格:


Sender
能否直接发送用户控件?如何在表单中提取产品对象?

一种方法是通过PictureBox控件的属性访问UserControl。然后,您可以像平常一样访问其
产品
成员:

void uCProducts_click(object sender, EventArgs e)
{
    PictureBox tempPic = (PictureBox)sender;
    UCProducts ucProducts = tempPic.Parent as UCProducts;
    Product theProduct = ucProducts.product;
}
不过,您可能应该转换
产品
。另外,试着坚持正确的方向

for (int i = 0; i < SomeList.Count; i++)
{
    uCProducts = new UCProducts(SomeList[i]);
    uCProducts.ptcbProduct.Click += new EventHandler(uCProducts_click);
    flpClothing.Controls.Add(uCProducts);
}
void uCProducts_click(object sender, EventArgs e)
{
    PictureBox tempPic = (PictureBox)sender;
}
void uCProducts_click(object sender, EventArgs e)
{
    PictureBox tempPic = (PictureBox)sender;
    UCProducts ucProducts = tempPic.Parent as UCProducts;
    Product theProduct = ucProducts.product;
}