在C#形式的背景圖像,我創建了一個功能,將,調用一次,與所需的圖像,大小和位置創建一個圖片:C#更改多個控件
private void NewPic(string nName, int locX, int locY, int SizX, int SizY, Image Img)
{
PictureBox Pic = new PictureBox();
Pic.Name = nName; Pic.Image = Img;
Pic.BackColor = Color.Transparent;
Pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
Pic.Size = new System.Drawing.Size(SizX, SizY);
Pic.Location = new System.Drawing.Point(locX, locY);
Controls.Add(Pic);
Pic.Click += new EventHandler(Pic_Click);
}
現在,當我需要一個圖片我只是這樣做:
NewPic("FIRE", 32, 100, 120, 120, Properties.Resources.Image);
是,在點擊事件,當我點擊圖片框,我希望它改變它的背景圖像,但是,如果我點擊了一些其他的PictureBox我想最後一個復位的問題它自我:
private void Pic_Click(object sender, System.EventArgs e)
{
PictureBox pb = (PictureBox)sender;
switch (pb.Name)
{
case "1":
pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
pb.BackgroundImageLayout = ImageLayout.Stretch;
//INSERT CODE HERE: to remove from other if it has
break;
case "2":
pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
pb.BackgroundImageLayout = ImageLayout.Stretch;
//INSERT CODE HERE: to remove from other if it has
break;
}
}
我需要一個可以應用到多個pictureboxes /對象的代碼,而不僅僅是兩個
是>,這是我想念的,謝謝你,謝謝大家。如果你有更多的建議,我會認爲它們都是:) –
「switch語句」似乎也是重複的,可以在每個圖片框都有相同背景圖像的情況下被消除。 –
這是...請詳細說明一下嗎? –