We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,787 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to loop through controls(different).

Hi, I need to loop through a bunch of picture boxes that I have, all with names such as:

PictureBox00, PictureBox01, PictureBox02 etc.

I need to loop through these and adjust their values by what the current value in the loop is, I basically want to do this to it:

for (int i = 0; i < 3; i++)
{
PictureBox(i).enabled = false;
}

I need to access them like an array, I will not know what picturebox I would be dealing with at that time, only way I can see to do this is by using the loop, cheers.


EDIT: I think this was pretty easy to do in VB.NET via DirectCast, not sure if that would be used here(the similar version).

3
Contributors
5
Replies
3 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
Bountyhuntr
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi, Bountyhuntr, welcome at daniweb :)
Perhaps this C# snippet can solve your problem.

ddanbe
Industrious Poster
4,283 posts since Oct 2008
Reputation Points: 2,121
Solved Threads: 721
Skill Endorsements: 26

Hi, Bountyhuntr, welcome at daniweb :)
Perhaps this C# snippet can solve your problem.

I don't think that will help me as I need to access the correct picturebox which matches the current counter in the loop, i.e on the 3rd loop it needs to be the 3rd textbox which is why I chose the names for the textbox, hoping I could access the textbox number which corrisponds to the counter.

Bountyhuntr
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Is it not possible to extract the number out of the PictureBox Name with the substring method?

ddanbe
Industrious Poster
4,283 posts since Oct 2008
Reputation Points: 2,121
Solved Threads: 721
Skill Endorsements: 26

You can get a list of the PictureBoxes in the Controls collection using the linq extension method OfType.
Then sort the list to get them in name order.

var myPics = this.Controls.OfType<PictureBox>().ToList();
myPics.Sort((a, b) => { return a.Name.CompareTo(b.Name); });

You should only need to do this once, in the form constructor after the Initialize method.

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 188
Skill Endorsements: 3

@nick.crane: great code , my snippet dates from the time before I knew of the existance of LINQ.

ddanbe
Industrious Poster
4,283 posts since Oct 2008
Reputation Points: 2,121
Solved Threads: 721
Skill Endorsements: 26

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0821 seconds using 2.76MB