In C# on Form1 I genetate an array of PictureBoxes and populate each with an
image as seen in the code below.
Later on I want to access a specific PictureBox to change its image, but I
keep getting the error "The name 'PictureBox1' does not exist in the current
context"
The code I use to try to access the PictureBox is "PictureBox1[Target].Image
= HoldBitMap; " where kount is an integer.
I know the answer is probably in using the Controls collection but I can't
get the syntax correct.
Thanks in advance for your help.
Jim


private void InitializePictureBox()
{
//allocate the picture box
PictureBox[] PictureBox1 = new PictureBox[1000];
int i;
int j;
int kount = 0;
Random randObj = new Random(unchecked((int)
(DateTime.Now.Ticks)));

//initialise each picture box
for(i=0; i<8; i++)
{
for (j = 0; j < 6; j++)
{
PictureBox1[kount] = new PictureBox();

// Set the location and size of the PictureBox control.
PictureBox1[kount].Location = new
System.Drawing.Point(100 * i, 100 * j);
PictureBox1[kount].Size = new System.Drawing.Size(99,
99);
PictureBox1[kount].TabStop = false;

// Set the SizeMode property to the StretchImage value.
This
// will shrink or enlarge the image as needed to fit
into
// the PictureBox.
PictureBox1[kount].SizeMode = PictureBoxSizeMode.Zoom;
// .StretchImage;

// Set the border style to a three-dimensional border.
PictureBox1[kount].BorderStyle = BorderStyle.Fixed3D;

//Wire the picture boxes click to new common click
handler.
PictureBox1[kount].Click += new
System.EventHandler(ClickHandler);
PictureBox1[kount].Tag = kount;
// Add the PictureBox to the form.
Controls.Add(PictureBox1[kount]);
// Add the image
PictureBox1[kount].Image = MakeBitMap();
kount++;
}
}
}

Re: How to access a run time generated PictureBox on the form by Peter

Peter
Thu May 08 22:52:20 CDT 2008

On Thu, 08 May 2008 19:26:20 -0700, Jim McGivney <mcgivney@winid.com> =

wrote:

> In C# on Form1 I genetate an array of PictureBoxes and populate each =

> with an image as seen in the code below.
> Later on I want to access a specific PictureBox to change its image, =
=

> but I keep getting the error "The name 'PictureBox1' does not exist in=
=

> the current context"
> The code I use to try to access the PictureBox is =

> "PictureBox1[Target].Image =3D HoldBitMap; " where kount is an intege=
r.
> I know the answer is probably in using the Controls collection but I =

> can't get the syntax correct.

You could indeed use the controls collection. However, it would be easi=
er =

and more reliable for you to just declare your array as an instance memb=
er =

in your class, rather than as a local variable as you've done in the cod=
e =

you posted. Then the array would be available anywhere in your class, =

rather than just that one method.

Pete

Re: How to access a run time generated PictureBox on the form by Jim

Jim
Fri May 09 09:12:29 CDT 2008

Thanks for your help. Could you possibly show me some code (syntax) that I
can use to make my pictureBox array an instance member using the code in my
posting. Thanks for your help,
Jim


"Jim McGivney" <mcgivney@winid.com> wrote in message
news:05F78C94-A0D9-440B-8C06-711ABEEF23F2@microsoft.com...
> In C# on Form1 I genetate an array of PictureBoxes and populate each with
> an image as seen in the code below.
> Later on I want to access a specific PictureBox to change its image, but
> I keep getting the error "The name 'PictureBox1' does not exist in the
> current context"
> The code I use to try to access the PictureBox is
> "PictureBox1[Target].Image = HoldBitMap; " where kount is an integer.
> I know the answer is probably in using the Controls collection but I can't
> get the syntax correct.
> Thanks in advance for your help.
> Jim
>
>
> private void InitializePictureBox()
> {
> //allocate the picture box
> PictureBox[] PictureBox1 = new PictureBox[1000];
> int i;
> int j;
> int kount = 0;
> Random randObj = new Random(unchecked((int)
> (DateTime.Now.Ticks)));
>
> //initialise each picture box
> for(i=0; i<8; i++)
> {
> for (j = 0; j < 6; j++)
> {
> PictureBox1[kount] = new PictureBox();
>
> // Set the location and size of the PictureBox control.
> PictureBox1[kount].Location = new
> System.Drawing.Point(100 * i, 100 * j);
> PictureBox1[kount].Size = new System.Drawing.Size(99,
> 99);
> PictureBox1[kount].TabStop = false;
>
> // Set the SizeMode property to the StretchImage value.
> This
> // will shrink or enlarge the image as needed to fit
> into
> // the PictureBox.
> PictureBox1[kount].SizeMode = PictureBoxSizeMode.Zoom;
> // .StretchImage;
>
> // Set the border style to a three-dimensional border.
> PictureBox1[kount].BorderStyle = BorderStyle.Fixed3D;
>
> //Wire the picture boxes click to new common click
> handler.
> PictureBox1[kount].Click += new
> System.EventHandler(ClickHandler);
> PictureBox1[kount].Tag = kount;
> // Add the PictureBox to the form.
> Controls.Add(PictureBox1[kount]);
> // Add the image
> PictureBox1[kount].Image = MakeBitMap();
> kount++;
> }
> }
> }


Re: How to access a run time generated PictureBox on the form by Marc

Marc
Fri May 09 09:25:12 CDT 2008

Simply move it outside of the method.
For the record, I'd probably use a List<PitureBox> rather than a
PictureBox[] so that I can resize it more easily...

Marc


// declare the field
PictureBox[] PictureBox1;

private void InitializePictureBox()
{
//allocate the picture box
PictureBox1 = new PictureBox[1000];

// ...
}

Re: How to access a run time generated PictureBox on the form by Jim

Jim
Fri May 09 14:46:12 CDT 2008

Thanks for your help. It worked.
Jim


"Marc Gravell" <marc.gravell@gmail.com> wrote in message
news:%23yfh$BesIHA.4848@TK2MSFTNGP05.phx.gbl...
> Simply move it outside of the method.
> For the record, I'd probably use a List<PitureBox> rather than a
> PictureBox[] so that I can resize it more easily...
>
> Marc
>
>
> // declare the field
> PictureBox[] PictureBox1;
>
> private void InitializePictureBox()
> {
> //allocate the picture box
> PictureBox1 = new PictureBox[1000];
>
> // ...
> }