I am creating a form, and on load it gets all images from my resources folder and for each file creates a new button, sets the buttons background image to that image and adds that button to the form, but it is only displaying 1 button and there are 36 files in the resources folder.
My code is as follows:
ResourceSet resourceSet = Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
object resource = entry.Value;
Button b = new Button();
b.BackgroundImage = (Image)resource;
b.BackgroundImageLayout = ImageLayout.Stretch;
b.Height = 64;
b.Width = 64;
this.Controls.Add(b);
}
Please assist on what I am doing wrong.
My guess is that the code is indeed adding all the buttons but that they are all on top of each other. Each button will have a default value for
LeftandTopand those default values will be the same for each button. Since the buttons all have the same size, only the top button is visible.Solve the problem by setting the
LeftandTopproperties for each button. Obviously each different button needs to have a different value forLeftandTop.To answer the question you ask in a comment, you could use code along these lines: