I'm trying to add pictureboxes dynamically to a C# win32 form for a near-production-quality application I'm trying to build, and I've got it pretty much down.
The problem I'm running into is that I can't seem to add pictureboxes or controls to the form dynamically, in a method. I have added them to the form via the Form1 initializing method, but if I add controls in, say, a button_click method, it simply doesn't add them unless I have a panel container, and type panel1.Controls.Add(stuff). But, then the picturebox appears in a completely different location than intended - and blocked by, apparently, the panel itself. Most of the picturebox is blocked by the panel, and part of it is -outside- the panel. I have no idea what's going on.
Here's the code I'm using that I learned online to add the pictureboxes, in a button click method:
PictureBox pb = new PictureBox();
pb.Size = new Size(this.Size.Width / 14, this.Size.Width / 12); //I use this picturebox simply to debug and see if I can create a single picturebox, and that way I can tell if something goes wrong with my array of pictureboxes. Thus far however, neither are working.
pb.BackgroundImage = Properties.Resources.cardback;
pb.BackgroundImageLayout = ImageLayout.Stretch;
pb.Location = new Point(50, 50);
pb.Anchor = AnchorStyles.Left;
pb.Visible = true;
InitializeComponent();
this.Controls.Add(pb);
PictureBox[] pbName = new PictureBox[totaldeckcount];
for (int i = 0; i < totaldeckcount; i++)
{
pbName[i] = new PictureBox();
pbName[i].Size = new Size(this.Size.Width / 14, this.Size.Width / 12);
pbName[i].BackgroundImage = Properties.Resources.cardback;
pbName[i].BackgroundImageLayout = ImageLayout.Stretch;
pbName[i].Image = Properties.Resources.cardback;
pbName[i].Anchor = AnchorStyles.Left;
pbName[i].Visible = true;
int x = 0;
int y = 15;
if (i > 10)
{
y += (int)((this.Size.Height * i) + 30);
}
x = (int)((this.Size.Width / 12) * Math.IEEERemainder(i, 10));
pbName[i].Location = new Point(x, y);
this.Controls.Add(pbName[i]);
}
Cardback is a working texture, I've seen the thing pop up when I try making a picturebox with it in Form1's method, so that's not the problem. The problem doesn't -appear- to be my syntax, since I was able to copy the
PictureBox pb = new PictureBox();
code directly into the Form1 method and it executed just fine.
I can't find anything online via google, and this has me completely stumped.
InitializeComponent();call which is inside theforloop. - keyboardPpbName[i].Parent = this;- Sriram SakthivelLocationproperty anywhere - Sriram Sakthivel