473,322 Members | 1,846 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,322 software developers and data experts.

Concantenated Object Naming in a Loop

Hello,

I am trying to get this code:

p1_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[0].ToString() + ".GIF");
p2_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[1].ToString() + ".GIF");
p3_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[2].ToString() + ".GIF");
p4_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[3].ToString() + ".GIF");
p5_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[4].ToString() + ".GIF");
p6_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[5].ToString() + ".GIF");
p7_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[6].ToString() + ".GIF");
p8_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[7].ToString() + ".GIF");
p9_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[8].ToString() + ".GIF");
p10_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[9].ToString() + ".GIF");

To work like this:

for (int i=1;i<11;i++) {

"p"+i+"_1".Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[0].ToString() + ".GIF");

}
But I can't figure out the syntax. Thanks

Jun 4 '06 #1
5 2311
>To work like this:

for (int i=1;i<11;i++) {

"p"+i+"_1".Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[0].ToString() + ".GIF");

}
But I can't figure out the syntax. Thanks


Assuming the pX_1 varaibles refer to PictureBox controls you can do
something like this

PictureBox[] pics = {p1_1, p2_1, p3_1, p4_1, p5_1, p6_1, p7_1, p8_1,
p9_1, p10_1};
for (int i = 0; i < pics.Length; i++) {
pics[i].Image = Bitmap.FromFile(Path.Combine(imgPath,
picture[i].ToString() + ".GIF"));
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 4 '06 #2
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ut**************@TK2MSFTNGP03.phx.gbl...
Assuming the pX_1 varaibles refer to PictureBox controls you can do
something like this

PictureBox[] pics = {p1_1, p2_1, p3_1, p4_1, p5_1, p6_1, p7_1, p8_1,
p9_1, p10_1};
for (int i = 0; i < pics.Length; i++) {
pics[i].Image = Bitmap.FromFile(Path.Combine(imgPath,
picture[i].ToString() + ".GIF"));
}


another option is to use:

foreach(Control c in this.Controls)
{
PictureBox p = c as PictureBox;
if(p != null)
etc

Michael
Jun 5 '06 #3
TheLostLeaf wrote:
Hello,

I am trying to get this code:

p1_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[0].ToString() + ".GIF");
p2_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[1].ToString() + ".GIF");
p3_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[2].ToString() + ".GIF");
p4_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[3].ToString() + ".GIF");
p5_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[4].ToString() + ".GIF");
p6_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[5].ToString() + ".GIF");
p7_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[6].ToString() + ".GIF");
p8_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[7].ToString() + ".GIF");
p9_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[8].ToString() + ".GIF");
p10_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[9].ToString() + ".GIF");

To work like this:

If p1_1 is a picture box on a form then:
for (int i=1;i<11;i++) {

"p"+i+"_1".Image = System.Drawing.Bitmap.FromFile(imgPath +
(this.Controls["p"+i+"_1"] as PictureBox).Image =
System.Drawing.Bitmap.FromFile(imgPath + picture[i-1].ToString() + ".GIF");
picture[0].ToString() + ".GIF");

}
But I can't figure out the syntax. Thanks


Note, this will only work with .net 2.0 which exposes a string indexer
for the controls property as well as the default int one.
Otherwise you would have to loop through the controls and find the
correct one by name.

HTH
JB
Jun 5 '06 #4
John B wrote:
TheLostLeaf wrote:
Hello,

I am trying to get this code:

p1_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[0].ToString() + ".GIF");
p2_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[1].ToString() + ".GIF");
p3_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[2].ToString() + ".GIF");
p4_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[3].ToString() + ".GIF");
p5_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[4].ToString() + ".GIF");
p6_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[5].ToString() + ".GIF");
p7_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[6].ToString() + ".GIF");
p8_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[7].ToString() + ".GIF");
p9_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[8].ToString() + ".GIF");
p10_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[9].ToString() + ".GIF");

To work like this:

If p1_1 is a picture box on a form then:
for (int i=1;i<11;i++) {

"p"+i+"_1".Image = System.Drawing.Bitmap.FromFile(imgPath +


(this.Controls["p"+ i.ToString() +"_1"] as PictureBox).Image =

^^^ Oops :) System.Drawing.Bitmap.FromFile(imgPath + picture[i-1].ToString() + ".GIF");
picture[0].ToString() + ".GIF");

}
But I can't figure out the syntax. Thanks


Note, this will only work with .net 2.0 which exposes a string indexer
for the controls property as well as the default int one.
Otherwise you would have to loop through the controls and find the
correct one by name.

HTH
JB

Jun 5 '06 #5
If still on 1.0/1.1 framework, and not too worried about performance,
why not use System.Reflection ?

John B wrote:
TheLostLeaf wrote:
Hello,

I am trying to get this code:

p1_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[0].ToString() + ".GIF");
p2_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[1].ToString() + ".GIF");
p3_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[2].ToString() + ".GIF");
p4_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[3].ToString() + ".GIF");
p5_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[4].ToString() + ".GIF");
p6_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[5].ToString() + ".GIF");
p7_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[6].ToString() + ".GIF");
p8_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[7].ToString() + ".GIF");
p9_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[8].ToString() + ".GIF");
p10_1.Image = System.Drawing.Bitmap.FromFile(imgPath +
picture[9].ToString() + ".GIF");

To work like this:


If p1_1 is a picture box on a form then:
for (int i=1;i<11;i++) {

"p"+i+"_1".Image = System.Drawing.Bitmap.FromFile(imgPath +


(this.Controls["p"+i+"_1"] as PictureBox).Image =
System.Drawing.Bitmap.FromFile(imgPath + picture[i-1].ToString() + ".GIF");
picture[0].ToString() + ".GIF");

}
But I can't figure out the syntax. Thanks


Note, this will only work with .net 2.0 which exposes a string indexer
for the controls property as well as the default int one.
Otherwise you would have to loop through the controls and find the
correct one by name.

HTH
JB


Jun 5 '06 #6

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

Similar topics

2
by: Aaron | last post by:
I have a data sructure setup and I populate it in a loop like so: y=0 while X: DS.name = "ASDF" DS.ID = 1234 list = DS; y = y + 1
12
by: Chad Z. Hower aka Kudzu | last post by:
Object MyOnject = new Object; What does the signify? -- Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/ "Programming is an art form that fights back"
8
by: Lance | last post by:
I've been teaching myself C# for a few months now and I have a concept problem. I know how to instantiate an object from a class I’ve created, but now I want to retrieve and store data in a...
6
by: Ed Jay | last post by:
<disclaimer>New to js.</disclaimer> I have several pages, each with menues comprising checkboxes or radio boxes within the same form. I presently 'brute force' clear the buttons with individual...
3
by: rsine | last post by:
I have searched around a little and have yet to find a naming convention for the string builder object. I really do not want to use "str" since this is for string objects and thus could be...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. ...
23
by: Thorsten Kampe | last post by:
Okay, I hear you saying 'not another naming conventions thread'. I've read through Google and the 'naming conventions' threads were rather *spelling conventions* threads. I'm not interested...
10
rrocket
by: rrocket | last post by:
I have a bunch of params: InputParam1, InputParam2, InputParam3, etc... I would like to loop through them instead of writing out code for each one, but am having some issues getting it to work...
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.