C#如何将picturebox点击事件与相应的功能结合起来?

问题描述 投票:0回答:1

C#如何将picturebox点击事件与相应的功能结合起来?

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); 
}

我想让这些变得简单。请帮忙....

c# picturebox
1个回答
0
投票

创建一个这样的函数

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函数以将事件添加到图片框。

© www.soinside.com 2019 - 2024. All rights reserved.