Setting the back ground image of Buttons in an Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MarwaAlSagheer
    New Member
    • Nov 2008
    • 36

    Setting the back ground image of Buttons in an Array

    Code:
    private void ChangeButtonsBackgroundimage(string[] NamesOfImageList,string FolderName)
        {
         try
          {
            ArrayList Arr_Button = new ArrayList();
            Arr_Button.Add(this.button1);
            Arr_Button.Add(this.button2);
            Arr_Button.Add(this.button3);
                          
            for (int i = 0; i < NamesOfImageList.Length; i++)
            {
               Button TempButton = new Button();
               string FileName = FolderName + @"\" + NamesOfImageList[i] + ".bmp";
               FileName = Get_ImagePath(FileName);
               TempButton.BackgroundImage = Image.FromFile(FileName);
                Arr_Button[i] = TempButton;
                                
           }
         }
         catch (Exception ex)
         { }
    }
    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 .
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    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:

    Code:
     private void ChangeButtonsBackgroundimage(string[] NamesOfImageList, string FolderName)
            {
                try
                {
                    List<Button> buttons = new List<Button>();
                    buttons.Add(this.button1);
                    buttons.Add(this.button2);
                    buttons.Add(this.button3);
    
                    for (int i = 0; i < NamesOfImageList.Length; i++)
                    {
                        string FileName = Path.Combine(FolderName, NamesOfImageList[i]);
                        buttons[i].BackgroundImage = Image.FromFile(FileName);
    
                    }
                }
                catch (Exception ex)
                { }
            }

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      Originally posted by MarwaAlSagheer
      Code:
      private void ChangeButtonsBackgroundimage(string[] NamesOfImageList,string FolderName)
          {
           try
            {
              ArrayList Arr_Button = new ArrayList();
              Arr_Button.Add(this.button1);
              Arr_Button.Add(this.button2);
              Arr_Button.Add(this.button3);
                            
              for (int i = 0; i < NamesOfImageList.Length; i++)
              {
                 Button TempButton = new Button();
                 string FileName = FolderName + @"\" + NamesOfImageList[i] + ".bmp";
                 FileName = Get_ImagePath(FileName);
                 TempButton.BackgroundImage = Image.FromFile(FileName);
                  Arr_Button[i] = TempButton;
      [B]//try this[/B]
      //((Arr_Button[i])as Button).BackgroundImage= TempButton.BackgroundImage;
         //                         
             }
           }
           catch (Exception ex)
           { }
      }
      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 .
      ((Arr_Button[i])as Button).Backgro undImage= TempButton.Back groundImage;
      nukefusion has a better solution....

      Comment

      • MarwaAlSagheer
        New Member
        • Nov 2008
        • 36

        #4
        Mr.nukefusion and Mr.DeepBlue Thanks alooot :) Two solutions are correct.
        Have a nice Day

        Comment

        • PRR
          Recognized Expert Contributor
          • Dec 2007
          • 750

          #5
          Originally posted by MarwaAlSagheer
          Mr.nukefusion and Mr.DeepBlue Thanks alooot :) Two solutions are correct.
          Have a nice Day
          Glad we could help ... Do continue to post your queries on Bytes
          You too have a great day!

          Comment

          Working...