C#如何将图片框的点击事件组合到对应的函数中?
C# How to combine the picturebox click event to the corresponding function?
C#如何将图片框的点击事件组合到对应的函数中?
private void pbx_process(int i)
{
PictureBox picture_box
= this.Controls.Find("pbx_image" + i, true).FirstOrDefault()
as PictureBox;
if (picture_box.Image != null) pbx_image.Image = picture_box.Image;
else pbx_image.Image = null;
}
private void pbx_image1_Click(object sender, EventArgs e)
{
pbx_process(1);
}
private void pbx_image2_Click(object sender, EventArgs e)
{
pbx_process(2);
}
private void pbx_image3_Click(object sender, EventArgs e)
{
pbx_process(3);
}
我想让这些变得简单。请帮忙....
像这样创建一个函数
AddEvent()
{
pbx_image1.click+=pbx_image_Click;
pbx_image2.click+=pbx_image_Click;
pbx_image3.click+=pbx_image_Click;
}
然后
private void pbx_image_Click(object sender, EventArgs e)
{
pbx_process(sender as PictureBox); or you can do the process right here
}
然后
private void pbx_process(PictureBox pictureBox)
{
if (pictureBox.Image != null) pbx_image.Image = pictureBox.Image;
else pbx_image.Image = null;
}
记得在加载表单后调用 AddEvent 函数以将事件添加到图片框。
C#如何将图片框的点击事件组合到对应的函数中?
private void pbx_process(int i)
{
PictureBox picture_box
= this.Controls.Find("pbx_image" + i, true).FirstOrDefault()
as PictureBox;
if (picture_box.Image != null) pbx_image.Image = picture_box.Image;
else pbx_image.Image = null;
}
private void pbx_image1_Click(object sender, EventArgs e)
{
pbx_process(1);
}
private void pbx_image2_Click(object sender, EventArgs e)
{
pbx_process(2);
}
private void pbx_image3_Click(object sender, EventArgs e)
{
pbx_process(3);
}
我想让这些变得简单。请帮忙....
像这样创建一个函数
AddEvent()
{
pbx_image1.click+=pbx_image_Click;
pbx_image2.click+=pbx_image_Click;
pbx_image3.click+=pbx_image_Click;
}
然后
private void pbx_image_Click(object sender, EventArgs e)
{
pbx_process(sender as PictureBox); or you can do the process right here
}
然后
private void pbx_process(PictureBox pictureBox)
{
if (pictureBox.Image != null) pbx_image.Image = pictureBox.Image;
else pbx_image.Image = null;
}
记得在加载表单后调用 AddEvent 函数以将事件添加到图片框。