How to modify the foreach identifier?
-
I have several PictureBox Controls in my Form. I want to change the Image property of one of them depending on its Tag property. This is my code:
foreach(Control ctrl in this.controls) { if(ctrl.GetType().ToString() == "System.Windows.Form.PictureBox") { System.Windows.Form.PictureBox temPic = (System.Windows.Form.PictureBox)ctrl; if(tempPic.Tag="1") { tempPic.Image = someImg; //an image object ctrl = tempPic; } } }
Doing this I get an error:Cannot assign to 'ctrl' because it is a 'foreach iteration variable'
Is there any workaround for this or any ideas in order to achive this goal? Any help would be apreciated :) Ivan -
I have several PictureBox Controls in my Form. I want to change the Image property of one of them depending on its Tag property. This is my code:
foreach(Control ctrl in this.controls) { if(ctrl.GetType().ToString() == "System.Windows.Form.PictureBox") { System.Windows.Form.PictureBox temPic = (System.Windows.Form.PictureBox)ctrl; if(tempPic.Tag="1") { tempPic.Image = someImg; //an image object ctrl = tempPic; } } }
Doing this I get an error:Cannot assign to 'ctrl' because it is a 'foreach iteration variable'
Is there any workaround for this or any ideas in order to achive this goal? Any help would be apreciated :) IvanHi, your code doesn't make much sense: 1. the statement
ctrl = tempPic;
would do no good since tempPic already equals ctrl due to a previous assignment. 2. why set a new value to ctrl, it is not used anywhere. (if you were to use PRE tags, see the "code block" button, you would have preserved the formatting of your code, making things much easier to read and understand). BTW1:if(ctrl.GetType().ToString() == "System.Windows.Form.PictureBox")...
is horrible; what you probably want is:if (ctrl is PictureBox)...
or the "as" keyword (see below). BTW2: your code will not compile, e.g.this.controls
and...Tag="1"
are wrong Maybe this is what you want overall:foreach(Control ctrl in Controls) {
PictureBox pb=ctrl as PictureBox;
if(pb!=null && pb.Tag=="1") pb.Image=someImg;
}I would like to suggest you buy a tutorial book on C# and study it; that will teach you all the basics in a structured and logical way, explaining the rationale and providing good examples. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, your code doesn't make much sense: 1. the statement
ctrl = tempPic;
would do no good since tempPic already equals ctrl due to a previous assignment. 2. why set a new value to ctrl, it is not used anywhere. (if you were to use PRE tags, see the "code block" button, you would have preserved the formatting of your code, making things much easier to read and understand). BTW1:if(ctrl.GetType().ToString() == "System.Windows.Form.PictureBox")...
is horrible; what you probably want is:if (ctrl is PictureBox)...
or the "as" keyword (see below). BTW2: your code will not compile, e.g.this.controls
and...Tag="1"
are wrong Maybe this is what you want overall:foreach(Control ctrl in Controls) {
PictureBox pb=ctrl as PictureBox;
if(pb!=null && pb.Tag=="1") pb.Image=someImg;
}I would like to suggest you buy a tutorial book on C# and study it; that will teach you all the basics in a structured and logical way, explaining the rationale and providing good examples. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
You left nothing for him :)
Sincerely Samer Abu Rabie Imagination is more important than knowledge !