473,396 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to access a run time generated PictureBox on the form

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++;
}
}
}

Jun 27 '08 #1
4 3674
On Thu, 08 May 2008 19:26:20 -0700, Jim McGivney <mc******@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 = 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.
You could indeed use the controls collection. However, it would be easier
and more reliable for you to just declare your array as an instance member
in your class, rather than as a local variable as you've done in the code
you posted. Then the array would be available anywhere in your class,
rather than just that one method.

Pete
Jun 27 '08 #2
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" <mc******@winid.comwrote in message
news:05**********************************@microsof t.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++;
}
}
}
Jun 27 '08 #3
Simply move it outside of the method.
For the record, I'd probably use a List<PitureBoxrather 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];

// ...
}
Jun 27 '08 #4
Thanks for your help. It worked.
Jim
"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Simply move it outside of the method.
For the record, I'd probably use a List<PitureBoxrather 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];

// ...
}
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Pavan Arise | last post by:
Dear all.. I have a picturebox filled on a form. The picturebox has some graphics displayed on it.I was trying to save the picturebox, but continuesly failed to do so. I am clueless of why it is...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
7
by: utkarsh | last post by:
Hi, I am using the following code to the update a section of the image which is drawn on a panel. this.m_Timer = new System.Windows.Forms.Timer(this.components); this.m_Timer.Tick += new...
4
by: 6tc1 | last post by:
Hi all, I have just finished debugging a windows application and have solved the problem - however, I want to be sure that I understand the problem before I move on. Before I detail the problem,...
0
by: akh | last post by:
I want to use de Drag and Drop ´s event to move a picture box from a form and within a Picture Box. But I have behaviour if the MyPBox As PictureBox as the Globale varible or not Thanks for...
3
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
1
by: Amit Dedhia | last post by:
Hi I am having problem working with Timers in C++/CLI (the .NET version of C++) I have an application which has several forms with pictureBox controls on it. There is a background timer...
5
by: AWW | last post by:
XP VB 2005 running an example from help that creates a picturebox in code - the picturebox is not created. If I comment out the "Dim Box as New PictureBox" and create it in Design mode - the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.