2012-05-06 45 views
0

在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 /對象的代碼,而不僅僅是兩個

回答

1

我認爲你需要存儲最後圖片框的圖像太

PictureBox _lastPictureBox = null; 
Image _lastPictureBoxImage = null; 

private void Pic_Click(object sender, System.EventArgs e) 
{ 
    PictureBox pb = (PictureBox)sender; 
    if (_lastPictureBox != null) 
    { 
     // update the previous one, eg: 
     _lastPictureBox.BackgroundImage = _lastPictureBoxImage; 
    } 

    // now set it to the current one: 
    _lastPictureBox = pb; 
    _lastPictureBoxImage = pb.Image; 
    switch (pb.Name) 
    { 
    case "1": 
     pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background 
     pb.BackgroundImageLayout = ImageLayout.Stretch; 
    break; 
    case "2": 
     pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background 
     pb.BackgroundImageLayout = ImageLayout.Stretch; 
    break; 
    } 

}

+0

是>,這是我想念的,謝謝你,謝謝大家。如果你有更多的建議,我會認爲它們都是:) –

+0

「switch語句」似乎也是重複的,可以在每個圖片框都有相同背景圖像的情況下被消除。 –

+0

這是...請詳細說明一下嗎? –

1

做到這一點,最簡單的方法是將成員添加到您的形式,將跟蹤先前點擊圖片框:

PictureBox _lastPictureBox = null; 

在處理程序中,檢查是否_lastPictureBox有一個值,並根據需要更新:

private void Pic_Click(object sender, System.EventArgs e) 
{ 
    PictureBox pb = (PictureBox)sender; 
    if (_lastPictureBox != null) 
    { 
     // update the previous one, eg: 
     _lastPictureBox.BackgroundImage = Properties.Resources.FirstImg; 
    } 

    // now set it to the current one: 
    _lastPictureBox = pb; 

    switch (pb.Name) 
    { 
    case "1": 
     pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background 
     pb.BackgroundImageLayout = ImageLayout.Stretch; 
     break; 
    case "2": 
     pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background 
     pb.BackgroundImageLayout = ImageLayout.Stretch; 
     break; 
    } 
} 
+0

嗯,我覺得這不管用。問題是我有4個創建的pictureboxes,所以他們不是空的,所以當_lastpicturebox = null什麼都不會發生,因爲我沒有空的pictureboxes –

+0

@AntonioTehSumtin_lastPictureBox最初爲null,所以它什麼也不做。但用戶第一次點擊圖片時,_lastPictureBox會獲取該圖片的引用(因此您可以在下次點擊時將其重置)。 – McGarnagle

0

如果我理解正確的話,你需要一個全局變量

PictureBox lastbox; 

那麼你可以插入此代碼:

lastbox.Image = Properties.Resources.Image; 
lasbox = pb; 
+0

我很抱歉,但我真的不認爲你幫了太多^^'謝謝你對此表示感謝。 :) –