将图片从用户控件传递到窗体
pass picture from usercontrol to Form
我有一个包含 1 个面板的主窗体,
该面板有 10 个用户控件和
每个用户控件都有图片框。
主要形式:
private void Form1_Load(object sender, EventArgs e)
{
Picturebox1.Image=....
for(int i=0;i<10;i++)
{
uscontrol a=new uscontrol()
{
usimage=Image.Fromfile....
};
panel1.Controls.Add(a);
}
}
用户控制:
public Image usimage
{
get { return imagebox.Image; }
set { imagebox.Image = value; }
}
当我点击其中一个用户控件时,我该怎么做,它将那个用户控件的图像传递给主窗体并在 Picturebox1 中显示它,
谢谢。
首先,您需要将 MouseClickEvent
添加到每个 uscontrol
。为此,请将您的 Form1_Load
编辑为此。
Picturebox1.Image=....
for(int i=0;i<10;i++)
{
uscontrol a=new uscontrol()
{
usimage=Image.Fromfile....
};
a.MouseClick += new MouseEventHandler(USControl_Clicked); //Note this added line
panel1.Controls.Add(a);
}
然后您需要将 USControl_Clicked
添加到您的 From1
代码中。
private void USControl_Clicked(object sender, MouseEventArgs e)
{
//Here we cast sender to your uscontrol class and access usimage
Picturebox1.Image = (sender as uscontrol).usimage;
}
我有一个包含 1 个面板的主窗体, 该面板有 10 个用户控件和 每个用户控件都有图片框。
主要形式:
private void Form1_Load(object sender, EventArgs e)
{
Picturebox1.Image=....
for(int i=0;i<10;i++)
{
uscontrol a=new uscontrol()
{
usimage=Image.Fromfile....
};
panel1.Controls.Add(a);
}
}
用户控制:
public Image usimage
{
get { return imagebox.Image; }
set { imagebox.Image = value; }
}
当我点击其中一个用户控件时,我该怎么做,它将那个用户控件的图像传递给主窗体并在 Picturebox1 中显示它, 谢谢。
首先,您需要将 MouseClickEvent
添加到每个 uscontrol
。为此,请将您的 Form1_Load
编辑为此。
Picturebox1.Image=....
for(int i=0;i<10;i++)
{
uscontrol a=new uscontrol()
{
usimage=Image.Fromfile....
};
a.MouseClick += new MouseEventHandler(USControl_Clicked); //Note this added line
panel1.Controls.Add(a);
}
然后您需要将 USControl_Clicked
添加到您的 From1
代码中。
private void USControl_Clicked(object sender, MouseEventArgs e)
{
//Here we cast sender to your uscontrol class and access usimage
Picturebox1.Image = (sender as uscontrol).usimage;
}