By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

Setting the back ground image of Buttons in an Array

P: 36
Expand|Select|Wrap|Line Numbers
  1.  
  2. private void ChangeButtonsBackgroundimage(string[] NamesOfImageList,string FolderName)
  3.     {
  4.      try
  5.       {
  6.         ArrayList Arr_Button = new ArrayList();
  7.         Arr_Button.Add(this.button1);
  8.         Arr_Button.Add(this.button2);
  9.         Arr_Button.Add(this.button3);
  10.  
  11.         for (int i = 0; i < NamesOfImageList.Length; i++)
  12.         {
  13.            Button TempButton = new Button();
  14.            string FileName = FolderName + @"\" + NamesOfImageList[i] + ".bmp";
  15.            FileName = Get_ImagePath(FileName);
  16.            TempButton.BackgroundImage = Image.FromFile(FileName);
  17.             Arr_Button[i] = TempButton;
  18.  
  19.        }
  20.      }
  21.      catch (Exception ex)
  22.      { }
  23. }
  24.  
Buttons after this function doesn't appear the Background image while I am trace code I found that all property values of TempButtone including Backgroundimage was set to Arr_Button[i] ,BUT why it doesn't appear ??

Thanx in Advance .
Dec 17 '08 #1
Share this Question
Share on Google+
4 Replies


nukefusion
Expert 100+
P: 221
Because the button that you've added to the array isn't the button in the Controls collection of the form. In your loop you are just creating a new button object and setting the background of that. The button on the form is a different object.

You'll need to change your code to remove the temporary button objects you are creating:

Expand|Select|Wrap|Line Numbers
  1.  private void ChangeButtonsBackgroundimage(string[] NamesOfImageList, string FolderName)
  2.         {
  3.             try
  4.             {
  5.                 List<Button> buttons = new List<Button>();
  6.                 buttons.Add(this.button1);
  7.                 buttons.Add(this.button2);
  8.                 buttons.Add(this.button3);
  9.  
  10.                 for (int i = 0; i < NamesOfImageList.Length; i++)
  11.                 {
  12.                     string FileName = Path.Combine(FolderName, NamesOfImageList[i]);
  13.                     buttons[i].BackgroundImage = Image.FromFile(FileName);
  14.  
  15.                 }
  16.             }
  17.             catch (Exception ex)
  18.             { }
  19.         }
  20.  
Dec 17 '08 #2

Expert 100+
P: 750
PRR
@MarwaAlSagheer
((Arr_Button[i])as Button).BackgroundImage= TempButton.BackgroundImage;
nukefusion has a better solution....
Dec 17 '08 #3

P: 36
Mr.nukefusion and Mr.DeepBlue Thanks alooot :) Two solutions are correct.
Have a nice Day
Dec 17 '08 #4

Expert 100+
P: 750
PRR
@MarwaAlSagheer
Glad we could help ... Do continue to post your queries on Bytes
You too have a great day!
Dec 17 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.